[cfe-commits] r55331 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/const-ptr-int-ptr-cast.c

Eli Friedman eli.friedman at gmail.com
Mon Aug 25 13:46:57 PDT 2008


Author: efriedma
Date: Mon Aug 25 15:46:57 2008
New Revision: 55331

URL: http://llvm.org/viewvc/llvm-project?rev=55331&view=rev
Log:
Fix for PR2720; be a little bit more permissive in initializers for 
casting pointers to integers.

Eventually, we should check whether we can evaluate an expression 
using Expr::tryEvaluate, and this codepath should be tightened to only 
handle standard-compliant cases.


Added:
    cfe/trunk/test/Sema/const-ptr-int-ptr-cast.c
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Aug 25 15:46:57 2008
@@ -927,35 +927,37 @@
     return CheckAddressConstantExpression(PExp) ||
            CheckArithmeticConstantExpression(IExp);
   }
-  case Expr::ImplicitCastExprClass: {
-    const Expr* SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
-
-    // Check for implicit promotion
-    if (SubExpr->getType()->isFunctionType() ||
-        SubExpr->getType()->isArrayType())
-      return CheckAddressConstantExpressionLValue(SubExpr);
-
-    // Check for pointer->pointer cast
-    if (SubExpr->getType()->isPointerType())
-      return CheckAddressConstantExpression(SubExpr);
-
-    if (SubExpr->getType()->isArithmeticType())
-      return CheckArithmeticConstantExpression(SubExpr);
-
-    Diag(Init->getExprLoc(),
-         diag::err_init_element_not_constant, Init->getSourceRange());
-    return true;
-  }
+  case Expr::ImplicitCastExprClass:
   case Expr::ExplicitCastExprClass: {
     const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr();
+    if (Init->getStmtClass() == Expr::ImplicitCastExprClass) {
+      // Check for implicit promotion
+      if (SubExpr->getType()->isFunctionType() ||
+          SubExpr->getType()->isArrayType())
+        return CheckAddressConstantExpressionLValue(SubExpr);
+    }
 
     // Check for pointer->pointer cast
     if (SubExpr->getType()->isPointerType())
       return CheckAddressConstantExpression(SubExpr);
 
-    // FIXME: Should we pedwarn for (int*)(0+0)?
-    if (SubExpr->getType()->isArithmeticType())
+    if (SubExpr->getType()->isIntegralType()) {
+      // Check for the special-case of a pointer->int->pointer cast;
+      // this isn't standard, but some code requires it. See
+      // PR2720 for an example.
+      if (const CastExpr* SubCast = dyn_cast<CastExpr>(SubExpr)) {
+        if (SubCast->getSubExpr()->getType()->isPointerType()) {
+          unsigned IntWidth = Context.getIntWidth(SubCast->getType());
+          unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
+          if (IntWidth >= PointerWidth) {
+            return CheckAddressConstantExpression(SubCast->getSubExpr());
+          }
+        }
+      }
+    }
+    if (SubExpr->getType()->isArithmeticType()) {
       return CheckArithmeticConstantExpression(SubExpr);
+    }
 
     Diag(Init->getExprLoc(),
          diag::err_init_element_not_constant, Init->getSourceRange());

Added: cfe/trunk/test/Sema/const-ptr-int-ptr-cast.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/const-ptr-int-ptr-cast.c?rev=55331&view=auto

==============================================================================
--- cfe/trunk/test/Sema/const-ptr-int-ptr-cast.c (added)
+++ cfe/trunk/test/Sema/const-ptr-int-ptr-cast.c Mon Aug 25 15:46:57 2008
@@ -0,0 +1,3 @@
+// RUN: clang -fsyntax-only -verify %s
+
+char *a = (void*)(unsigned long long)(void*)&a;





More information about the cfe-commits mailing list