]> granicus.if.org Git - clang/commitdiff
Emit a warning when list-initializing a std::initializer_list member.
authorSebastian Redl <sebastian.redl@getdesigned.at>
Sun, 19 Feb 2012 16:31:05 +0000 (16:31 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Sun, 19 Feb 2012 16:31:05 +0000 (16:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150933 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaExprCXX.cpp
test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp

index 0b785f3099054b4363dfa214dcab760018f36175..48dc911b6a30214c7064e4e8bc13939c3ed4fc8d 100644 (file)
@@ -1643,6 +1643,10 @@ Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation EqualLoc,
 
   ExprResult Init = InitExpr;
   if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) {
+    if (isa<InitListExpr>(InitExpr) && isStdInitializerList(FD->getType(), 0)) {
+    Diag(FD->getLocation(), diag::warn_dangling_std_initializer_list)
+        << /*at end of ctor*/1 << InitExpr->getSourceRange();
+    }
     // FIXME: if there is no EqualLoc, this is list-initialization.
     Init = PerformCopyInitialization(
       InitializedEntity::InitializeMember(FD), EqualLoc, InitExpr);
@@ -2112,6 +2116,11 @@ Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init,
       InitList = true;
       Args = &Init;
       NumArgs = 1;
+
+      if (isStdInitializerList(Member->getType(), 0)) {
+        Diag(IdLoc, diag::warn_dangling_std_initializer_list)
+            << /*at end of ctor*/1 << InitRange;
+      }
     }
 
     // Initialize the member.
index 5ec96be4870794d1c9c38621e130534379f447d3..c33ba7558b8ec801a4871f90e72e50021abcbdac 100644 (file)
@@ -1087,7 +1087,7 @@ Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
   if (initStyle == CXXNewExpr::ListInit && isStdInitializerList(AllocType, 0)) {
     Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(),
          diag::warn_dangling_std_initializer_list)
-      << /*at end of FE*/0 << Inits[0]->getSourceRange();
+        << /*at end of FE*/0 << Inits[0]->getSourceRange();
   }
 
   // In ARC, infer 'retaining' for the allocated 
index bfe3f7991eaae137d69af542e7148c88a4433299..6c299c7d8a1d2149f2b879642aff6fc1abe6f2dc 100644 (file)
@@ -127,3 +127,12 @@ void dangle() {
   new auto{1, 2, 3}; // expected-error {{cannot use list-initialization}}
   new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
 }
+
+struct haslist1 {
+  std::initializer_list<int> il = {1, 2, 3}; // expected-warning{{at the end of the constructor}}
+  haslist1();
+};
+
+haslist1::haslist1()
+: il{1, 2, 3} // expected-warning{{at the end of the constructor}}
+{}