[cfe-commits] r51779 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/static-init.c

Eli Friedman eli.friedman at gmail.com
Fri May 30 11:14:48 PDT 2008


Author: efriedma
Date: Fri May 30 13:14:48 2008
New Revision: 51779

URL: http://llvm.org/viewvc/llvm-project?rev=51779&view=rev
Log:
Allow a pointer implicitly cast to a bool as a constant expression, as 
required by the standard (the standard doesn't know anything about 
implicit casts).

Disallow pointers cast to non-integral arithmetic types as constant 
expressions.  This was previously allowed by accident. 


Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/static-init.c

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri May 30 13:14:48 2008
@@ -1304,23 +1304,38 @@
   if (Init->isNullPointerConstant(Context))
     return false;
   if (Init->getType()->isArithmeticType()) {
-    // Special check for pointer cast to int; we allow
-    // an address constant cast to an integer if the integer
-    // is of an appropriate width (this sort of code is apparently used
-    // in some places).
-    // FIXME: Add pedwarn?
-    Expr* SubE = 0;
-    if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init))
-      SubE = ICE->getSubExpr();
-    else if (CastExpr* CE = dyn_cast<CastExpr>(Init))
-      SubE = CE->getSubExpr();
-    if (SubE && (SubE->getType()->isPointerType() ||
-                 SubE->getType()->isArrayType() ||
-                 SubE->getType()->isFunctionType())) {
-      unsigned IntWidth = Context.getTypeSize(Init->getType());
-      unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
-      if (IntWidth >= PointerWidth)
-        return CheckAddressConstantExpression(Init);
+    QualType InitTy = Init->getType().getCanonicalType().getUnqualifiedType();
+    if (InitTy == Context.BoolTy) {
+      // Special handling for pointers implicitly cast to bool;
+      // (e.g. "_Bool rr = &rr;"). This is only legal at the top level.
+      if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) {
+        Expr* SubE = ICE->getSubExpr();
+        if (SubE->getType()->isPointerType() ||
+            SubE->getType()->isArrayType() ||
+            SubE->getType()->isFunctionType()) {
+          return CheckAddressConstantExpression(Init);
+        }
+      }
+    } else if (InitTy->isIntegralType()) {
+      Expr* SubE = 0;
+      if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init))
+        SubE = ICE->getSubExpr();
+      else if (CastExpr* CE = dyn_cast<CastExpr>(Init))
+        SubE = CE->getSubExpr();
+      // Special check for pointer cast to int; we allow as an extension
+      // an address constant cast to an integer if the integer
+      // is of an appropriate width (this sort of code is apparently used
+      // in some places).
+      // FIXME: Add pedwarn?
+      // FIXME: Don't allow bitfields here!  Need the FieldDecl for that.
+      if (SubE && (SubE->getType()->isPointerType() ||
+                   SubE->getType()->isArrayType() ||
+                   SubE->getType()->isFunctionType())) {
+        unsigned IntWidth = Context.getTypeSize(Init->getType());
+        unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
+        if (IntWidth >= PointerWidth)
+          return CheckAddressConstantExpression(Init);
+      }
     }
 
     return CheckArithmeticConstantExpression(Init);
@@ -1329,6 +1344,8 @@
   if (Init->getType()->isPointerType())
     return CheckAddressConstantExpression(Init);
 
+  // An array type at the top level that isn't an init-list must
+  // be a string literal
   if (Init->getType()->isArrayType())
     return false;
 

Modified: cfe/trunk/test/Sema/static-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/static-init.c?rev=51779&r1=51778&r2=51779&view=diff

==============================================================================
--- cfe/trunk/test/Sema/static-init.c (original)
+++ cfe/trunk/test/Sema/static-init.c Fri May 30 13:14:48 2008
@@ -1,3 +1,7 @@
 // RUN: clang -fsyntax-only -verify %s
 static int f = 10;
 static int b = f; // expected-error {{initializer element is not constant}}
+
+float r  = (float) &r; // expected-error {{initializer element is not constant}}
+long long s = (long long) &s;
+_Bool t = &t;





More information about the cfe-commits mailing list