mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 12:03:41 +00:00
cgo: define idents referenced only from macros
This commit is contained in:
committed by
Ron Evans
parent
8068419854
commit
c4867c8743
+2
-2
@@ -1148,7 +1148,7 @@ func (f *cgoFile) getASTDeclName(name string, found clangCursor, iscall bool) st
|
||||
if alias := cgoAliases["C."+name]; alias != "" {
|
||||
return alias
|
||||
}
|
||||
node := f.getASTDeclNode(name, found, iscall)
|
||||
node := f.getASTDeclNode(name, found)
|
||||
if node, ok := node.(*ast.FuncDecl); ok {
|
||||
if !iscall {
|
||||
return node.Name.Name + "$funcaddr"
|
||||
@@ -1160,7 +1160,7 @@ func (f *cgoFile) getASTDeclName(name string, found clangCursor, iscall bool) st
|
||||
|
||||
// getASTDeclNode will declare the given C AST node (if not already defined) and
|
||||
// returns it.
|
||||
func (f *cgoFile) getASTDeclNode(name string, found clangCursor, iscall bool) ast.Node {
|
||||
func (f *cgoFile) getASTDeclNode(name string, found clangCursor) ast.Node {
|
||||
if node, ok := f.defined[name]; ok {
|
||||
// Declaration was found in the current file, so return it immediately.
|
||||
return node
|
||||
|
||||
+19
-3
@@ -54,8 +54,8 @@ func init() {
|
||||
}
|
||||
|
||||
// parseConst parses the given string as a C constant.
|
||||
func parseConst(pos token.Pos, fset *token.FileSet, value string) (ast.Expr, *scanner.Error) {
|
||||
t := newTokenizer(pos, fset, value)
|
||||
func parseConst(pos token.Pos, fset *token.FileSet, value string, f *cgoFile) (ast.Expr, *scanner.Error) {
|
||||
t := newTokenizer(pos, fset, value, f)
|
||||
expr, err := parseConstExpr(t, precedenceLowest)
|
||||
t.Next()
|
||||
if t.curToken != token.EOF {
|
||||
@@ -96,6 +96,20 @@ func parseConstExpr(t *tokenizer, precedence int) (ast.Expr, *scanner.Error) {
|
||||
}
|
||||
|
||||
func parseIdent(t *tokenizer) (ast.Expr, *scanner.Error) {
|
||||
// Normally the name is something defined in the file (like another macro)
|
||||
// which we get the declaration from using getASTDeclName.
|
||||
// This ensures that names that are only referenced inside a macro are still
|
||||
// getting defined.
|
||||
if t.f != nil {
|
||||
if cursor, ok := t.f.names[t.curValue]; ok {
|
||||
return &ast.Ident{
|
||||
NamePos: t.curPos,
|
||||
Name: t.f.getASTDeclName(t.curValue, cursor, false),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// t.f is nil during testing. This is a fallback.
|
||||
return &ast.Ident{
|
||||
NamePos: t.curPos,
|
||||
Name: "C." + t.curValue,
|
||||
@@ -164,6 +178,7 @@ func unexpectedToken(t *tokenizer, expected token.Token) *scanner.Error {
|
||||
|
||||
// tokenizer reads C source code and converts it to Go tokens.
|
||||
type tokenizer struct {
|
||||
f *cgoFile
|
||||
curPos, peekPos token.Pos
|
||||
fset *token.FileSet
|
||||
curToken, peekToken token.Token
|
||||
@@ -173,8 +188,9 @@ type tokenizer struct {
|
||||
|
||||
// newTokenizer initializes a new tokenizer, positioned at the first token in
|
||||
// the string.
|
||||
func newTokenizer(start token.Pos, fset *token.FileSet, buf string) *tokenizer {
|
||||
func newTokenizer(start token.Pos, fset *token.FileSet, buf string, f *cgoFile) *tokenizer {
|
||||
t := &tokenizer{
|
||||
f: f,
|
||||
peekPos: start,
|
||||
fset: fset,
|
||||
buf: buf,
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ func TestParseConst(t *testing.T) {
|
||||
} {
|
||||
fset := token.NewFileSet()
|
||||
startPos := fset.AddFile("", -1, 1000).Pos(0)
|
||||
expr, err := parseConst(startPos, fset, tc.C)
|
||||
expr, err := parseConst(startPos, fset, tc.C, nil)
|
||||
s := "<invalid>"
|
||||
if err != nil {
|
||||
if !strings.HasPrefix(tc.Go, "error: ") {
|
||||
|
||||
+1
-1
@@ -408,7 +408,7 @@ func (f *cgoFile) createASTNode(name string, c clangCursor) (ast.Node, any) {
|
||||
if pos != token.NoPos {
|
||||
tokenPos = pos + token.Pos(len(name))
|
||||
}
|
||||
expr, scannerError := parseConst(tokenPos, f.fset, value)
|
||||
expr, scannerError := parseConst(tokenPos, f.fset, value, f)
|
||||
if scannerError != nil {
|
||||
f.errors = append(f.errors, *scannerError)
|
||||
return nil, nil
|
||||
|
||||
Vendored
+3
@@ -3,10 +3,13 @@ package main
|
||||
/*
|
||||
#define foo 3
|
||||
#define bar foo
|
||||
#define unreferenced 4
|
||||
#define referenced unreferenced
|
||||
*/
|
||||
import "C"
|
||||
|
||||
const (
|
||||
Foo = C.foo
|
||||
Bar = C.bar
|
||||
Baz = C.referenced
|
||||
)
|
||||
|
||||
Vendored
+2
@@ -47,3 +47,5 @@ type (
|
||||
|
||||
const C.foo = 3
|
||||
const C.bar = C.foo
|
||||
const C.unreferenced = 4
|
||||
const C.referenced = C.unreferenced
|
||||
|
||||
Reference in New Issue
Block a user