@@ -5873,27 +5873,38 @@ final class CParser(AST) : Parser!AST
5873
5873
5874
5874
const (char )* endp = &slice[length - 7 ];
5875
5875
5876
+ AST .Dsymbols newSymbols;
5877
+
5876
5878
size_t [void * ] defineTab; // hash table of #define's turned into Symbol's
5877
- // indexed by Identifier, returns index into symbols []
5879
+ // indexed by Identifier, returns index into newSymbols []
5878
5880
// The memory for this is leaked
5879
5881
5880
- void addVar (AST .Dsymbol s)
5882
+ void addSym (AST .Dsymbol s)
5881
5883
{
5882
- // printf("addVar () %s\n", s.toChars());
5884
+ // printf("addSym () %s\n", s.toChars());
5883
5885
if (auto v = s.isVarDeclaration())
5884
5886
v.isCmacro(true ); // mark it as coming from a C #define
5885
5887
/* If it's already defined, replace the earlier
5886
5888
* definition
5887
5889
*/
5888
5890
if (size_t * pd = cast (void * )s.ident in defineTab)
5889
5891
{
5890
- // printf("replacing %s\n", v .toChars());
5891
- ( * symbols) [* pd] = s;
5892
+ // printf("replacing %s\n", s .toChars());
5893
+ newSymbols [* pd] = s;
5892
5894
return ;
5893
5895
}
5894
- assert (symbols, " symbols is null" );
5895
- defineTab[cast (void * )s.ident] = symbols.length;
5896
- symbols.push(s);
5896
+ defineTab[cast (void * )s.ident] = newSymbols.length;
5897
+ newSymbols.push(s);
5898
+ }
5899
+
5900
+ void removeSym (Identifier ident)
5901
+ {
5902
+ // printf("removeSym() %s\n", ident.toChars());
5903
+ if (size_t * pd = cast (void * )ident in defineTab)
5904
+ {
5905
+ // printf("removing %s\n", ident.toChars());
5906
+ newSymbols[* pd] = null ;
5907
+ }
5897
5908
}
5898
5909
5899
5910
while (p < endp)
@@ -5937,7 +5948,7 @@ final class CParser(AST) : Parser!AST
5937
5948
*/
5938
5949
AST .Expression e = new AST .IntegerExp(scanloc, intvalue, t);
5939
5950
auto v = new AST .VarDeclaration(scanloc, t, id, new AST .ExpInitializer(scanloc, e), STC .manifest);
5940
- addVar (v);
5951
+ addSym (v);
5941
5952
++ p;
5942
5953
continue ;
5943
5954
}
@@ -5960,7 +5971,7 @@ final class CParser(AST) : Parser!AST
5960
5971
*/
5961
5972
AST .Expression e = new AST .RealExp(scanloc, floatvalue, t);
5962
5973
auto v = new AST .VarDeclaration(scanloc, t, id, new AST .ExpInitializer(scanloc, e), STC .manifest);
5963
- addVar (v);
5974
+ addSym (v);
5964
5975
++ p;
5965
5976
continue ;
5966
5977
}
@@ -5978,7 +5989,7 @@ final class CParser(AST) : Parser!AST
5978
5989
*/
5979
5990
AST .Expression e = new AST .StringExp(scanloc, str[0 .. len], len, 1 , postfix);
5980
5991
auto v = new AST .VarDeclaration(scanloc, null , id, new AST .ExpInitializer(scanloc, e), STC .manifest);
5981
- addVar (v);
5992
+ addSym (v);
5982
5993
++ p;
5983
5994
continue ;
5984
5995
}
@@ -6014,7 +6025,7 @@ final class CParser(AST) : Parser!AST
6014
6025
AST .TemplateParameters* tpl = new AST .TemplateParameters();
6015
6026
AST .Expression constraint = null ;
6016
6027
auto tempdecl = new AST .TemplateDeclaration(exp.loc, id, tpl, constraint, decldefs, false );
6017
- addVar (tempdecl);
6028
+ addSym (tempdecl);
6018
6029
++ p;
6019
6030
continue ;
6020
6031
}
@@ -6105,7 +6116,7 @@ final class CParser(AST) : Parser!AST
6105
6116
AST .Dsymbols* decldefs = new AST .Dsymbols();
6106
6117
decldefs.push(fd);
6107
6118
auto tempdecl = new AST .TemplateDeclaration(exp.loc, id, tpl, null , decldefs, false );
6108
- addVar (tempdecl);
6119
+ addSym (tempdecl);
6109
6120
6110
6121
++ p;
6111
6122
continue ;
@@ -6116,13 +6127,31 @@ final class CParser(AST) : Parser!AST
6116
6127
}
6117
6128
}
6118
6129
}
6130
+ else if (p[0 .. 6 ] == " #undef" )
6131
+ {
6132
+ p += 6 ;
6133
+ nextToken();
6134
+ // printf("undef %s\n", token.toChars());
6135
+ if (token.value == TOK .identifier)
6136
+ removeSym(token.ident);
6137
+ }
6119
6138
// scan to end of line
6120
6139
while (* p)
6121
6140
++ p;
6122
6141
++ p; // advance to start of next line
6123
6142
scanloc.linnum = scanloc.linnum + 1 ;
6124
6143
}
6125
6144
6145
+ if (newSymbols.length)
6146
+ {
6147
+ assert (symbols, " symbols is null" );
6148
+ symbols.reserve (newSymbols.length);
6149
+
6150
+ foreach (sym; newSymbols)
6151
+ if (sym) // undefined entries are null
6152
+ symbols.push(sym);
6153
+ }
6154
+
6126
6155
scanloc = scanlocSave;
6127
6156
eSink = save;
6128
6157
defines = buf;
0 commit comments