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

Anders Carlsson andersca at mac.com
Wed Feb 18 22:30:51 PST 2009


Author: andersca
Date: Thu Feb 19 00:30:50 2009
New Revision: 65023

URL: http://llvm.org/viewvc/llvm-project?rev=65023&view=rev
Log:
Emit the correct diagnostics when we constant fold an array size to a negative value.

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=65023&r1=65022&r2=65023&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Feb 19 00:30:50 2009
@@ -3397,11 +3397,14 @@
 /// types into constant array types in certain situations which would otherwise
 /// be errors (for GCC compatibility).
 static QualType TryToFixInvalidVariablyModifiedType(QualType T,
-                                                    ASTContext &Context) {
+                                                    ASTContext &Context,
+                                                    bool &SizeIsNegative) {
   // This method tries to turn a variable array into a constant
   // array even when the size isn't an ICE.  This is necessary
   // for compatibility with code that depends on gcc's buggy
   // constant expression folding, like struct {char x[(int)(char*)2];}
+  SizeIsNegative = false;
+  
   const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T);
   if (!VLATy) return QualType();
   
@@ -3415,6 +3418,8 @@
   if (Res >= llvm::APSInt(Res.getBitWidth(), Res.isUnsigned()))
     return Context.getConstantArrayType(VLATy->getElementType(),
                                         Res, ArrayType::Normal, 0);
+
+  SizeIsNegative = true;
   return QualType();
 }
 
@@ -3463,12 +3468,17 @@
   // C99 6.7.2.1p8: A member of a structure or union may have any type other
   // than a variably modified type.
   if (T->isVariablyModifiedType()) {
-    QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context);
+    bool SizeIsNegative;
+    QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context,
+                                                           SizeIsNegative);
     if (!FixedTy.isNull()) {
       Diag(Loc, diag::warn_illegal_constant_array_size);
       T = FixedTy;
     } else {
-      Diag(Loc, diag::err_typecheck_field_variable_size);
+      if (SizeIsNegative)
+        Diag(Loc, diag::err_typecheck_negative_array_size);
+      else
+        Diag(Loc, diag::err_typecheck_field_variable_size);
       T = Context.IntTy;
       InvalidDecl = true;
     }

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

==============================================================================
--- cfe/trunk/test/Sema/const-eval.c (original)
+++ cfe/trunk/test/Sema/const-eval.c Thu Feb 19 00:30:50 2009
@@ -34,3 +34,7 @@
 int g17[(3?:1) - 2]; 
 
 EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1);
+
+struct s {
+  int a[(int)-1.0f]; // expected-error {{array size is negative}}
+};





More information about the cfe-commits mailing list