From 66572afde48b08fc3c9cc2722ee8457e9ebc98e3 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Fri, 30 May 2008 18:14:48 +0000 Subject: 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. llvm-svn: 51779 --- clang/lib/Sema/SemaDecl.cpp | 51 ++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'clang/lib/Sema/SemaDecl.cpp') diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 2856403ccf9..eb0abafcaaa 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/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; -- cgit v1.2.3