@@ -16,6 +16,11 @@ import (
16
16
17
17
type posMapper func (pos token.Position ) token.Position
18
18
19
+ type adjustMap struct {
20
+ sync.Mutex
21
+ m map [string ]posMapper
22
+ }
23
+
19
24
// FilenameUnadjuster is needed because a lot of linters use fset.Position(f.Pos())
20
25
// to get filename. And they return adjusted filename (e.g. *.qtpl) for an issue. We need
21
26
// restore real .go filename to properly output it, parse it, etc.
@@ -27,7 +32,7 @@ type FilenameUnadjuster struct {
27
32
28
33
var _ Processor = & FilenameUnadjuster {}
29
34
30
- func processUnadjusterPkg (m map [ string ] posMapper , pkg * packages.Package , log logutils.Log ) {
35
+ func processUnadjusterPkg (m * adjustMap , pkg * packages.Package , log logutils.Log ) {
31
36
fset := token .NewFileSet () // it's more memory efficient to not store all in one fset
32
37
33
38
for _ , filename := range pkg .CompiledGoFiles {
@@ -36,7 +41,7 @@ func processUnadjusterPkg(m map[string]posMapper, pkg *packages.Package, log log
36
41
}
37
42
}
38
43
39
- func processUnadjusterFile (filename string , m map [ string ] posMapper , log logutils.Log , fset * token.FileSet ) {
44
+ func processUnadjusterFile (filename string , m * adjustMap , log logutils.Log , fset * token.FileSet ) {
40
45
syntax , err := parser .ParseFile (fset , filename , nil , parser .ParseComments )
41
46
if err != nil {
42
47
// Error will be reported by typecheck
@@ -57,7 +62,9 @@ func processUnadjusterFile(filename string, m map[string]posMapper, log logutils
57
62
return // file.go -> /caches/cgo-xxx
58
63
}
59
64
60
- m [adjustedFilename ] = func (adjustedPos token.Position ) token.Position {
65
+ m .Lock ()
66
+ defer m .Unlock ()
67
+ m .m [adjustedFilename ] = func (adjustedPos token.Position ) token.Position {
61
68
tokenFile := fset .File (syntax .Pos ())
62
69
if tokenFile == nil {
63
70
log .Warnf ("Failed to get token file for %s" , adjustedFilename )
@@ -68,22 +75,23 @@ func processUnadjusterFile(filename string, m map[string]posMapper, log logutils
68
75
}
69
76
70
77
func NewFilenameUnadjuster (pkgs []* packages.Package , log logutils.Log ) * FilenameUnadjuster {
71
- m := map [string ]posMapper {}
78
+ m := adjustMap {m : map [string ]posMapper {}}
79
+
72
80
startedAt := time .Now ()
73
81
var wg sync.WaitGroup
74
82
wg .Add (len (pkgs ))
75
83
for _ , pkg := range pkgs {
76
84
go func (pkg * packages.Package ) {
77
85
// It's important to call func here to run GC
78
- processUnadjusterPkg (m , pkg , log )
86
+ processUnadjusterPkg (& m , pkg , log )
79
87
wg .Done ()
80
88
}(pkg )
81
89
}
82
90
wg .Wait ()
83
- log .Infof ("Pre-built %d adjustments in %s" , len (m ), time .Since (startedAt ))
91
+ log .Infof ("Pre-built %d adjustments in %s" , len (m . m ), time .Since (startedAt ))
84
92
85
93
return & FilenameUnadjuster {
86
- m : m ,
94
+ m : m . m ,
87
95
log : log ,
88
96
loggedUnadjustments : map [string ]bool {},
89
97
}
0 commit comments