forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilename_unadjuster.go
130 lines (103 loc) · 3.38 KB
/
filename_unadjuster.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package processors
import (
"go/parser"
"go/token"
"strings"
"sync"
"time"
"golang.org/x/tools/go/packages"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
var _ Processor = (*FilenameUnadjuster)(nil)
type posMapper func(pos token.Position) token.Position
type adjustMap struct {
sync.Mutex
m map[string]posMapper
}
// FilenameUnadjuster fixes filename based on adjusted and unadjusted position (related to line directives and cgo).
//
// A lot of linters use `fset.Position(f.Pos())` to get filename,
// and they return adjusted filename (e.g.` *.qtpl`) for an issue.
// We need restore real `.go` filename to properly output it, parse it, etc.
//
// Require absolute file path.
type FilenameUnadjuster struct {
m map[string]posMapper // map from adjusted filename to position mapper: adjusted -> unadjusted position
log logutils.Log
loggedUnadjustments map[string]bool
}
func NewFilenameUnadjuster(pkgs []*packages.Package, log logutils.Log) *FilenameUnadjuster {
m := adjustMap{m: map[string]posMapper{}}
startedAt := time.Now()
var wg sync.WaitGroup
wg.Add(len(pkgs))
for _, pkg := range pkgs {
go func(pkg *packages.Package) {
// It's important to call func here to run GC
processUnadjusterPkg(&m, pkg, log)
wg.Done()
}(pkg)
}
wg.Wait()
log.Infof("Pre-built %d adjustments in %s", len(m.m), time.Since(startedAt))
return &FilenameUnadjuster{
m: m.m,
log: log,
loggedUnadjustments: map[string]bool{},
}
}
func (*FilenameUnadjuster) Name() string {
return "filename_unadjuster"
}
func (p *FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, error) {
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
mapper := p.m[issue.FilePath()]
if mapper == nil {
return issue
}
newIssue := *issue
newIssue.Pos = mapper(issue.Pos)
if !p.loggedUnadjustments[issue.Pos.Filename] {
p.log.Infof("Unadjusted from %v to %v", issue.Pos, newIssue.Pos)
p.loggedUnadjustments[issue.Pos.Filename] = true
}
return &newIssue
}), nil
}
func (*FilenameUnadjuster) Finish() {}
func processUnadjusterPkg(m *adjustMap, pkg *packages.Package, log logutils.Log) {
fset := token.NewFileSet() // it's more memory efficient to not store all in one fset
for _, filename := range pkg.CompiledGoFiles {
// It's important to call func here to run GC
processUnadjusterFile(filename, m, log, fset)
}
}
func processUnadjusterFile(filename string, m *adjustMap, log logutils.Log, fset *token.FileSet) {
syntax, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
// Error will be reported by typecheck
return
}
adjustedFilename := fset.PositionFor(syntax.Pos(), true).Filename
if adjustedFilename == "" {
return
}
unadjustedFilename := fset.PositionFor(syntax.Pos(), false).Filename
if unadjustedFilename == "" || unadjustedFilename == adjustedFilename {
return
}
if !strings.HasSuffix(unadjustedFilename, ".go") {
return // file.go -> /caches/cgo-xxx
}
m.Lock()
defer m.Unlock()
m.m[adjustedFilename] = func(adjustedPos token.Position) token.Position {
tokenFile := fset.File(syntax.Pos())
if tokenFile == nil {
log.Warnf("Failed to get token file for %s", adjustedFilename)
return adjustedPos
}
return fset.PositionFor(tokenFile.Pos(adjustedPos.Offset), false)
}
}