From c1cc6dccd42b91a2ebb397415940da91dbf36103 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Fri, 30 May 2008 18:14:48 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51779 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 51 +++++++++++++++++++++++++++-------------- test/Sema/static-init.c | 4 ++++ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 2856403ccf..eb0abafcaa 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1304,23 +1304,38 @@ bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { 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(Init)) - SubE = ICE->getSubExpr(); - else if (CastExpr* CE = dyn_cast(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(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(Init)) + SubE = ICE->getSubExpr(); + else if (CastExpr* CE = dyn_cast(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 @@ bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { 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; diff --git a/test/Sema/static-init.c b/test/Sema/static-init.c index e710973700..2439d5e714 100644 --- a/test/Sema/static-init.c +++ b/test/Sema/static-init.c @@ -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; -- 2.40.0