aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gostaticanalysis/analysisutil/file.go
blob: b9b295530464b76d75181a65c18037bfcfc3ac89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package analysisutil

import (
	"go/ast"
	"go/token"
	"regexp"

	"golang.org/x/tools/go/analysis"
)

// File finds *ast.File in pass.Files by pos.
func File(pass *analysis.Pass, pos token.Pos) *ast.File {
	for _, f := range pass.Files {
		if f.Pos() <= pos && pos <= f.End() {
			return f
		}
	}
	return nil
}

var genCommentRegexp = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)

// IsGeneratedFile reports whether the file has been generated automatically.
// If file is nil, IsGeneratedFile will return false.
func IsGeneratedFile(file *ast.File) bool {
	if file == nil || len(file.Comments) == 0 {
		return false
	}
	return genCommentRegexp.MatchString(file.Comments[0].List[0].Text)
}