diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -1007,21 +1007,37 @@ // // (where std::array is an aggregate struct containing a single array field. - // FIXME: Should aggregate initialization of a struct with a single - // base class and no members also suppress the warning? - if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) + if (!Entity.getParent()) return false; - auto *ParentRD = - Entity.getParent()->getType()->castAs()->getDecl(); - if (CXXRecordDecl *CXXRD = dyn_cast(ParentRD)) - if (CXXRD->getNumBases()) - return false; + // Allows elide brace initialization for aggregates with empty base + if (Entity.getKind() == InitializedEntity::EK_Base) { + auto *ParentRD = + Entity.getParent()->getType()->castAs()->getDecl(); + if (CXXRecordDecl *CXXRD = dyn_cast(ParentRD)) { + if (CXXRD->getNumBases() == 1) { + return ParentRD->field_begin() == ParentRD->field_end(); + } + } + return false; + } - auto FieldIt = ParentRD->field_begin(); - assert(FieldIt != ParentRD->field_end() && - "no fields but have initializer for member?"); - return ++FieldIt == ParentRD->field_end(); + // Allows elide brace initilization for aggregates with one member + if (Entity.getKind() == InitializedEntity::EK_Member) { + auto *ParentRD = + Entity.getParent()->getType()->castAs()->getDecl(); + if (CXXRecordDecl *CXXRD = dyn_cast(ParentRD)) { + if (CXXRD->getNumBases()) { + return false; + } + } + auto FieldIt = ParentRD->field_begin(); + assert(FieldIt != ParentRD->field_end() && + "no fields but have initializer for member?"); + return ++FieldIt == ParentRD->field_end(); + } + + return false; } /// Check whether the range of the initializer \p ParentIList from element diff --git a/clang/test/SemaCXX/aggregate-initialization.cpp b/clang/test/SemaCXX/aggregate-initialization.cpp --- a/clang/test/SemaCXX/aggregate-initialization.cpp +++ b/clang/test/SemaCXX/aggregate-initialization.cpp @@ -172,11 +172,20 @@ }; ArrayAndBaseClass z = {1, 2, 3}; // expected-warning {{suggest braces}} - // It's not clear whether we should be warning in this case. If this - // pattern becomes idiomatic, it would be reasonable to suppress the - // warning here too. + // This pattern is used for tagged aggregates and must not warn template struct JustABaseClass : StdArray {}; - JustABaseClass w = {1, 2, 3}; // expected-warning {{suggest braces}} + JustABaseClass w = {1, 2, 3}; + // but this should be also ok + JustABaseClass v = {{1, 2, 3}}; + + template struct OnionBaseClass : JustABaseClass {}; + OnionBaseClass u = {1, 2, 3}; + OnionBaseClass t = {{{1, 2, 3}}}; + + struct EmptyBase {}; + + template struct AggregateAndEmpty : StdArray, EmptyBase {}; + AggregateAndEmpty p = {1, 2, 3}; // expected-warning {{suggest braces}} #endif #pragma clang diagnostic pop