From 93bbb04db40807a76d2ac65438509996f8844a59 Mon Sep 17 00:00:00 2001 From: higher-performance Date: Wed, 25 Sep 2024 13:28:46 -0400 Subject: [PATCH] Implement explicit field initialization checks in RecordDecl and make it work in both C and C++ --- .../clang/AST/CXXRecordDeclDefinitionBits.def | 9 -- clang/include/clang/AST/Decl.h | 8 ++ clang/include/clang/AST/DeclBase.h | 13 ++- clang/include/clang/AST/DeclCXX.h | 5 - clang/include/clang/Basic/Attr.td | 3 +- clang/include/clang/Basic/AttrDocs.td | 31 +++--- .../clang/Basic/DiagnosticSemaKinds.td | 8 +- clang/lib/AST/Decl.cpp | 8 +- clang/lib/AST/DeclCXX.cpp | 32 ++++-- clang/lib/Sema/SemaDecl.cpp | 3 + clang/lib/Sema/SemaInit.cpp | 26 ++++- clang/lib/Serialization/ASTReaderDecl.cpp | 1 + clang/lib/Serialization/ASTWriterDecl.cpp | 4 +- clang/test/Sema/uninit-variables.c | 11 ++ clang/test/SemaCXX/uninitialized.cpp | 100 ++++++++++++++---- 15 files changed, 196 insertions(+), 66 deletions(-) diff --git a/clang/include/clang/AST/CXXRecordDeclDefinitionBits.def b/clang/include/clang/AST/CXXRecordDeclDefinitionBits.def index 54f28046c63ebb..6620840df0ced2 100644 --- a/clang/include/clang/AST/CXXRecordDeclDefinitionBits.def +++ b/clang/include/clang/AST/CXXRecordDeclDefinitionBits.def @@ -119,15 +119,6 @@ FIELD(HasInitMethod, 1, NO_MERGE) /// within anonymous unions or structs. FIELD(HasInClassInitializer, 1, NO_MERGE) -/// Custom attribute that is True if any field is marked as requiring explicit -/// initialization with [[clang::requires_explicit_initialization]] in a type -/// without a user-provided default constructor, or if this is the case for any -/// base classes and/or member variables whose types are aggregates. -/// -/// In this case, default-construction is diagnosed, as it would not explicitly -/// initialize the field. -FIELD(HasUninitializedExplicitInitFields, 1, NO_MERGE) - /// True if any field is of reference type, and does not have an /// in-class initializer. /// diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 0600ecc4d14a18..111b5582a4848c 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -4269,6 +4269,14 @@ class RecordDecl : public TagDecl { RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion = V; } + bool hasUninitializedExplicitInitFields() const { + return RecordDeclBits.HasUninitializedExplicitInitFields; + } + + void setHasUninitializedExplicitInitFields(bool V) { + RecordDeclBits.HasUninitializedExplicitInitFields = V; + } + /// Determine whether this class can be passed in registers. In C++ mode, /// it must have at least one trivial, non-deleted copy or move constructor. /// FIXME: This should be set as part of completeDefinition. diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index ee662ed73d7e0e..81027cb036ebe3 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -1447,6 +1447,9 @@ class DeclContext { /// hasLazyLocalLexicalLookups, hasLazyExternalLexicalLookups friend class ASTWriter; +protected: + enum { NumOdrHashBits = 25 }; + // We use uint64_t in the bit-fields below since some bit-fields // cross the unsigned boundary and this breaks the packing. @@ -1668,6 +1671,14 @@ class DeclContext { LLVM_PREFERRED_TYPE(bool) uint64_t HasNonTrivialToPrimitiveCopyCUnion : 1; + /// True if any field is marked as requiring explicit initialization with + /// [[clang::requires_explicit_initialization]]. + /// In C++, this is also set for types without a user-provided default + /// constructor, and is propagated from any base classes and/or member + /// variables whose types are aggregates. + LLVM_PREFERRED_TYPE(bool) + uint64_t HasUninitializedExplicitInitFields : 1; + /// Indicates whether this struct is destroyed in the callee. LLVM_PREFERRED_TYPE(bool) uint64_t ParamDestroyedInCallee : 1; @@ -1682,7 +1693,7 @@ class DeclContext { /// True if a valid hash is stored in ODRHash. This should shave off some /// extra storage and prevent CXXRecordDecl to store unused bits. - uint64_t ODRHash : 26; + uint64_t ODRHash : NumOdrHashBits; }; /// Number of inherited and non-inherited bits in RecordDeclBitfields. diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 27994da80243d6..252e6e92564142 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -1152,11 +1152,6 @@ class CXXRecordDecl : public RecordDecl { /// structs). bool hasInClassInitializer() const { return data().HasInClassInitializer; } - bool hasUninitializedExplicitInitFields() const { - return !isUnion() && !hasUserProvidedDefaultConstructor() && - data().HasUninitializedExplicitInitFields; - } - /// Whether this class or any of its subobjects has any members of /// reference type which would make value-initialization ill-formed. /// diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa7d1b8013d036..7f9ec696fc8380 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -1862,10 +1862,9 @@ def Leaf : InheritableAttr { } def ExplicitInit : InheritableAttr { - let Spellings = [Clang<"requires_explicit_initialization", 0>]; + let Spellings = [Clang<"requires_explicit_initialization", 1>]; let Subjects = SubjectList<[Field], ErrorDiag>; let Documentation = [ExplicitInitDocs]; - let LangOpts = [CPlusPlus]; let SimpleHandler = 1; } diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 625dff5b4b7aa9..42c63c86093842 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -1426,24 +1426,31 @@ The ``clang::requires_explicit_initialization`` attribute indicates that the field of an aggregate must be initialized explicitly by users when the class is constructed. Its usage is invalid on non-aggregates. -Example usage: +Note that this attribute is *not* a memory safety feature, and is *not* intended +to guard against use of uninitialized memory. + +Rather, it is intended for use in "parameter-objects", used to simulate the +passing of named parameters. +The attribute generates a warning when explicit initializers for such +"named parameters" are not provided: .. code-block:: c++ - struct some_aggregate { - int x; - int y [[clang::requires_explicit_initialization]]; + struct ArrayIOParams { + size_t count [[clang::requires_explicit_initialization]]; + size_t element_size [[clang::requires_explicit_initialization]]; + int flags = 0; }; - some_aggregate create() { - return {.x = 1}; // error: y is not initialized explicitly - } + size_t ReadArray(FILE *file, void *buffer, ArrayIOParams params); -This attribute is *not* a memory safety feature, and is *not* intended to guard -against use of uninitialized memory. -Rather, its intended use is in structs that represent "parameter objects", to -allow extending them while ensuring that callers do not forget to specify -values for newly added fields ("parameters"). + int main() { + unsigned int buf[512]; + ReadArray(stdin, buf, { + .count = sizeof(buf) / sizeof(*buf), + // warning: field 'element_size' is not explicitly initialized + }); + } }]; } diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 6dc386135034d6..b75b10ed4e0952 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -2336,8 +2336,8 @@ def err_init_reference_member_uninitialized : Error< def note_uninit_reference_member : Note< "uninitialized reference member is here">; def warn_field_requires_explicit_init : Warning< - "field %0 is not explicitly initialized, but was marked as requiring " - "explicit initialization">, InGroup; + "field %select{%1|in %1}0 is not explicitly initialized, but was marked as " + "requiring explicit initialization">, InGroup; def warn_field_is_uninit : Warning<"field %0 is uninitialized when used here">, InGroup; def warn_base_class_is_uninit : Warning< @@ -3148,6 +3148,10 @@ def warn_attribute_ignored_no_calls_in_stmt: Warning< "statement">, InGroup; +def warn_attribute_needs_aggregate : Warning< + "%0 attribute is ignored in non-aggregate type %1">, + InGroup; + def warn_attribute_ignored_non_function_pointer: Warning< "%0 attribute is ignored because %1 is not a function pointer">, InGroup; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index a14b1b33d35efc..167f747f2fb297 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -5014,6 +5014,7 @@ RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); setHasNonTrivialToPrimitiveDestructCUnion(false); setHasNonTrivialToPrimitiveCopyCUnion(false); + setHasUninitializedExplicitInitFields(false); setParamDestroyedInCallee(false); setArgPassingRestrictions(RecordArgPassingKind::CanPassInRegs); setIsRandomized(false); @@ -5214,9 +5215,10 @@ unsigned RecordDecl::getODRHash() { // Only calculate hash on first call of getODRHash per record. ODRHash Hash; Hash.AddRecordDecl(this); - // For RecordDecl the ODRHash is stored in the remaining 26 - // bit of RecordDeclBits, adjust the hash to accomodate. - setODRHash(Hash.CalculateHash() >> 6); + // For RecordDecl the ODRHash is stored in the remaining + // bits of RecordDeclBits, adjust the hash to accommodate. + static_assert(sizeof(Hash.CalculateHash()) * CHAR_BIT == 32); + setODRHash(Hash.CalculateHash() >> (32 - NumOdrHashBits)); return RecordDeclBits.ODRHash; } diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index d56374ead6d794..d261982b5d69a6 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -82,7 +82,6 @@ CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false), HasOnlyCMembers(true), HasInitMethod(false), HasInClassInitializer(false), - HasUninitializedExplicitInitFields(false), HasUninitializedReferenceMember(false), HasUninitializedFields(false), HasInheritedConstructor(false), HasInheritedDefaultConstructor(false), HasInheritedAssignment(false), @@ -460,6 +459,10 @@ CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, if (BaseClassDecl->hasMutableFields()) data().HasMutableFields = true; + if (BaseClassDecl->hasUninitializedExplicitInitFields() && + BaseClassDecl->isAggregate()) + setHasUninitializedExplicitInitFields(true); + if (BaseClassDecl->hasUninitializedReferenceMember()) data().HasUninitializedReferenceMember = true; @@ -1114,7 +1117,7 @@ void CXXRecordDecl::addedMember(Decl *D) { data().PlainOldData = false; if (Field->hasAttr() && !Field->hasInClassInitializer()) { - data().HasUninitializedExplicitInitFields = true; + setHasUninitializedExplicitInitFields(true); } if (T->isReferenceType()) { @@ -1370,7 +1373,7 @@ void CXXRecordDecl::addedMember(Decl *D) { if (FieldRec->hasUninitializedExplicitInitFields() && FieldRec->isAggregate() && !Field->hasInClassInitializer()) - data().HasUninitializedExplicitInitFields = true; + setHasUninitializedExplicitInitFields(true); if (FieldRec->hasUninitializedReferenceMember() && !Field->hasInClassInitializer()) @@ -2160,17 +2163,32 @@ void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { I.setAccess((*I)->getAccess()); ASTContext &Context = getASTContext(); - if (!Context.getLangOpts().CPlusPlus20 && isAggregate() && - hasUserDeclaredConstructor()) { + + if (isAggregate() && hasUserDeclaredConstructor() && + !Context.getLangOpts().CPlusPlus20) { // Diagnose any aggregate behavior changes in C++20 for (field_iterator I = field_begin(), E = field_end(); I != E; ++I) { if (const auto *attr = I->getAttr()) { Context.getDiagnostics().Report( - getLocation(), + attr->getLocation(), diag::warn_cxx20_compat_requires_explicit_init_non_aggregate) - << attr->getRange() << Context.getRecordType(this); + << attr << Context.getRecordType(this); + } + } + } + + if (!isAggregate() && hasUninitializedExplicitInitFields()) { + // Diagnose any fields that required explicit initialization in a + // non-aggregate type. (Note that the fields may not be directly in this + // type, but in a subobject. In such cases we don't emit diagnoses here.) + for (field_iterator I = field_begin(), E = field_end(); I != E; ++I) { + if (const auto *attr = I->getAttr()) { + Context.getDiagnostics().Report(attr->getLocation(), + diag::warn_attribute_needs_aggregate) + << attr << Context.getRecordType(this); } } + setHasUninitializedExplicitInitFields(false); } } diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 8557c25b93a8da..9a29214779c4ce 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -19086,6 +19086,9 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, if (FT.hasNonTrivialToPrimitiveCopyCUnion() || Record->isUnion()) Record->setHasNonTrivialToPrimitiveCopyCUnion(true); } + if (FD->hasAttr()) { + Record->setHasUninitializedExplicitInitFields(true); + } if (FT.isDestructedType()) { Record->setNonTrivialToPrimitiveDestroy(true); Record->setParamDestroyedInCallee(true); diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index e89d72e67d854e..b650c3f584e1da 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -268,6 +268,14 @@ static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, updateStringLiteralType(Str, DeclT); } +void emitUninitializedExplicitInitFields(Sema &S, const RecordDecl *R) { + for (const FieldDecl *Field : R->fields()) { + if (Field->hasAttr()) { + S.Diag(Field->getLocation(), diag::note_entity_declared_at) << Field; + } + } +} + //===----------------------------------------------------------------------===// // Semantic checking for initializer lists. //===----------------------------------------------------------------------===// @@ -746,7 +754,7 @@ void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, if (!VerifyOnly && Field->hasAttr()) { SemaRef.Diag(ILE->getExprLoc(), diag::warn_field_requires_explicit_init) - << Field; + << /* Var-in-Record */ 0 << Field; SemaRef.Diag(Field->getLocation(), diag::note_entity_declared_at) << Field; } @@ -4573,7 +4581,8 @@ static void TryConstructorInitialization(Sema &S, DestRecordDecl != nullptr && DestRecordDecl->isAggregate() && DestRecordDecl->hasUninitializedExplicitInitFields()) { S.Diag(Kind.getLocation(), diag::warn_field_requires_explicit_init) - << "in class"; + << /* Var-in-Record */ 1 << DestRecordDecl; + emitUninitializedExplicitInitFields(S, DestRecordDecl); } // C++11 [dcl.init]p6: @@ -6472,6 +6481,19 @@ void InitializationSequence::InitializeFrom(Sema &S, } } + if (!S.getLangOpts().CPlusPlus && + Kind.getKind() == InitializationKind::IK_Default) { + RecordDecl *Rec = DestType->getAsRecordDecl(); + if (Rec && Rec->hasUninitializedExplicitInitFields()) { + VarDecl *Var = dyn_cast_or_null(Entity.getDecl()); + if (Var && !Initializer) { + S.Diag(Var->getLocation(), diag::warn_field_requires_explicit_init) + << /* Var-in-Record */ 1 << Rec; + emitUninitializedExplicitInitFields(S, Rec); + } + } + } + // - If the destination type is a reference type, see 8.5.3. if (DestType->isReferenceType()) { // C++0x [dcl.init.ref]p1: diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 9272e23c7da3fc..b53aa8a649ae6b 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -859,6 +859,7 @@ RedeclarableResult ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) { RecordDeclBits.getNextBit()); RD->setHasNonTrivialToPrimitiveDestructCUnion(RecordDeclBits.getNextBit()); RD->setHasNonTrivialToPrimitiveCopyCUnion(RecordDeclBits.getNextBit()); + RD->setHasUninitializedExplicitInitFields(RecordDeclBits.getNextBit()); RD->setParamDestroyedInCallee(RecordDeclBits.getNextBit()); RD->setArgPassingRestrictions( (RecordArgPassingKind)RecordDeclBits.getNextBits(/*Width=*/2)); diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index 555f6325da646b..92145831e4db64 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -574,6 +574,7 @@ void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) { RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion()); RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDestructCUnion()); RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveCopyCUnion()); + RecordDeclBits.addBit(D->hasUninitializedExplicitInitFields()); RecordDeclBits.addBit(D->isParamDestroyedInCallee()); RecordDeclBits.addBits(llvm::to_underlying(D->getArgPassingRestrictions()), 2); Record.push_back(RecordDeclBits); @@ -2407,7 +2408,8 @@ void ASTWriter::WriteDeclAbbrevs() { // isNonTrivialToPrimitiveCopy, isNonTrivialToPrimitiveDestroy, // hasNonTrivialToPrimitiveDefaultInitializeCUnion, // hasNonTrivialToPrimitiveDestructCUnion, - // hasNonTrivialToPrimitiveCopyCUnion, isParamDestroyedInCallee, + // hasNonTrivialToPrimitiveCopyCUnion, + // hasUninitializedExplicitInitFields, isParamDestroyedInCallee, // getArgPassingRestrictions // ODRHash Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 26)); diff --git a/clang/test/Sema/uninit-variables.c b/clang/test/Sema/uninit-variables.c index 70a00793fd29ed..d4f4686b600c40 100644 --- a/clang/test/Sema/uninit-variables.c +++ b/clang/test/Sema/uninit-variables.c @@ -551,3 +551,14 @@ struct full_of_empty empty_test_2(void) { struct full_of_empty e; return e; // no-warning } + +struct with_explicit_field { + int x; + int y [[clang::requires_explicit_initialization]]; // #FIELD_Y +}; + +void aggregate() { + struct with_explicit_field a; // expected-warning {{field in 'with_explicit_field' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_Y {{'y' declared here}} + struct with_explicit_field b = {1}; // expected-warning {{field 'y' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_Y {{'y' declared here}} + (void)(&a != &b); +} diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp index 37cf6d1dd60c0a..95e04cdfe5909a 100644 --- a/clang/test/SemaCXX/uninitialized.cpp +++ b/clang/test/SemaCXX/uninitialized.cpp @@ -1474,47 +1474,103 @@ template struct Outer { Outer::Inner outerinner; void aggregate() { - struct S { - [[clang::requires_explicit_initialization]] int x; // expected-note {{declared}} // expected-note {{declared}} // expected-note {{declared}} // expected-note {{declared}} - int y; - int z = 12; - [[clang::requires_explicit_initialization]] int q = 100; // expected-note {{declared}} // expected-note {{declared}} // expected-note {{declared}} // expected-note {{declared}} - static void foo(S) { } + struct NonAgg { + NonAgg() { } + [[clang::requires_explicit_initialization]] int na; // expected-warning {{'requires_explicit_initialization' attribute is ignored in non-aggregate type 'NonAgg'}} }; + NonAgg nonagg; // no-warning + (void)nonagg; - struct D : S { // expected-warning {{not explicitly initialized}} - int f1; - int f2 [[clang::requires_explicit_initialization]]; // expected-note {{declared}} // expected-note {{declared}} + struct S { + [[clang::requires_explicit_initialization]] int s1; // #FIELD_S1 + int s2; + int s3 = 12; + [[clang::requires_explicit_initialization]] int s4 = 100; // #FIELD_S4 + static void foo(S) { } }; struct C { - [[clang::requires_explicit_initialization]] int w; + [[clang::requires_explicit_initialization]] int c1; // #FIELD_C1 C() = default; // Test pre-C++20 aggregates }; + struct D : S { // #TYPE_D + int d1; + int d2 [[clang::requires_explicit_initialization]]; // #FIELD_D2 + }; + + struct D2 : D { // #TYPE_D2 + }; + + struct E { // #TYPE_E + int e1; + D e2 [[clang::requires_explicit_initialization]]; // #FIELD_E2 + struct { + [[clang::requires_explicit_initialization]] D e3; + D2 e4 [[clang::requires_explicit_initialization]]; + }; + }; + S::foo(S{1, 2, 3, 4}); - S::foo(S{.x = 100, .q = 100}); - S::foo(S{.x = 100}); // expected-warning {{'q' is not explicitly initialized}} - S s{.x = 100, .q = 100}; + S::foo(S{.s1 = 100, .s4 = 100}); + S::foo(S{.s1 = 100}); // expected-warning {{field 's4' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S4 {{'s4' declared here}} + + S s{.s1 = 100, .s4 = 100}; (void)s; - S t{.q = 100}; // expected-warning {{'x' is not explicitly initialized}} + + S t{.s4 = 100}; // expected-warning {{field 's1' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S1 {{'s1' declared here}} (void)t; - S *ptr1 = new S; // expected-warning {{not explicitly initialized}} + + S *ptr1 = new S; // expected-warning {{field in 'S' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S4 {{'s4' declared here}} expected-note@#FIELD_S1 {{'s1' declared here}} delete ptr1; - S *ptr2 = new S{.x = 100, .q = 100}; + + S *ptr2 = new S{.s1 = 100, .s4 = 100}; delete ptr2; + #if __cplusplus >= 202002L - D a({}, 0); // expected-warning {{'x' is not explicitly initialized}} expected-warning {{'f2' is not explicitly initialized}} + // expected-warning@+2 {{field 'd2' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning {{field 's1' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S1 {{'s1' declared here}} + D a({}, 0); (void)a; #else - C a; // expected-warning {{not explicitly initialized}} + C a; // expected-warning {{field in 'C' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_C1 {{'c1' declared here}} (void)a; #endif - D b{.f2 = 1}; // expected-warning {{'x' is not explicitly initialized}} expected-warning {{'q' is not explicitly initialized}} + + // expected-warning@+2 {{field 's4' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 's1' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S1 {{'s1' declared here}} + D b{.d2 = 1}; (void)b; - D c{.f1 = 5}; // expected-warning {{'x' is not explicitly initialized}} expected-warning {{'q' is not explicitly initialized}} expected-warning {{'f2' is not explicitly initialized}} - c = {{}, 0}; // expected-warning {{'x' is not explicitly initialized}} expected-warning {{'q' is not explicitly initialized}} expected-warning {{'f2' is not explicitly initialized}} + + // expected-warning@+3 {{field 's4' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+2 {{field 'd2' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@+1 {{field 's1' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S1 {{'s1' declared here}} + D c{.d1 = 5}; + + // expected-warning@+3 {{field 's4' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+2 {{field 'd2' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@+1 {{field 's1' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S1 {{'s1' declared here}} + c = {{}, 0}; (void)c; - D d; // expected-warning {{not explicitly initialized}} // expected-note {{constructor}} + + // expected-note@+3 {{in implicit default constructor for 'D' first required here}} + // expected-warning@#TYPE_D {{field in 'S' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_S1 {{'s1' declared here}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field in 'D' is not explicitly initialized, but was marked as requiring explicit initialization}} expected-note@#FIELD_D2 {{'d2' declared here}} + D d; (void)d; + + // expected-warning@+12 {{field in 'E' is not explicitly initialized, but was marked as requiring explicit initialization}} + // expected-note@#FIELD_E2 {{'e2' declared here}} + // expected-warning@#TYPE_E {{field in 'D' is not explicitly initialized, but was marked as requiring explicit initialization}} + // expected-note@+9 {{in implicit default constructor for 'E' first required here}} + // expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@#TYPE_E {{field in 'D' is not explicitly initialized, but was marked as requiring explicit initialization}} + // expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@#TYPE_E {{field in 'D2' is not explicitly initialized, but was marked as requiring explicit initialization}} + // expected-note@#TYPE_E {{in implicit default constructor for 'D2' first required here}} + // expected-warning@#TYPE_D2 {{field in 'D' is not explicitly initialized, but was marked as requiring explicit initialization}} + // expected-note@+2 {{in implicit default constructor for 'E' first required here}} + // expected-note@#FIELD_D2 {{'d2' declared here}} + E e; + (void)e; }