summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-02-28 21:56:50 +0000
committerAnders Carlsson <andersca@mac.com>2009-02-28 21:56:50 +0000
commit6c885805cd8a254644e47eefe5dbf18e8d20e87c (patch)
tree1b8d1220f801b50b96758160787ec2b39f0711ea /clang
parent06a2321051787eae3c7af987ca38c26b0681e276 (diff)
downloadbcm5719-llvm-6c885805cd8a254644e47eefe5dbf18e8d20e87c.tar.gz
bcm5719-llvm-6c885805cd8a254644e47eefe5dbf18e8d20e87c.zip
Fix invalid VLAs/VMs in Sema::ActOnVariableDeclarator, so that the variable will have the right type by the time the initializer is checked. This ensures that code like
int a[(int)(1.0 / 1.0) = { 1 } will work. Eli, please review. llvm-svn: 65725
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Sema/SemaDecl.cpp36
-rw-r--r--clang/test/Sema/const-eval.c3
2 files changed, 39 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c9b5405fbe1..76794e571d4 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1622,6 +1622,42 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
Diag(D.getIdentifierLoc(), diag::warn_attribute_weak_on_local);
}
+ bool isIllegalVLA = R->isVariableArrayType() && NewVD->hasGlobalStorage();
+ bool isIllegalVM = R->isVariablyModifiedType() && NewVD->hasLinkage();
+ if (isIllegalVLA || isIllegalVM) {
+ bool SizeIsNegative;
+ QualType FixedTy =
+ TryToFixInvalidVariablyModifiedType(R, Context, SizeIsNegative);
+ if (!FixedTy.isNull()) {
+ Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size);
+ NewVD->setType(FixedTy);
+ } else if (R->isVariableArrayType()) {
+ NewVD->setInvalidDecl();
+
+ const VariableArrayType *VAT = Context.getAsVariableArrayType(R);
+ // FIXME: This won't give the correct result for
+ // int a[10][n];
+ SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange();
+
+ if (NewVD->isFileVarDecl())
+ Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope)
+ << SizeRange;
+ else if (NewVD->getStorageClass() == VarDecl::Static)
+ Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage)
+ << SizeRange;
+ else
+ Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage)
+ << SizeRange;
+ } else {
+ InvalidDecl = true;
+
+ if (NewVD->isFileVarDecl())
+ Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope);
+ else
+ Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage);
+ }
+ }
+
// If name lookup finds a previous declaration that is not in the
// same scope as the new declaration, this may still be an
// acceptable redeclaration.
diff --git a/clang/test/Sema/const-eval.c b/clang/test/Sema/const-eval.c
index fd7b40ea1ba..7714f488171 100644
--- a/clang/test/Sema/const-eval.c
+++ b/clang/test/Sema/const-eval.c
@@ -47,3 +47,6 @@ EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1);
EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1);
+int g23[(int)(1.0 / 1.0)] = { 1 };
+int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // expected-warning {{excess elements in array initializer}}
+int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25);
OpenPOWER on IntegriCloud