| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Serge Bazanski | 8bd2ab1 | 2021-12-17 17:32:16 +0100 | [diff] [blame] | 4 | package lib |
| 5 | |
| 6 | import ( |
| 7 | "go/ast" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | genPrefix = "// Code generated" |
| 13 | genSuffix = "DO NOT EDIT." |
| 14 | ) |
| 15 | |
| 16 | // IsGeneratedFile returns true if the file is generated according to |
| 17 | // https://golang.org/s/generatedcode and other heuristics. |
| 18 | func IsGeneratedFile(file *ast.File) bool { |
| 19 | for _, c := range file.Comments { |
| 20 | for _, t := range c.List { |
| 21 | if strings.HasPrefix(t.Text, genPrefix) && strings.HasSuffix(t.Text, genSuffix) { |
| 22 | return true |
| 23 | } |
| 24 | // Generated testmain.go stubs from rules_go - for some reason, they don't |
| 25 | // contain the expected markers. |
| 26 | if strings.Contains(t.Text, "This package must be initialized before packages being tested.") { |
| 27 | return true |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | return false |
| 32 | } |