@@ -449,9 +449,12 @@ type Package struct {
449
449
// //go:embed a* b.c
450
450
// then the list will contain those two strings as separate entries.
451
451
// (See package embed for more details about //go:embed.)
452
- EmbedPatterns []string // patterns from GoFiles, CgoFiles
453
- TestEmbedPatterns []string // patterns from TestGoFiles
454
- XTestEmbedPatterns []string // patterns from XTestGoFiles
452
+ EmbedPatterns []string // patterns from GoFiles, CgoFiles
453
+ EmbedPatternPos map [string ][]token.Position // line information for EmbedPatterns
454
+ TestEmbedPatterns []string // patterns from TestGoFiles
455
+ TestEmbedPatternPos map [string ][]token.Position // line information for TestEmbedPatterns
456
+ XTestEmbedPatterns []string // patterns from XTestGoFiles
457
+ XTestEmbedPatternPos map [string ][]token.Position // line information for XTestEmbedPatternPos
455
458
}
456
459
457
460
// IsCommand reports whether the package is considered a
@@ -794,10 +797,12 @@ Found:
794
797
var badGoError error
795
798
var Sfiles []string // files with ".S"(capital S)/.sx(capital s equivalent for case insensitive filesystems)
796
799
var firstFile , firstCommentFile string
797
- var embeds , testEmbeds , xTestEmbeds []string
798
- imported := make (map [string ][]token.Position )
799
- testImported := make (map [string ][]token.Position )
800
- xTestImported := make (map [string ][]token.Position )
800
+ embedPos := make (map [string ][]token.Position )
801
+ testEmbedPos := make (map [string ][]token.Position )
802
+ xTestEmbedPos := make (map [string ][]token.Position )
803
+ importPos := make (map [string ][]token.Position )
804
+ testImportPos := make (map [string ][]token.Position )
805
+ xTestImportPos := make (map [string ][]token.Position )
801
806
allTags := make (map [string ]bool )
802
807
fset := token .NewFileSet ()
803
808
for _ , d := range dirs {
@@ -920,40 +925,42 @@ Found:
920
925
}
921
926
}
922
927
923
- var fileList , embedList * []string
924
- var importMap map [string ][]token.Position
928
+ var fileList * []string
929
+ var importMap , embedMap map [string ][]token.Position
925
930
switch {
926
931
case isCgo :
927
932
allTags ["cgo" ] = true
928
933
if ctxt .CgoEnabled {
929
934
fileList = & p .CgoFiles
930
- importMap = imported
931
- embedList = & embeds
935
+ importMap = importPos
936
+ embedMap = embedPos
932
937
} else {
933
- // Ignore imports from cgo files if cgo is disabled.
938
+ // Ignore imports and embeds from cgo files if cgo is disabled.
934
939
fileList = & p .IgnoredGoFiles
935
940
}
936
941
case isXTest :
937
942
fileList = & p .XTestGoFiles
938
- importMap = xTestImported
939
- embedList = & xTestEmbeds
943
+ importMap = xTestImportPos
944
+ embedMap = xTestEmbedPos
940
945
case isTest :
941
946
fileList = & p .TestGoFiles
942
- importMap = testImported
943
- embedList = & testEmbeds
947
+ importMap = testImportPos
948
+ embedMap = testEmbedPos
944
949
default :
945
950
fileList = & p .GoFiles
946
- importMap = imported
947
- embedList = & embeds
951
+ importMap = importPos
952
+ embedMap = embedPos
948
953
}
949
954
* fileList = append (* fileList , name )
950
955
if importMap != nil {
951
956
for _ , imp := range info .imports {
952
957
importMap [imp .path ] = append (importMap [imp .path ], fset .Position (imp .pos ))
953
958
}
954
959
}
955
- if embedList != nil {
956
- * embedList = append (* embedList , info .embeds ... )
960
+ if embedMap != nil {
961
+ for _ , emb := range info .embeds {
962
+ embedMap [emb .pattern ] = append (embedMap [emb .pattern ], emb .pos )
963
+ }
957
964
}
958
965
}
959
966
@@ -962,13 +969,13 @@ Found:
962
969
}
963
970
sort .Strings (p .AllTags )
964
971
965
- p .EmbedPatterns = uniq ( embeds )
966
- p .TestEmbedPatterns = uniq ( testEmbeds )
967
- p .XTestEmbedPatterns = uniq ( xTestEmbeds )
972
+ p .EmbedPatterns , p . EmbedPatternPos = cleanDecls ( embedPos )
973
+ p .TestEmbedPatterns , p . TestEmbedPatternPos = cleanDecls ( testEmbedPos )
974
+ p .XTestEmbedPatterns , p . XTestEmbedPatternPos = cleanDecls ( xTestEmbedPos )
968
975
969
- p .Imports , p .ImportPos = cleanImports ( imported )
970
- p .TestImports , p .TestImportPos = cleanImports ( testImported )
971
- p .XTestImports , p .XTestImportPos = cleanImports ( xTestImported )
976
+ p .Imports , p .ImportPos = cleanDecls ( importPos )
977
+ p .TestImports , p .TestImportPos = cleanDecls ( testImportPos )
978
+ p .XTestImports , p .XTestImportPos = cleanDecls ( xTestImportPos )
972
979
973
980
// add the .S/.sx files only if we are using cgo
974
981
// (which means gcc will compile them).
@@ -1340,7 +1347,7 @@ type fileInfo struct {
1340
1347
parsed * ast.File
1341
1348
parseErr error
1342
1349
imports []fileImport
1343
- embeds []string
1350
+ embeds []fileEmbed
1344
1351
embedErr error
1345
1352
}
1346
1353
@@ -1350,6 +1357,11 @@ type fileImport struct {
1350
1357
doc * ast.CommentGroup
1351
1358
}
1352
1359
1360
+ type fileEmbed struct {
1361
+ pattern string
1362
+ pos token.Position
1363
+ }
1364
+
1353
1365
// matchFile determines whether the file with the given name in the given directory
1354
1366
// should be included in the package being constructed.
1355
1367
// If the file should be included, matchFile returns a non-nil *fileInfo (and a nil error).
@@ -1424,7 +1436,7 @@ func (ctxt *Context) matchFile(dir, name string, allTags map[string]bool, binary
1424
1436
return info , nil
1425
1437
}
1426
1438
1427
- func cleanImports (m map [string ][]token.Position ) ([]string , map [string ][]token.Position ) {
1439
+ func cleanDecls (m map [string ][]token.Position ) ([]string , map [string ][]token.Position ) {
1428
1440
all := make ([]string , 0 , len (m ))
1429
1441
for path := range m {
1430
1442
all = append (all , path )
0 commit comments