r213332 - PR20346: fix aggregate initialization / template instantiation bug:

Richard Smith richard-llvm at metafoo.co.uk
Thu Jul 17 16:12:07 PDT 2014


Author: rsmith
Date: Thu Jul 17 18:12:06 2014
New Revision: 213332

URL: http://llvm.org/viewvc/llvm-project?rev=213332&view=rev
Log:
PR20346: fix aggregate initialization / template instantiation bug:

If, during the initial parse of a template, we perform aggregate initialization
and form an implicit value initialization for an array type, then when we come
to instantiate the template and redo the initialization step, we would try to
match the implicit value initialization up against an array *element*, not to
the complete array.

Remarkably, we've had this bug since ~the dawn of time, but only noticed it
recently.

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/SemaTemplate/instantiate-decl-init.cpp

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=213332&r1=213331&r2=213332&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Thu Jul 17 18:12:06 2014
@@ -914,6 +914,15 @@ void InitListChecker::CheckSubElementTyp
     assert(SemaRef.getLangOpts().CPlusPlus &&
            "non-aggregate records are only possible in C++");
     // C++ initialization is handled later.
+  } else if (auto *VIE = dyn_cast<ImplicitValueInitExpr>(expr)) {
+    // This happens during template instantiation when we see an InitListExpr
+    // that we've already checked once.
+    assert(SemaRef.Context.hasSameType(VIE->getType(), ElemType) &&
+           "found implicit initialization for the wrong type");
+    if (!VerifyOnly)
+      UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+    ++Index;
+    return;
   }
 
   // FIXME: Need to handle atomic aggregate types with implicit init lists.

Modified: cfe/trunk/test/SemaTemplate/instantiate-decl-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-decl-init.cpp?rev=213332&r1=213331&r2=213332&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-decl-init.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-decl-init.cpp Thu Jul 17 18:12:06 2014
@@ -45,3 +45,23 @@ template<int N> void f1() {
   NonTrivial array[N];
 }
 template<> void f1<2>();
+
+namespace PR20346 {
+  struct S { short inner_s; };
+
+  struct outer_struct {
+    wchar_t arr[32];
+    S outer_s;
+  };
+
+  template <class T>
+  void OpenFileSession() {
+    // Ensure that we don't think the ImplicitValueInitExpr generated here
+    // during the initial parse only initializes the first array element!
+    outer_struct asdfasdf = {};
+  };
+
+  void foo() {
+    OpenFileSession<int>();
+  }
+}





More information about the cfe-commits mailing list