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