Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ Bug Fixes to C++ Support
- Fix the dynamic_cast to final class optimization to correctly handle
casts that are guaranteed to fail (#GH137518).
- Fix bug rejecting partial specialization of variable templates with auto NTTPs (#GH118190).
- Fix a crash if errors "member of anonymous [...] redeclares" and
"intializing multiple members of union" coincide (#GH149985).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
60 changes: 32 additions & 28 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5509,40 +5509,44 @@ InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, DeclContext *Owner,
// distinct from the names of any other entity in the
// scope in which the anonymous union is declared.
Invalid = true;
} else {
// C++ [class.union]p2:
// For the purpose of name lookup, after the anonymous union
// definition, the members of the anonymous union are
// considered to have been defined in the scope in which the
// anonymous union is declared.
unsigned OldChainingSize = Chaining.size();
if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD))
Chaining.append(IF->chain_begin(), IF->chain_end());
else
Chaining.push_back(VD);
}
// Inject the IndirectFieldDecl even if invalid, because later
// diagnostics may depend on it being present.

// C++ [class.union]p2:
// For the purpose of name lookup, after the anonymous union
// definition, the members of the anonymous union are
// considered to have been defined in the scope in which the
// anonymous union is declared.
unsigned OldChainingSize = Chaining.size();
if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD))
Chaining.append(IF->chain_begin(), IF->chain_end());
else
Chaining.push_back(VD);

assert(Chaining.size() >= 2);
NamedDecl **NamedChain =
new (SemaRef.Context)NamedDecl*[Chaining.size()];
for (unsigned i = 0; i < Chaining.size(); i++)
NamedChain[i] = Chaining[i];
assert(Chaining.size() >= 2);
NamedDecl **NamedChain =
new (SemaRef.Context) NamedDecl *[Chaining.size()];
for (unsigned i = 0; i < Chaining.size(); i++)
NamedChain[i] = Chaining[i];

IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(),
VD->getType(), {NamedChain, Chaining.size()});
IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(),
VD->getType(), {NamedChain, Chaining.size()});

for (const auto *Attr : VD->attrs())
IndirectField->addAttr(Attr->clone(SemaRef.Context));
for (const auto *Attr : VD->attrs())
IndirectField->addAttr(Attr->clone(SemaRef.Context));

IndirectField->setAccess(AS);
IndirectField->setImplicit();
SemaRef.PushOnScopeChains(IndirectField, S);
IndirectField->setAccess(AS);
IndirectField->setImplicit();
IndirectField->setInvalidDecl(Invalid);
SemaRef.PushOnScopeChains(IndirectField, S);

// That includes picking up the appropriate access specifier.
if (AS != AS_none) IndirectField->setAccess(AS);
// That includes picking up the appropriate access specifier.
if (AS != AS_none)
IndirectField->setAccess(AS);

Chaining.resize(OldChainingSize);
}
Chaining.resize(OldChainingSize);
}
}

Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/class/class.mem/p13.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct X4 { // expected-note{{previous}}
int X;
union {
float Y;
unsigned X4; // expected-error{{redeclares 'X4'}}
unsigned X4; // expected-error{{redeclares 'X4'}} expected-error {{'X4' has the same name as its class}}
};
};
};
Expand Down
10 changes: 10 additions & 0 deletions clang/test/CXX/class/class.union/class.union.anon/p4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ union U {
int y = 1; // expected-error {{initializing multiple members of union}}
};
};

namespace GH149985 {
union U {
int x; // expected-note {{previous declaration is here}}
union {
int x = {}; // expected-error {{member of anonymous union redeclares}} expected-note {{previous initialization is here}}
};
int y = {}; // expected-error {{initializing multiple members of union}}
};
}