diff options
author | Eli Friedman <eli.friedman@gmail.com> | 2009-04-28 19:17:36 +0000 |
---|---|---|
committer | Eli Friedman <eli.friedman@gmail.com> | 2009-04-28 19:17:36 +0000 |
commit | a38da57cd67f5079f3136535b55f68ba242224d0 (patch) | |
tree | fd1ca405e9d40f69dfc3976cf1994dd2c9d6ae33 | |
parent | c379c072405f39bca1d3552408fc0427328e8b6d (diff) | |
download | bcm5719-llvm-a38da57cd67f5079f3136535b55f68ba242224d0.tar.gz bcm5719-llvm-a38da57cd67f5079f3136535b55f68ba242224d0.zip |
PR4097: add logic to Evaluate to handle pointer equality comparisons.
llvm-svn: 70317
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 23 | ||||
-rw-r--r-- | clang/test/Sema/const-eval.c | 2 |
2 files changed, 18 insertions, 7 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 1ea252fecb7..45cf24b9638 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -900,8 +900,8 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { } } - if (E->getOpcode() == BinaryOperator::Sub) { - if (LHSTy->isPointerType() && RHSTy->isPointerType()) { + if (LHSTy->isPointerType() && RHSTy->isPointerType()) { + if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { APValue LHSValue; if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) return false; @@ -915,13 +915,22 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { if (LHSValue.getLValueBase() || RHSValue.getLValueBase()) return false; - const QualType Type = E->getLHS()->getType(); - const QualType ElementType = Type->getAsPointerType()->getPointeeType(); + if (E->getOpcode() == BinaryOperator::Sub) { + const QualType Type = E->getLHS()->getType(); + const QualType ElementType = Type->getAsPointerType()->getPointeeType(); - uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); - D /= Info.Ctx.getTypeSize(ElementType) / 8; + uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); + D /= Info.Ctx.getTypeSize(ElementType) / 8; - return Success(D, E); + return Success(D, E); + } + bool Result; + if (E->getOpcode() == BinaryOperator::EQ) { + Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); + } else if (E->getOpcode() == BinaryOperator::NE) { + Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); + } + return Success(Result, E); } } if (!LHSTy->isIntegralType() || diff --git a/clang/test/Sema/const-eval.c b/clang/test/Sema/const-eval.c index ce1aacd2745..79f96b9c39b 100644 --- a/clang/test/Sema/const-eval.c +++ b/clang/test/Sema/const-eval.c @@ -63,3 +63,5 @@ static struct a V2 = (struct a)(struct a){ 1, 2}; static const struct a V1 = (struct a){ 1, 2}; EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1) +EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1) +EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1) |