Added a patch to prevent variable definition creation for incomplete types 
with zero initializer elements to prevent the mentioned assertion and throw 
diagnostic.
  Patch in
  llvm/tools/clang/lib/SemaSemaTemplateInstantiateDecl.cpp

  Please let me know your review comments.

  Thanks.

Hi dblaikie, rsmith,

http://llvm-reviews.chandlerc.com/D1700

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1700?vs=4342&id=4957#toc

Files:
  llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp

Index: llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -364,6 +364,21 @@
   if (SubstQualifier(D, Var))
     return 0;
 
+  if (D->getInit() && !InstantiatingVarTemplate) {
+    const Expr* Arg = D->getInit();
+    const InitListExpr *ILE = dyn_cast<InitListExpr>(Arg);
+    if(!ILE || (ILE && !ILE->isStringLiteralInit() && ILE->getNumInits() == 0 
)) {
+      if (D->getType()->isIncompleteType() || 
D->getType()->isIncompleteOrObjectType() ) {
+        if (SemaRef.RequireCompleteType(Var->getLocation(),
+                Var->getType(), 
diag::err_typecheck_incomplete_array_needs_initializer)) {
+          Var->setInvalidDecl();
+          return 0;
+        }
+      }
+    }
+  }
+
+
   SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
                                      StartingScope, InstantiatingVarTemplate);
   return Var;
Index: llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp
===================================================================
--- llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp
+++ llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s 
+
+template<class T>
+T&& create();
+
+template<class T, class... Args>
+void test() {
+  T t(create<Args>()...);      // expected-error {{definition of variable with 
array type needs an explicit size or an initializer}}
+  (void) t;
+}
+
+int main() {
+  test<int[]>();               // expected-note {{in instantiation of function 
template specialization 'test<int []>' requested here}}
+}
+
Index: llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ llvm/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -364,6 +364,21 @@
   if (SubstQualifier(D, Var))
     return 0;
 
+  if (D->getInit() && !InstantiatingVarTemplate) {
+    const Expr* Arg = D->getInit();
+    const InitListExpr *ILE = dyn_cast<InitListExpr>(Arg);
+    if(!ILE || (ILE && !ILE->isStringLiteralInit() && ILE->getNumInits() == 0 )) {
+      if (D->getType()->isIncompleteType() || D->getType()->isIncompleteOrObjectType() ) {
+        if (SemaRef.RequireCompleteType(Var->getLocation(),
+                Var->getType(), diag::err_typecheck_incomplete_array_needs_initializer)) {
+          Var->setInvalidDecl();
+          return 0;
+        }
+      }
+    }
+  }
+
+
   SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
                                      StartingScope, InstantiatingVarTemplate);
   return Var;
Index: llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp
===================================================================
--- llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp
+++ llvm/tools/clang/test/SemaCXX/cxx0x-function-template-specialization.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s 
+
+template<class T>
+T&& create();
+
+template<class T, class... Args>
+void test() {
+  T t(create<Args>()...);	// expected-error {{definition of variable with array type needs an explicit size or an initializer}}
+  (void) t;
+}
+
+int main() {
+  test<int[]>();	        // expected-note {{in instantiation of function template specialization 'test<int []>' requested here}}
+}
+
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to