summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Redl <sebastian.redl@getdesigned.at>2009-11-05 19:47:47 +0000
committerSebastian Redl <sebastian.redl@getdesigned.at>2009-11-05 19:47:47 +0000
commit1060067dd11ae11cc637d9d3c50eb9407dca514b (patch)
tree1c0bbdc5e4d00e9cab1e4885f6d050cb574202aa
parent415c64ea3f621212180ba8b04909fdfc3c51309a (diff)
downloadbcm5719-llvm-1060067dd11ae11cc637d9d3c50eb9407dca514b.tar.gz
bcm5719-llvm-1060067dd11ae11cc637d9d3c50eb9407dca514b.zip
Don't allow definitions of array variables without some size information in C++. Fixed PR5401
llvm-svn: 86165
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td3
-rw-r--r--clang/lib/Sema/SemaDecl.cpp10
-rw-r--r--clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp2
-rw-r--r--clang/test/SemaCXX/dependent-types.cpp2
4 files changed, 15 insertions, 2 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 06b09a0b730..e825e6e826e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d89cb5fc97f..088c7dc7807 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -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
diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
index 93c955f736d..a3c147db5ee 100644
--- a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
@@ -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}}
diff --git a/clang/test/SemaCXX/dependent-types.cpp b/clang/test/SemaCXX/dependent-types.cpp
index b2a5c45787c..300312580a8 100644
--- a/clang/test/SemaCXX/dependent-types.cpp
+++ b/clang/test/SemaCXX/dependent-types.cpp
@@ -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];
}
OpenPOWER on IntegriCloud