]> granicus.if.org Git - clang/commitdiff
Don't allow definitions of array variables without some size information in C++....
authorSebastian Redl <sebastian.redl@getdesigned.at>
Thu, 5 Nov 2009 19:47:47 +0000 (19:47 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Thu, 5 Nov 2009 19:47:47 +0000 (19:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86165 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
test/SemaCXX/dependent-types.cpp

index 06b09a0b7304f72382bc55834be70bf143af4f2d..e825e6e826eda91a11448d5dd6d611168fadf2fe 100644 (file)
@@ -1488,6 +1488,9 @@ def err_tentative_def_incomplete_type_arr : Error<
   "tentative definition has array of type %0 that is never completed">;
 def warn_tentative_incomplete_array : Warning<
   "tentative array definition assumed to have one element">;
+def err_typecheck_incomplete_array_needs_initializer : Error<
+  "definition of variable with array type needs an explicit size "
+  "or an initializer">;
 
 def err_realimag_invalid_type : Error<"invalid type %0 to %1 operator">;
 def err_typecheck_sclass_fscope : Error<
index d89cb5fc97f562fb5d2333406197426ba54fa30d..088c7dc780717ca1a6cb082b6dbade877bc21397 100644 (file)
@@ -3433,6 +3433,16 @@ void Sema::ActOnUninitializedDecl(DeclPtrTy dcl,
       return;
     }
 
+    // An array without size is an incomplete type, and there are no special
+    // rules in C++ to make such a definition acceptable.
+    if (getLangOptions().CPlusPlus && Type->isIncompleteArrayType() &&
+        !Var->hasExternalStorage()) {
+      Diag(Var->getLocation(),
+           diag::err_typecheck_incomplete_array_needs_initializer);
+      Var->setInvalidDecl();
+      return;
+    }
+
     // C++ [temp.expl.spec]p15:
     //   An explicit specialization of a static data member of a template is a
     //   definition if the declaration includes an initializer; otherwise, it 
index 93c955f736d9627380b0a190376a02026665e15b..a3c147db5ee21f5937c6194cea009997cddcb9c3 100644 (file)
@@ -20,7 +20,7 @@ int ar6[-1]; // expected-error {{array size is negative}}
 int ar7[0u]; // expected-warning {{zero size arrays are an extension}}
 
 // An array with unknown bound is incomplete.
-int ar8[]; // FIXME: This needs to fail!
+int ar8[]; // expected-error {{needs an explicit size or an initializer}}
 // So is an array with an incomplete element type.
 struct Incomplete; // expected-note {{forward declaration}}
 Incomplete ar9[10]; // expected-error {{incomplete type}}
index b2a5c45787c910a8dd1af867986748b779558a1d..300312580a8fd46ce3f986486855f08299ed0c3f 100644 (file)
@@ -4,7 +4,7 @@ template<typename T, int Size> void f() {
   T x1;
   T* x2;
   T& x3; // expected-error{{declaration of reference variable 'x3' requires an initializer}}
-  T x4[]; // expected-error{{variable has incomplete type 'T []'}}
+  T x4[]; // expected-error{{needs an explicit size or an initializer}}
   T x5[Size];
   int x6[Size];
 }