Debian Codegraph @

gopls/internal/util/asm/parse_test.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package asm_test
6
7 import (
8 "bytes"
9 "fmt"
10 "testing"
11
12 "github.com/google/go-cmp/cmp"
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/gopls/internal/util/asm"
15 )
16
17 // TestIdents checks that (likely) identifiers are extracted in the expected places.
18 func TestIdents(t *testing.T) {
19 src := []byte(`
20 // This is a nonsense file containing a variety of syntax.
21
22 #include "foo.h"
23 #ifdef MACRO
24 DATA hello<>+0x00(SB)/64, $"Hello"
25 GLOBL hello<(SB), RODATA, $64
26 #endif
27
28 TEXT mypkg·f(SB),NOSPLIT,$0
29 MOVD R1, 16(RSP) // another comment
30 MOVD $otherpkg·data(SB), R2
31 JMP label
32 label:
33 BL ·g(SB)
34
35 TEXT ·g(SB),NOSPLIT,$0
36 MOVD $runtime·g0(SB), g
37 MOVD R0, g_stackguard0(g)
38 MOVD R0, (g_stack+stack_lo)(g)
39 `[1:])
40 const filename = "asm.s"
41 m := protocol.NewMapper(protocol.URIFromPath(filename), src)
42 file := asm.Parse(src)
43
44 want := `
45 asm.s:5:6-11: data "hello"
46 asm.s:6:7-12: global "hello"
47 asm.s:9:6-13: text "mypkg.f"
48 asm.s:11:8-21: ref "otherpkg.data"
49 asm.s:12:6-11: ref "label"
50 asm.s:13:1-6: label "label"
51 asm.s:14:5-7: ref ".g"
52 asm.s:16:6-8: text ".g"
53 asm.s:17:8-18: ref "runtime.g0"
54 asm.s:17:25-26: ref "g"
55 asm.s:18:11-24: ref "g_stackguard0"
56 `[1:]
57 var buf bytes.Buffer
58 for _, id := range file.Idents {
59 line, col := m.OffsetLineCol8(id.Offset)
60 _, endCol := m.OffsetLineCol8(id.Offset + len(id.Name))
61 fmt.Fprintf(&buf, "%s:%d:%d-%d:\t%s %q\n", filename, line, col, endCol, id.Kind, id.Name)
62 }
63 got := buf.String()
64 if got != want {
65 t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, want, cmp.Diff(want, got))
66 }
67 }