Skip to content

Commit 0f4eb14

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 0f4eb14

File tree

5 files changed

+81
-39
lines changed

5 files changed

+81
-39
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, ".")
Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
1-
This test validates the References request functionality in Go assembly files.
21

3-
It ensures that references to both exported (`Add`) and unexported (`sub`) functions are correctly identified across Go and assembly files. The test covers:
4-
- Locating the definition of functions in both Go and assembly files.
5-
- Identifying all references to the functions (`Add` and `sub`) within the Go and assembly files.
6-
7-
The test includes:
8-
- `Add`: An exported function with references in both Go and assembly files.
9-
- `sub`: An unexported function with references in both Go and assembly files, including a usage in Go code (`var _ = sub`).
10-
11-
The assembly file demonstrates portable assembly syntax and verifies cross-file reference handling.
122

133
-- go.mod --
144
module example.com
155
go 1.24
166

17-
-- foo/foo.go --
18-
package foo
19-
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)
7+
-- example/label_example.s --
8+
TEXT ·JumpDemo(SB), $0-0
9+
label1: //@loc(defLabel, "label1")
10+
JMP label1 //@loc(refLabel, "label1")
2311

24-
-- foo/foo.s --
25-
// portable assembly
26-
#include "textflag.h"
27-
28-
TEXT ·Add(SB), NOSPLIT, $0-24 //@ loc(def, "·Add"), refs("Add", def, use)
29-
MOVQ a+0(FP), AX
30-
ADDQ b+8(FP), AX
12+
-- example/in_package_ref.s --
13+
TEXT ·Foo(SB), $0-8 //@loc(defFooAsm, "·Foo")
3114
RET
3215

33-
TEXT ·sub(SB), NOSPLIT, $0-24 //@ loc(defSub, "·sub"), refs("sub", defSub, useSub, refSub)
34-
MOVQ a+0(FP), AX
35-
SUBQ b+8(FP), AX
16+
TEXT ·Bar(SB), $0-8
17+
CALL ·Foo(SB) //@loc(callFooAsm, "·Foo")
3618
RET
3719

38-
TEXT ·AddAndSub(SB), NOSPLIT, $0-24
39-
CALL ·Add(SB)
40-
CALL ·sub(SB)
20+
-- example/example_data.s --
21+
DATA ·myGlobal+0(SB)/8, $42
22+
GLOBL ·myGlobal(SB), NOPTR, 8
23+
24+
TEXT ·UseGlobal(SB), $0-0
25+
MOVQ ·myGlobal(SB), AX //@loc(refMyGlobal, "·myGlobal")
4126
RET
27+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
This test validates the References request functionality in Go assembly files.
2+
3+
It ensures that references to both exported (`Add`) and unexported (`sub`) functions are correctly identified across Go and assembly files. The test covers:
4+
- Locating the definition of functions in both Go and assembly files.
5+
- Identifying all references to the functions (`Add` and `sub`) within the Go and assembly files.
6+
7+
The test includes:
8+
- `Add`: An exported function with references in both Go and assembly files.
9+
- `sub`: An unexported function with references in both Go and assembly files, including a usage in Go code (`var _ = sub`).
10+
11+
The assembly file demonstrates portable assembly syntax and verifies cross-file reference handling.
12+
13+
-- go.mod --
14+
module example.com
15+
go 1.24
16+
17+
-- go_call_asm/example_func.go --
18+
package go_call_asm
19+
20+
func Add(a, b int) int //@loc(defAddGo, "Add"), refs("Add", defAddGo, callAddGo)
21+
func UseAdd() int {
22+
return Add(1, 2) //@loc(callAddGo, "Add")
23+
}
24+
var myGlobal int64 //@loc(defMyGlobalGo, "myGlobal"), refs("myGlobal", defMyGlobalGo, refMyGlobal, refMyGlobalGo)
25+
var _ = myGlobal //@loc(refMyGlobalGo, "myGlobal")
26+
27+
-- go_call_asm/example_asm.s --
28+
TEXT ·Add(SB), $0-24 //@loc(defAddAsm, "·Add"), refs("Add", defAddGo, callAddGo)
29+
MOVQ a+0(FP), AX
30+
ADDQ b+8(FP), AX
31+
RET
32+
33+
DATA ·myGlobal+0(SB)/8, $42
34+
GLOBL ·myGlobal(SB), NOPTR, 8
35+
TEXT ·UseGlobal(SB), $0-0
36+
MOVQ ·myGlobal(SB), AX //@loc(refMyGlobal, "·myGlobal")
37+
RET
38+
39+
-- asm_call_go/example_sub.go --
40+
package asm_call_go
41+
42+
func Sub(a, b int) int { return a - b } //@loc(defSubGo, "Sub")
43+
44+
-- asm_call_go/call_sub.s --
45+
TEXT ·CallSub(SB), $0-16
46+
MOVQ $10, AX
47+
MOVQ $3, BX
48+
MOVQ AX, a+0(FP)
49+
MOVQ BX, b+8(FP)
50+
CALL ·Sub(SB) //@loc(callSubAsm, "·Sub")
51+
RET

0 commit comments

Comments
 (0)