mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-20 04:19:04 +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 != "" {
|
if alias := cgoAliases["C."+name]; alias != "" {
|
||||||
return alias
|
return alias
|
||||||
}
|
}
|
||||||
node := f.getASTDeclNode(name, found, iscall)
|
node := f.getASTDeclNode(name, found)
|
||||||
if node, ok := node.(*ast.FuncDecl); ok {
|
if node, ok := node.(*ast.FuncDecl); ok {
|
||||||
if !iscall {
|
if !iscall {
|
||||||
return node.Name.Name + "$funcaddr"
|
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
|
// getASTDeclNode will declare the given C AST node (if not already defined) and
|
||||||
// returns it.
|
// 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 {
|
if node, ok := f.defined[name]; ok {
|
||||||
// Declaration was found in the current file, so return it immediately.
|
// Declaration was found in the current file, so return it immediately.
|
||||||
return node
|
return node
|
||||||
|
|||||||
+19
-3
@@ -54,8 +54,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseConst parses the given string as a C constant.
|
// parseConst parses the given string as a C constant.
|
||||||
func parseConst(pos token.Pos, fset *token.FileSet, value string) (ast.Expr, *scanner.Error) {
|
func parseConst(pos token.Pos, fset *token.FileSet, value string, f *cgoFile) (ast.Expr, *scanner.Error) {
|
||||||
t := newTokenizer(pos, fset, value)
|
t := newTokenizer(pos, fset, value, f)
|
||||||
expr, err := parseConstExpr(t, precedenceLowest)
|
expr, err := parseConstExpr(t, precedenceLowest)
|
||||||
t.Next()
|
t.Next()
|
||||||
if t.curToken != token.EOF {
|
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) {
|
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{
|
return &ast.Ident{
|
||||||
NamePos: t.curPos,
|
NamePos: t.curPos,
|
||||||
Name: "C." + t.curValue,
|
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.
|
// tokenizer reads C source code and converts it to Go tokens.
|
||||||
type tokenizer struct {
|
type tokenizer struct {
|
||||||
|
f *cgoFile
|
||||||
curPos, peekPos token.Pos
|
curPos, peekPos token.Pos
|
||||||
fset *token.FileSet
|
fset *token.FileSet
|
||||||
curToken, peekToken token.Token
|
curToken, peekToken token.Token
|
||||||
@@ -173,8 +188,9 @@ type tokenizer struct {
|
|||||||
|
|
||||||
// newTokenizer initializes a new tokenizer, positioned at the first token in
|
// newTokenizer initializes a new tokenizer, positioned at the first token in
|
||||||
// the string.
|
// 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{
|
t := &tokenizer{
|
||||||
|
f: f,
|
||||||
peekPos: start,
|
peekPos: start,
|
||||||
fset: fset,
|
fset: fset,
|
||||||
buf: buf,
|
buf: buf,
|
||||||
|
|||||||
+1
-1
@@ -59,7 +59,7 @@ func TestParseConst(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
startPos := fset.AddFile("", -1, 1000).Pos(0)
|
startPos := fset.AddFile("", -1, 1000).Pos(0)
|
||||||
expr, err := parseConst(startPos, fset, tc.C)
|
expr, err := parseConst(startPos, fset, tc.C, nil)
|
||||||
s := "<invalid>"
|
s := "<invalid>"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !strings.HasPrefix(tc.Go, "error: ") {
|
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 {
|
if pos != token.NoPos {
|
||||||
tokenPos = pos + token.Pos(len(name))
|
tokenPos = pos + token.Pos(len(name))
|
||||||
}
|
}
|
||||||
expr, scannerError := parseConst(tokenPos, f.fset, value)
|
expr, scannerError := parseConst(tokenPos, f.fset, value, f)
|
||||||
if scannerError != nil {
|
if scannerError != nil {
|
||||||
f.errors = append(f.errors, *scannerError)
|
f.errors = append(f.errors, *scannerError)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
Vendored
+3
@@ -3,10 +3,13 @@ package main
|
|||||||
/*
|
/*
|
||||||
#define foo 3
|
#define foo 3
|
||||||
#define bar foo
|
#define bar foo
|
||||||
|
#define unreferenced 4
|
||||||
|
#define referenced unreferenced
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Foo = C.foo
|
Foo = C.foo
|
||||||
Bar = C.bar
|
Bar = C.bar
|
||||||
|
Baz = C.referenced
|
||||||
)
|
)
|
||||||
|
|||||||
Vendored
+2
@@ -47,3 +47,5 @@ type (
|
|||||||
|
|
||||||
const C.foo = 3
|
const C.foo = 3
|
||||||
const C.bar = C.foo
|
const C.bar = C.foo
|
||||||
|
const C.unreferenced = 4
|
||||||
|
const C.referenced = C.unreferenced
|
||||||
|
|||||||
Reference in New Issue
Block a user