Skip to content

Commit 9d2a65a

Browse files
committed
gopls: Update the condition for assembly reference checks and add test cases.
Update the condition for identifying assembly files to allow only ref and data types. Remove Go definition references from the goasm reference process. Add test files for different types of reference relationships between asm and Go. Updates golang/go#71754
1 parent b302501 commit 9d2a65a

File tree

10 files changed

+127
-20
lines changed

10 files changed

+127
-20
lines changed

gopls/internal/cache/xrefs/xrefs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info, asmFiles
117117
// For each asm file, record references to identifiers.
118118
for fileIndex, af := range asmFiles {
119119
for _, id := range af.Idents {
120-
if id.Kind != asm.Data && id.Kind != asm.Text {
120+
if id.Kind != asm.Data && id.Kind != asm.Ref {
121121
continue
122122
}
123123
_, name, ok := morestrings.CutLast(id.Name, ".")

gopls/internal/goasm/references.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
8080
for _, pgf := range pkg.CompiledGoFiles() {
8181
for curId := range pgf.Cursor.Preorder((*ast.Ident)(nil)) {
8282
id := curId.Node().(*ast.Ident)
83+
if !includeDeclaration && pkg.TypesInfo().Defs[id] != nil {
84+
continue
85+
}
8386
if !matches(pkg.TypesInfo().ObjectOf(id)) {
8487
continue
8588
}
@@ -91,17 +94,19 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
9194
}
9295
}
9396

94-
if includeDeclaration {
95-
for _, asmFile := range pkg.AsmFiles() {
96-
for _, id := range asmFile.Idents {
97-
if id.Name == found.Name &&
98-
(id.Kind == asm.Text || id.Kind == asm.Global || id.Kind == asm.Label) {
99-
if loc, err := asmFile.NodeLocation(id); err == nil {
100-
locations = append(locations, loc)
101-
}
97+
for _, asmFile := range pkg.AsmFiles() {
98+
for _, id := range asmFile.Idents {
99+
if id.Name == found.Name &&
100+
(id.Kind == asm.Data || id.Kind == asm.Ref) {
101+
if id.Kind == asm.Data && !includeDeclaration {
102+
continue
103+
}
104+
if loc, err := asmFile.NodeLocation(id); err == nil {
105+
locations = append(locations, loc)
102106
}
103107
}
104108
}
105109
}
110+
106111
return locations, nil
107112
}

gopls/internal/golang/references.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ func localReferences(pkg *cache.Package, targets map[types.Object]bool, correspo
615615
// Iterate over all assembly files and find all references to the target object.
616616
for _, pgf := range pkg.AsmFiles() {
617617
for _, id := range pgf.Idents {
618-
if id.Kind != asm.Data && id.Kind != asm.Text {
618+
if id.Kind != asm.Data && id.Kind != asm.Ref {
619619
continue
620620
}
621621
_, name, ok := morestrings.CutLast(id.Name, ".")

gopls/internal/test/marker/testdata/references/asm.txt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,23 @@ go 1.24
1717
-- foo/foo.go --
1818
package foo
1919

20-
func Add(a, b int) int //@ loc(use, "Add"), refs("Add", use, def)
21-
func sub(a, b int) int //@ loc(useSub, "sub"), refs("sub", useSub, defSub, refSub)
22-
var _ = sub //@loc(refSub, "sub"), refs("sub", useSub, defSub, refSub)
20+
func Add(a, b int) int //@ loc(defAdd, "Add"), refs("Add", defAdd, callAdd)
21+
func sub(a, b int) int //@ loc(defSub, "sub"), refs("sub", defSub, refSub, callSub)
22+
var _ = sub //@loc(refSub, "sub")
2323

2424
-- foo/foo.s --
25-
// portable assembly
26-
#include "textflag.h"
2725

28-
TEXT ·Add(SB), NOSPLIT, $0-24 //@ loc(def, "·Add"), refs("Add", def, use)
26+
TEXT ·Add(SB), $0-24 //@ loc(addAsm, "·Add"), refs("Add", defAdd, callAdd)
2927
MOVQ a+0(FP), AX
3028
ADDQ b+8(FP), AX
3129
RET
3230

33-
TEXT ·sub(SB), NOSPLIT, $0-24 //@ loc(defSub, "·sub"), refs("sub", defSub, useSub, refSub)
31+
TEXT ·sub(SB), $0-24 //@ loc(subAsm, "·sub"), refs("sub", defSub, refSub, callSub)
3432
MOVQ a+0(FP), AX
3533
SUBQ b+8(FP), AX
3634
RET
3735

38-
TEXT ·AddAndSub(SB), NOSPLIT, $0-24
39-
CALL ·Add(SB)
40-
CALL ·sub(SB)
36+
TEXT ·AddAndSub(SB), $0-24
37+
CALL ·Add(SB) //@ loc(callAdd, "·Add")
38+
CALL ·sub(SB) //@ loc(callSub, "·sub")
4139
RET
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
-- go.mod --
3+
module example.com
4+
go 1.24
5+
6+
-- foo/foo.go --
7+
package foo
8+
9+
var myGlobal int64 //@loc(defMyGlobalGo, "myGlobal")
10+
11+
-- foo/foo.s --
12+
DATA ·myGlobal+0(SB)/8, $42
13+
GLOBL ·myGlobal(SB), NOPTR, 8
14+
15+
TEXT ·UseGlobal(SB), $0-0
16+
MOVQ ·myGlobal(SB), AX //@loc(refMyGlobal, "·myGlobal")
17+
RET
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
-- go.mod --
4+
module example.com
5+
go 1.24
6+
7+
-- foo/foo.s --
8+
TEXT ·JumpDemo(SB), $0-0
9+
label1: //@loc(defLabel, "label1")
10+
JMP label1 //@loc(refLabel, "label1")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
-- go.mod --
4+
module example.com
5+
go 1.24
6+
7+
-- foo/foo.go --
8+
package foo
9+
10+
func Add(a, b int) int //@loc(defAddGo, "Add"), refs("Add", defAddGo, callAddGo)
11+
func UseAdd() int {
12+
return Add(1, 2) //@loc(callAddGo, "Add")
13+
}
14+
var myGlobal int64 //@loc(defMyGlobalGo, "myGlobal")
15+
16+
-- foo/foo.s --
17+
TEXT ·Add(SB), $0-24 //@loc(defAddAsm, "·Add"), refs("Add", defAddGo, callAddGo)
18+
MOVQ a+0(FP), AX
19+
ADDQ b+8(FP), AX
20+
RET
21+
22+
DATA ·myGlobal+0(SB)/8, $42
23+
GLOBL ·myGlobal(SB), NOPTR, 8
24+
TEXT ·UseGlobal(SB), $0-0
25+
MOVQ ·myGlobal(SB), AX //@loc(refMyGlobal, "·myGlobal")
26+
RET
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
-- go.mod --
3+
module example.com
4+
go 1.24
5+
6+
-- foo/foo.s --
7+
TEXT ·Foo(SB), $0-8 //@loc(defFooAsm, "·Foo")
8+
RET
9+
10+
TEXT ·Bar(SB), $0-8
11+
CALL ·Foo(SB) //@loc(callFooAsm, "·Foo")
12+
RET
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
-- go.mod --
4+
module example.com
5+
go 1.24
6+
7+
-- foo/foo.go --
8+
package foo
9+
10+
func Add(a, b int) int //@loc(defAdd, "Add")
11+
12+
func UseAdd() int {
13+
return Add(1, 2) //@loc(callAddGo, "Add")
14+
}
15+
16+
-- foo/foo.s --
17+
TEXT ·Add(SB), $0-24 //@loc(defAddAsm, "·Add"), refs("Add", defAdd, callAddGo)
18+
MOVQ a+0(FP), AX
19+
ADDQ b+8(FP), AX
20+
RET
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
-- go.mod --
4+
module example.com
5+
go 1.24
6+
7+
-- foo/foo.go --
8+
package foo
9+
10+
func Sub(a, b int) int { return a - b } //@loc(defSubGo, "Sub")
11+
12+
-- foo/foo.s --
13+
TEXT ·CallSub(SB), $0-16
14+
MOVQ $10, AX
15+
MOVQ $3, BX
16+
MOVQ AX, a+0(FP)
17+
MOVQ BX, b+8(FP)
18+
CALL ·Sub(SB) //@loc(callSubAsm, "·Sub")
19+
RET

0 commit comments

Comments
 (0)