@@ -54,8 +54,8 @@ func init() {
5454}
5555
5656// parseConst parses the given string as a C constant.
57- func parseConst (pos token.Pos , fset * token.FileSet , value string ) (ast.Expr , * scanner.Error ) {
58- t := newTokenizer (pos , fset , value )
57+ func parseConst (pos token.Pos , fset * token.FileSet , value string , f * cgoFile ) (ast.Expr , * scanner.Error ) {
58+ t := newTokenizer (pos , fset , value , f )
5959 expr , err := parseConstExpr (t , precedenceLowest )
6060 t .Next ()
6161 if t .curToken != token .EOF {
@@ -96,6 +96,20 @@ func parseConstExpr(t *tokenizer, precedence int) (ast.Expr, *scanner.Error) {
9696}
9797
9898func parseIdent (t * tokenizer ) (ast.Expr , * scanner.Error ) {
99+ // Normally the name is something defined in the file (like another macro)
100+ // which we get the declaration from using getASTDeclName.
101+ // This ensures that names that are only referenced inside a macro are still
102+ // getting defined.
103+ if t .f != nil {
104+ if cursor , ok := t .f .names [t .curValue ]; ok {
105+ return & ast.Ident {
106+ NamePos : t .curPos ,
107+ Name : t .f .getASTDeclName (t .curValue , cursor , false ),
108+ }, nil
109+ }
110+ }
111+
112+ // t.f is nil during testing. This is a fallback.
99113 return & ast.Ident {
100114 NamePos : t .curPos ,
101115 Name : "C." + t .curValue ,
@@ -164,6 +178,7 @@ func unexpectedToken(t *tokenizer, expected token.Token) *scanner.Error {
164178
165179// tokenizer reads C source code and converts it to Go tokens.
166180type tokenizer struct {
181+ f * cgoFile
167182 curPos , peekPos token.Pos
168183 fset * token.FileSet
169184 curToken , peekToken token.Token
@@ -173,8 +188,9 @@ type tokenizer struct {
173188
174189// newTokenizer initializes a new tokenizer, positioned at the first token in
175190// the string.
176- func newTokenizer (start token.Pos , fset * token.FileSet , buf string ) * tokenizer {
191+ func newTokenizer (start token.Pos , fset * token.FileSet , buf string , f * cgoFile ) * tokenizer {
177192 t := & tokenizer {
193+ f : f ,
178194 peekPos : start ,
179195 fset : fset ,
180196 buf : buf ,
0 commit comments