Rewrite Expr::isNullPointerConstant() to deal with multiple levels of explicit casts.

Now, isNullPointerConstant() will return true for the following: "(void*)(double*)0"



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45951 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Steve Naroff 2008-01-14 02:53:34 +00:00
Родитель 08f1967167
Коммит aaffbf7c79
2 изменённых файлов: 37 добавлений и 23 удалений

Просмотреть файл

@ -929,37 +929,47 @@ bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
return true; return true;
} }
/// Helper function for isNullPointerConstant. This routine skips all
/// explicit casts, implicit casts and paren expressions.
const Expr * getNullPointerConstantExpr(const Expr *exp) {
if (const CastExpr *CE = dyn_cast<CastExpr>(exp)) {
return getNullPointerConstantExpr(CE->getSubExpr());
} else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(exp)) {
return getNullPointerConstantExpr(ICE->getSubExpr());
} else if (const ParenExpr *PE = dyn_cast<ParenExpr>(exp)) {
// Accept ((void*)0) as a null pointer constant, as many other
// implementations do.
return getNullPointerConstantExpr(PE->getSubExpr());
}
return exp;
}
/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
/// integer constant expression with the value zero, or if this is one that is /// integer constant expression with the value zero, or if this is one that is
/// cast to void*. /// cast to void*.
bool Expr::isNullPointerConstant(ASTContext &Ctx) const { bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
// Strip off a cast to void*, if it exists. const CastExpr *CE = dyn_cast<CastExpr>(this);
if (const CastExpr *CE = dyn_cast<CastExpr>(this)) { const Expr *e = getNullPointerConstantExpr(this);
// Check that it is a cast to void*.
if (CE) {
bool castToVoidStar = false;
// Check if the highest precedence cast is "void *".
if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) { if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
QualType Pointee = PT->getPointeeType(); QualType Pointee = PT->getPointeeType();
if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void* if (Pointee.getQualifiers() == 0 && Pointee->isVoidType())
CE->getSubExpr()->getType()->isIntegerType()) // from int. castToVoidStar = true;
return CE->getSubExpr()->isNullPointerConstant(Ctx);
} }
} else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { // This cast must be an integer type or void *.
// Ignore the ImplicitCastExpr type entirely. if (!CE->getType()->isIntegerType() && !castToVoidStar)
return ICE->getSubExpr()->isNullPointerConstant(Ctx); return false;
} else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { } else if (!e->getType()->isIntegerType()) {
// Accept ((void*)0) as a null pointer constant, as many other // This expression must be an integer type.
// implementations do.
return PE->getSubExpr()->isNullPointerConstant(Ctx);
}
// This expression must be an integer type.
if (!getType()->isIntegerType())
return false; return false;
}
// If we have an integer constant expression, we need to *evaluate* it and // If we have an integer constant expression, we need to *evaluate* it and
// test for the value 0. // test for the value 0.
llvm::APSInt Val(32); llvm::APSInt Val(32);
return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; return e->isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
} }
unsigned OCUVectorElementExpr::getNumElements() const { unsigned OCUVectorElementExpr::getNumElements() const {

Просмотреть файл

@ -1,17 +1,21 @@
// RUN: clang -fsyntax-only -verify -pedantic %s // RUN: clang -fsyntax-only -verify -pedantic %s
void foo() { void foo() {
*(0 ? (double *)0 : (void *)0) = 0; *(0 ? (double *)0 : (void *)0) = 0;
*((void *) 0) = 0; // -expected-error {{incomplete type 'void' is not assignable}} *(0 ? (double *)0 : (void *)(int *)0) = 0;
*(0 ? (double *)0 : (void *)(double *)0) = 0;
*(0 ? (double *)0 : (int *)(void *)0) = 0; // expected-warning {{pointer type mismatch ('double *' and 'int *')}}
*(0 ? (double *)0 : (double *)(void *)0) = 0;
*((void *) 0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
double *dp; double *dp;
int *ip; int *ip;
void *vp; void *vp;
dp = vp; dp = vp;
vp = dp; vp = dp;
ip = dp; // -expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}} ip = dp; // expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}}
dp = ip; // -expected-warning {{incompatible pointer types assigning 'int *', expected 'double *'}} dp = ip; // expected-warning {{incompatible pointer types assigning 'int *', expected 'double *'}}
dp = 0 ? (double *)0 : (void *)0; dp = 0 ? (double *)0 : (void *)0;
vp = 0 ? (double *)0 : (void *)0; vp = 0 ? (double *)0 : (void *)0;
ip = 0 ? (double *)0 : (void *)0; // -expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}} ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}}
} }