Skip to content

Commit

Permalink
fix Issue 22698 - ImportC: nested struct tag stored in wrong scope
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jan 30, 2022
1 parent da27c59 commit 541c71b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -3221,17 +3221,14 @@ final class CParser(AST) : Parser!AST
if (token.value == TOK.leftCurly)
{
nextToken();
auto symbolsSave = symbols;
symbols = new AST.Dsymbols();
members = new AST.Dsymbols(); // so `members` will be non-null even with 0 members
while (token.value != TOK.rightCurly)
{
cparseStructDeclaration(symbolsSave);
cparseStructDeclaration(members);

if (token.value == TOK.endOfFile)
break;
}
members = symbols; // `members` will be non-null even with 0 members
symbols = symbolsSave;
check(TOK.rightCurly);

if ((*members).length == 0) // C11 6.7.2.1-8
Expand Down Expand Up @@ -3267,19 +3264,18 @@ final class CParser(AST) : Parser!AST
* declarator
* declarator (opt) : constant-expression
* Params:
* tagSymbols = where to put any tag symbols that we encounter
* members = where to put the fields (members)
*/
void cparseStructDeclaration(ref AST.Dsymbols* tagSymbols)
void cparseStructDeclaration(AST.Dsymbols* members)
{
//printf("cparseStructDeclaration()\n");
if (token.value == TOK._Static_assert)
{
auto s = cparseStaticAssert();
symbols.push(s);
members.push(s);
return;
}

auto symbolsSave = symbols;
Specifier specifier;
specifier.packalign = this.packalign;
auto tspec = cparseSpecifierQualifierList(LVL.member, specifier);
Expand Down Expand Up @@ -3308,10 +3304,8 @@ final class CParser(AST) : Parser!AST
* the containing struct
*/
auto ad = new AST.AnonDeclaration(tt.loc, tt.tok == TOK.union_, tt.members);
if (!symbols)
symbols = new AST.Dsymbols();
auto s = applySpecifier(ad, specifier);
symbols.push(s);
members.push(s);
return;
}
if (!tt.id && !tt.members)
Expand All @@ -3325,10 +3319,10 @@ final class CParser(AST) : Parser!AST
? new AST.StructDeclaration(tt.loc, tt.id, false)
: new AST.UnionDeclaration(tt.loc, tt.id);
stag.members = tt.members;
if (!tagSymbols)
tagSymbols = new AST.Dsymbols();
if (!symbols)
symbols = new AST.Dsymbols();
auto s = applySpecifier(stag, specifier);
tagSymbols.push(s);
symbols.push(s);
return;
}

Expand Down Expand Up @@ -3369,18 +3363,14 @@ final class CParser(AST) : Parser!AST
if (token.value == TOK.__attribute__)
cparseGnuAttributes(specifier);

AST.Dsymbol s = null;
symbols = symbolsSave;
if (!symbols)
symbols = new AST.Dsymbols; // lazilly create it

if (!tspec && !specifier.scw && !specifier.mod)
error("specifier-qualifier-list required");
else if (width)
{
if (specifier.alignExps)
error("no alignment-specifier for bit field declaration"); // C11 6.7.5-2
s = new AST.BitFieldDeclaration(width.loc, dt, id, width);
auto s = new AST.BitFieldDeclaration(width.loc, dt, id, width);
members.push(s);
}
else if (id)
{
Expand All @@ -3390,11 +3380,10 @@ final class CParser(AST) : Parser!AST
// declare the symbol
// Give member variables an implicit void initializer
auto initializer = new AST.VoidInitializer(token.loc);
s = new AST.VarDeclaration(token.loc, dt, id, initializer, specifiersToSTC(LVL.member, specifier));
AST.Dsymbol s = new AST.VarDeclaration(token.loc, dt, id, initializer, specifiersToSTC(LVL.member, specifier));
s = applySpecifier(s, specifier);
members.push(s);
}
if (s !is null)
symbols.push(s);

switch (token.value)
{
Expand Down
9 changes: 9 additions & 0 deletions test/compilable/test22698.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// https://issues.dlang.org/show_bug.cgi?id=22698

struct S
{
struct T { int x; };
};

struct T t;

0 comments on commit 541c71b

Please sign in to comment.