Skip to content

Commit f566c0f

Browse files
authored
Merge branch 'main' into main
2 parents 895530a + 09f5b9a commit f566c0f

File tree

57 files changed

+1726
-669
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1726
-669
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,11 +1715,40 @@ Variadic Friends __cpp_variadic_friend C
17151715
Trivial Relocatability __cpp_trivial_relocatability C++26 C++03
17161716
--------------------------------------------- -------------------------------- ------------- -------------
17171717
Designated initializers (N494) C99 C89
1718+
``_Complex`` (N693) C99 C89, C++
1719+
``_Bool`` (N815) C99 C89
1720+
Variable-length arrays (N683) C99 C89, C++
1721+
Flexible array members C99 C89, C++
1722+
static and type quals in arrays C99 C89
1723+
``long long`` (N601) C99 C89
1724+
Hexadecimal floating constants (N308) C99 C89
1725+
Compound literals (N716) C99 C89, C++
1726+
``//`` comments (N644) C99 C89
1727+
Mixed declarations and code (N740) C99 C89
1728+
Variadic macros (N707) C99 C89
1729+
Empty macro arguments (N570) C99 C89
1730+
Trailing comma in enum declaration C99 C89
1731+
Implicit ``return 0`` in ``main`` C99 C89
1732+
``__func__`` (N611) C99 C89
1733+
``_Generic`` (N1441) C11 C89, C++
1734+
``_Static_assert`` (N1330) C11 C89, C++
1735+
``_Atomic`` (N1485) C11 C89, C++
1736+
``_Thread_local`` (N1364) C11 C89, C++
17181737
Array & element qualification (N2607) C23 C89
17191738
Attributes (N2335) C23 C89
17201739
``#embed`` (N3017) C23 C89, C++
1740+
Enum with fixed underlying type (N3030) C23 C89
1741+
``#warning`` (N2686) C23 C89
1742+
``_BitInt`` (N3035) C23 C89, C++
1743+
Binary literals (N2549) C23 C89
1744+
Unnamed parameters in a function definition C23 C89
1745+
Free positioning of labels (N2508) C23 C89
1746+
``#elifdef`` (N2645) C23 C89
1747+
``__has_include`` (N2799) C23 C89
17211748
Octal literals prefixed with ``0o`` or ``0O`` C2y C89, C++
17221749
``_Countof`` (N3369, N3469) C2y C89
1750+
``_Generic`` with a type operand (N3260) C2y C89, C++
1751+
``++``/``--`` on ``_Complex`` value (N3259) C2y C89, C++
17231752
============================================= ================================ ============= =============
17241753

17251754
Builtin type aliases

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ AST Dumping Potentially Breaking Changes
7777

7878
Clang Frontend Potentially Breaking Changes
7979
-------------------------------------------
80+
- Members of anonymous unions/structs are now injected as ``IndirectFieldDecl``
81+
into the enclosing record even if their names conflict with other names in the
82+
scope. These ``IndirectFieldDecl`` are marked invalid.
8083

8184
Clang Python Bindings Potentially Breaking Changes
8285
--------------------------------------------------
@@ -217,6 +220,8 @@ Bug Fixes to C++ Support
217220
- Fix the dynamic_cast to final class optimization to correctly handle
218221
casts that are guaranteed to fail (#GH137518).
219222
- Fix bug rejecting partial specialization of variable templates with auto NTTPs (#GH118190).
223+
- Fix a crash if errors "member of anonymous [...] redeclares" and
224+
"intializing multiple members of union" coincide (#GH149985).
220225
- Fix a crash when using ``explicit(bool)`` in pre-C++11 language modes. (#GH152729)
221226
- Fix the parsing of variadic member functions when the ellipis immediately follows a default argument.(#GH153445)
222227

clang/lib/Sema/SemaDecl.cpp

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5503,48 +5503,54 @@ InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, DeclContext *Owner,
55035503
if ((isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) &&
55045504
cast<NamedDecl>(D)->getDeclName()) {
55055505
ValueDecl *VD = cast<ValueDecl>(D);
5506-
if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(),
5507-
VD->getLocation(), AnonRecord->isUnion(),
5508-
SC)) {
5509-
// C++ [class.union]p2:
5510-
// The names of the members of an anonymous union shall be
5511-
// distinct from the names of any other entity in the
5512-
// scope in which the anonymous union is declared.
5506+
// C++ [class.union]p2:
5507+
// The names of the members of an anonymous union shall be
5508+
// distinct from the names of any other entity in the
5509+
// scope in which the anonymous union is declared.
5510+
5511+
bool FieldInvalid = CheckAnonMemberRedeclaration(
5512+
SemaRef, S, Owner, VD->getDeclName(), VD->getLocation(),
5513+
AnonRecord->isUnion(), SC);
5514+
if (FieldInvalid)
55135515
Invalid = true;
5514-
} else {
5515-
// C++ [class.union]p2:
5516-
// For the purpose of name lookup, after the anonymous union
5517-
// definition, the members of the anonymous union are
5518-
// considered to have been defined in the scope in which the
5519-
// anonymous union is declared.
5520-
unsigned OldChainingSize = Chaining.size();
5521-
if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD))
5522-
Chaining.append(IF->chain_begin(), IF->chain_end());
5523-
else
5524-
Chaining.push_back(VD);
55255516

5526-
assert(Chaining.size() >= 2);
5527-
NamedDecl **NamedChain =
5528-
new (SemaRef.Context)NamedDecl*[Chaining.size()];
5529-
for (unsigned i = 0; i < Chaining.size(); i++)
5530-
NamedChain[i] = Chaining[i];
5517+
// Inject the IndirectFieldDecl even if invalid, because later
5518+
// diagnostics may depend on it being present, see findDefaultInitializer.
5519+
5520+
// C++ [class.union]p2:
5521+
// For the purpose of name lookup, after the anonymous union
5522+
// definition, the members of the anonymous union are
5523+
// considered to have been defined in the scope in which the
5524+
// anonymous union is declared.
5525+
unsigned OldChainingSize = Chaining.size();
5526+
if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD))
5527+
Chaining.append(IF->chain_begin(), IF->chain_end());
5528+
else
5529+
Chaining.push_back(VD);
55315530

5532-
IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
5533-
SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(),
5534-
VD->getType(), {NamedChain, Chaining.size()});
5531+
assert(Chaining.size() >= 2);
5532+
NamedDecl **NamedChain =
5533+
new (SemaRef.Context) NamedDecl *[Chaining.size()];
5534+
for (unsigned i = 0; i < Chaining.size(); i++)
5535+
NamedChain[i] = Chaining[i];
55355536

5536-
for (const auto *Attr : VD->attrs())
5537-
IndirectField->addAttr(Attr->clone(SemaRef.Context));
5537+
IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
5538+
SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(),
5539+
VD->getType(), {NamedChain, Chaining.size()});
55385540

5539-
IndirectField->setAccess(AS);
5540-
IndirectField->setImplicit();
5541-
SemaRef.PushOnScopeChains(IndirectField, S);
5541+
for (const auto *Attr : VD->attrs())
5542+
IndirectField->addAttr(Attr->clone(SemaRef.Context));
55425543

5543-
// That includes picking up the appropriate access specifier.
5544-
if (AS != AS_none) IndirectField->setAccess(AS);
5544+
IndirectField->setAccess(AS);
5545+
IndirectField->setImplicit();
5546+
IndirectField->setInvalidDecl(FieldInvalid);
5547+
SemaRef.PushOnScopeChains(IndirectField, S);
55455548

5546-
Chaining.resize(OldChainingSize);
5547-
}
5549+
// That includes picking up the appropriate access specifier.
5550+
if (AS != AS_none)
5551+
IndirectField->setAccess(AS);
5552+
5553+
Chaining.resize(OldChainingSize);
55485554
}
55495555
}
55505556

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7012,9 +7012,12 @@ void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) {
70127012
for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
70137013
++I) {
70147014
NamedDecl *D = (*I)->getUnderlyingDecl();
7015+
// Invalid IndirectFieldDecls have already been diagnosed with
7016+
// err_anonymous_record_member_redecl in
7017+
// SemaDecl.cpp:CheckAnonMemberRedeclaration.
70157018
if (((isa<FieldDecl>(D) || isa<UnresolvedUsingValueDecl>(D)) &&
70167019
Record->hasUserDeclaredConstructor()) ||
7017-
isa<IndirectFieldDecl>(D)) {
7020+
(isa<IndirectFieldDecl>(D) && !D->isInvalidDecl())) {
70187021
Diag((*I)->getLocation(), diag::err_member_name_of_class)
70197022
<< D->getDeclName();
70207023
break;

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,31 +2604,120 @@ SemaOpenACC::CreateInitRecipe(OpenACCClauseKind CK, const Expr *VarExpr) {
26042604
QualType VarTy =
26052605
VarExpr->getType().getNonReferenceType().getUnqualifiedType();
26062606

2607+
IdentifierInfo *VarName = [&]() {
2608+
switch (CK) {
2609+
case OpenACCClauseKind::Private:
2610+
return &getASTContext().Idents.get("openacc.private.init");
2611+
case OpenACCClauseKind::FirstPrivate:
2612+
return &getASTContext().Idents.get("openacc.firstprivate.init");
2613+
case OpenACCClauseKind::Reduction:
2614+
return &getASTContext().Idents.get("openacc.reduction.init");
2615+
default:
2616+
llvm_unreachable("Unknown clause kind?");
2617+
}
2618+
}();
2619+
26072620
VarDecl *Recipe = VarDecl::Create(
26082621
getASTContext(), SemaRef.getCurContext(), VarExpr->getBeginLoc(),
2609-
VarExpr->getBeginLoc(),
2610-
&getASTContext().Idents.get("openacc.private.init"), VarTy,
2622+
VarExpr->getBeginLoc(), VarName, VarTy,
26112623
getASTContext().getTrivialTypeSourceInfo(VarTy), SC_Auto);
26122624

26132625
ExprResult Init;
26142626
VarDecl *Temporary = nullptr;
2615-
2616-
if (CK == OpenACCClauseKind::Private) {
2627+
{
26172628
// Trap errors so we don't get weird ones here. If we can't init, we'll just
26182629
// swallow the errors.
26192630
Sema::TentativeAnalysisScope Trap{SemaRef};
26202631
InitializedEntity Entity = InitializedEntity::InitializeVariable(Recipe);
2621-
InitializationKind Kind =
2622-
InitializationKind::CreateDefault(Recipe->getLocation());
26232632

2624-
InitializationSequence InitSeq(SemaRef.SemaRef, Entity, Kind, {});
2625-
Init = InitSeq.Perform(SemaRef.SemaRef, Entity, Kind, {});
2626-
} else if (CK == OpenACCClauseKind::FirstPrivate) {
2627-
// TODO: OpenACC: Implement this to do a 'copy' operation.
2628-
} else if (CK == OpenACCClauseKind::Reduction) {
2629-
// TODO: OpenACC: Implement this for whatever reduction needs.
2630-
} else {
2631-
llvm_unreachable("Unknown clause kind in CreateInitRecipe");
2633+
if (CK == OpenACCClauseKind::Private) {
2634+
InitializationKind Kind =
2635+
InitializationKind::CreateDefault(Recipe->getLocation());
2636+
2637+
InitializationSequence InitSeq(SemaRef.SemaRef, Entity, Kind, {});
2638+
Init = InitSeq.Perform(SemaRef.SemaRef, Entity, Kind, {});
2639+
2640+
} else if (CK == OpenACCClauseKind::FirstPrivate) {
2641+
// Create a VarDecl to be the 'copied-from' for the copy section of the
2642+
// recipe. This allows us to make the association so that we can use the
2643+
// standard 'generation' ability of the init.
2644+
Temporary = VarDecl::Create(
2645+
getASTContext(), SemaRef.getCurContext(), VarExpr->getBeginLoc(),
2646+
VarExpr->getBeginLoc(), &getASTContext().Idents.get("openacc.temp"),
2647+
VarTy, getASTContext().getTrivialTypeSourceInfo(VarTy), SC_Auto);
2648+
auto *TemporaryDRE = DeclRefExpr::Create(
2649+
getASTContext(), NestedNameSpecifierLoc{}, SourceLocation{},
2650+
Temporary,
2651+
/*ReferstoEnclosingVariableOrCapture=*/false,
2652+
DeclarationNameInfo{DeclarationName{Temporary->getDeclName()},
2653+
VarExpr->getBeginLoc()},
2654+
VarTy, clang::VK_LValue, Temporary, nullptr, NOUR_None);
2655+
2656+
Expr *InitExpr = nullptr;
2657+
2658+
if (const auto *ArrTy = getASTContext().getAsConstantArrayType(VarTy)) {
2659+
// Arrays need to have each individual element initialized as there
2660+
// isn't a normal 'equals' feature in C/C++. This section sets these up
2661+
// as an init list after 'initializing' each individual element.
2662+
llvm::SmallVector<Expr *> Args;
2663+
2664+
// Decay to pointer for the array subscript expression.
2665+
auto *CastToPtr = ImplicitCastExpr::Create(
2666+
getASTContext(),
2667+
getASTContext().getPointerType(ArrTy->getElementType()),
2668+
CK_ArrayToPointerDecay, TemporaryDRE, /*BasePath=*/nullptr,
2669+
clang::VK_LValue, FPOptionsOverride{});
2670+
2671+
for (std::size_t I = 0; I < ArrTy->getLimitedSize(); ++I) {
2672+
// Each element needs to be some sort of copy initialization from an
2673+
// array-index of the original temporary (referenced via a
2674+
// DeclRefExpr).
2675+
2676+
auto *Idx = IntegerLiteral::Create(
2677+
getASTContext(), llvm::APInt(sizeof(std::size_t) * 8, I),
2678+
getASTContext().getSizeType(), VarExpr->getBeginLoc());
2679+
2680+
Expr *Subscript = new (getASTContext()) ArraySubscriptExpr(
2681+
CastToPtr, Idx, ArrTy->getElementType(), clang::VK_LValue,
2682+
OK_Ordinary, VarExpr->getBeginLoc());
2683+
2684+
// Generate a simple copy from the result of the subscript. This will
2685+
// do a bitwise copy or a copy-constructor, as necessary.
2686+
InitializedEntity CopyEntity =
2687+
InitializedEntity::InitializeElement(getASTContext(), I, Entity);
2688+
InitializationKind CopyKind =
2689+
InitializationKind::CreateCopy(VarExpr->getBeginLoc(), {});
2690+
InitializationSequence CopySeq(SemaRef.SemaRef, CopyEntity, CopyKind,
2691+
Subscript,
2692+
/*TopLevelOfInitList=*/true);
2693+
2694+
ExprResult ElemRes =
2695+
CopySeq.Perform(SemaRef.SemaRef, CopyEntity, CopyKind, Subscript);
2696+
Args.push_back(ElemRes.get());
2697+
}
2698+
2699+
InitExpr = new (getASTContext())
2700+
InitListExpr(getASTContext(), VarExpr->getBeginLoc(), Args,
2701+
VarExpr->getEndLoc());
2702+
InitExpr->setType(VarTy);
2703+
2704+
} else {
2705+
// If this isn't an array, we can just do normal copy init from a simple
2706+
// variable reference, so set that up.
2707+
InitExpr = TemporaryDRE;
2708+
}
2709+
2710+
InitializationKind Kind = InitializationKind::CreateForInit(
2711+
Recipe->getLocation(), /*DirectInit=*/true, InitExpr);
2712+
InitializationSequence InitSeq(SemaRef.SemaRef, Entity, Kind, InitExpr,
2713+
/*TopLevelOfInitList=*/false,
2714+
/*TreatUnavailableAsInvalid=*/false);
2715+
Init = InitSeq.Perform(SemaRef.SemaRef, Entity, Kind, InitExpr, &VarTy);
2716+
} else if (CK == OpenACCClauseKind::Reduction) {
2717+
// TODO: OpenACC: Implement this for whatever reduction needs.
2718+
} else {
2719+
llvm_unreachable("Unknown clause kind in CreateInitRecipe");
2720+
}
26322721
}
26332722

26342723
if (Init.get()) {

clang/test/CXX/class/class.union/class.union.anon/p4.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,28 @@ union U {
88
int y = 1; // expected-error {{initializing multiple members of union}}
99
};
1010
};
11+
12+
namespace GH149985 {
13+
union X {
14+
enum {
15+
csize = 42,
16+
cs = sizeof(int) // expected-note {{previous declaration is here}}
17+
};
18+
struct {
19+
int data; // expected-note {{previous declaration is here}}
20+
union X *cs[csize] = {}; // expected-error {{member of anonymous struct redeclares}} expected-note {{previous initialization is here}}
21+
};
22+
struct {
23+
int data; // expected-error {{member of anonymous struct redeclares}}
24+
union X *ds[2] = {}; // expected-error {{initializing multiple members of union}}
25+
};
26+
};
27+
28+
union U {
29+
int x; // expected-note {{previous declaration is here}}
30+
union {
31+
int x = {}; // expected-error {{member of anonymous union redeclares}} expected-note {{previous initialization is here}}
32+
};
33+
int y = {}; // expected-error {{initializing multiple members of union}}
34+
};
35+
}

0 commit comments

Comments
 (0)