From 8f15fd45f2cbe0218cc88dcf10bf4b75306f8569 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 1 May 2024 20:33:31 +0800 Subject: [PATCH] initLinkname: support var --- cl/builtin_test.go | 2 +- cl/import.go | 39 ++++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cl/builtin_test.go b/cl/builtin_test.go index a3fa722a..66d0e839 100644 --- a/cl/builtin_test.go +++ b/cl/builtin_test.go @@ -122,7 +122,7 @@ func TestErrInitLinkname(t *testing.T) { } }() var ctx context - ctx.initLinkname("foo", "//go:linkname Printf printf") + ctx.initLinkname("foo", "//go:linkname Printf printf", false) } func TestErrVarOf(t *testing.T) { diff --git a/cl/import.go b/cl/import.go index a2a380f7..fbb06e49 100644 --- a/cl/import.go +++ b/cl/import.go @@ -90,19 +90,14 @@ func (p *context) importPkg(pkg *types.Package, i *pkgInfo) { for _, name := range names { if token.IsExported(name) { obj := scope.Lookup(name) - if obj, ok := obj.(*types.Func); ok { + switch obj := obj.(type) { + case *types.Func: if pos := obj.Pos(); pos != token.NoPos { - f := fset.File(pos) - if fp := f.Position(pos); fp.Line > 2 { - lines, err := contentOf(contents, fp.Filename) - if err != nil { - panic(err) - } - if i := fp.Line - 2; i < len(lines) { - line := string(lines[i]) - p.initLinkname(pkgPath, line) - } - } + p.initLinknameByPos(fset, pos, pkgPath, contents, false) + } + case *types.Var: + if pos := obj.Pos(); pos != token.NoPos { + p.initLinknameByPos(fset, pos, pkgPath, contents, true) } } } @@ -117,7 +112,7 @@ func (p *context) initFiles(pkgPath string, files []*ast.File) { if doc := decl.Doc; doc != nil { if n := len(doc.List); n > 0 { line := doc.List[n-1].Text - p.initLinkname(pkgPath, line) + p.initLinkname(pkgPath, line, false) } } } @@ -126,7 +121,21 @@ func (p *context) initFiles(pkgPath string, files []*ast.File) { } } -func (p *context) initLinkname(pkgPath, line string) { +func (p *context) initLinknameByPos(fset *token.FileSet, pos token.Pos, pkgPath string, contents contentMap, isVar bool) { + f := fset.File(pos) + if fp := f.Position(pos); fp.Line > 2 { + lines, err := contentOf(contents, fp.Filename) + if err != nil { + panic(err) + } + if i := fp.Line - 2; i < len(lines) { + line := string(lines[i]) + p.initLinkname(pkgPath, line, isVar) + } + } +} + +func (p *context) initLinkname(pkgPath, line string, isVar bool) { const ( linkname = "//go:linkname " ) @@ -134,7 +143,7 @@ func (p *context) initLinkname(pkgPath, line string) { text := strings.TrimSpace(line[len(linkname):]) if idx := strings.IndexByte(text, ' '); idx > 0 { link := strings.TrimLeft(text[idx+1:], " ") - if strings.Contains(link, ".") { // eg. C.printf, C.strlen, llgo.cstr + if isVar || strings.Contains(link, ".") { // eg. C.printf, C.strlen, llgo.cstr name := pkgPath + "." + text[:idx] p.link[name] = link } else {