[cfe-commits] r65725 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/const-eval.c

Anders Carlsson andersca at mac.com
Sat Feb 28 13:56:50 PST 2009


Author: andersca
Date: Sat Feb 28 15:56:50 2009
New Revision: 65725

URL: http://llvm.org/viewvc/llvm-project?rev=65725&view=rev
Log:
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.
 

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/const-eval.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=65725&r1=65724&r2=65725&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Feb 28 15:56:50 2009
@@ -1622,6 +1622,42 @@
     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.

Modified: cfe/trunk/test/Sema/const-eval.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/const-eval.c?rev=65725&r1=65724&r2=65725&view=diff

==============================================================================
--- cfe/trunk/test/Sema/const-eval.c (original)
+++ cfe/trunk/test/Sema/const-eval.c Sat Feb 28 15:56:50 2009
@@ -47,3 +47,6 @@
 
 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);





More information about the cfe-commits mailing list