Don't allow definitions of array variables without some size information in C++. Fixed PR5401

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86165 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 06b09a0..e825e6e 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1488,6 +1488,9 @@
   "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<
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index d89cb5f..088c7dc 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -3433,6 +3433,16 @@
       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 
diff --git a/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp b/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
index 93c955f..a3c147d 100644
--- a/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
+++ b/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
@@ -20,7 +20,7 @@
 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}}
diff --git a/test/SemaCXX/dependent-types.cpp b/test/SemaCXX/dependent-types.cpp
index b2a5c45..3003125 100644
--- a/test/SemaCXX/dependent-types.cpp
+++ b/test/SemaCXX/dependent-types.cpp
@@ -4,7 +4,7 @@
   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];
 }