From a31698842e9893559d58a7bf1a987f2ae90bea0b Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Mon, 16 Apr 2012 04:30:08 +0000 Subject: [PATCH] Make constant evaluation for pointer comparisons work correctly for some uncommon cases. . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154794 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ExprConstant.cpp | 24 ++++++++++++++++++------ test/Sema/const-eval-64.c | 7 +++++++ test/Sema/const-eval.c | 4 ++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 test/Sema/const-eval-64.c diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 4a06e74615..4839c29976 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -5088,14 +5088,26 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { } } + // The comparison here must be unsigned, and performed with the same + // width as the pointer. + // FIXME: Knowing the base is the same for the LHS and RHS isn't enough + // for relational operators. + unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); + uint64_t CompareLHS = LHSOffset.getQuantity(); + uint64_t CompareRHS = RHSOffset.getQuantity(); + assert(PtrSize <= 64 && "Unexpected pointer width"); + uint64_t Mask = ~0ULL >> (64 - PtrSize); + CompareLHS &= Mask; + CompareRHS &= Mask; + switch (E->getOpcode()) { default: llvm_unreachable("missing comparison operator"); - case BO_LT: return Success(LHSOffset < RHSOffset, E); - case BO_GT: return Success(LHSOffset > RHSOffset, E); - case BO_LE: return Success(LHSOffset <= RHSOffset, E); - case BO_GE: return Success(LHSOffset >= RHSOffset, E); - case BO_EQ: return Success(LHSOffset == RHSOffset, E); - case BO_NE: return Success(LHSOffset != RHSOffset, E); + case BO_LT: return Success(CompareLHS < CompareRHS, E); + case BO_GT: return Success(CompareLHS > CompareRHS, E); + case BO_LE: return Success(CompareLHS <= CompareRHS, E); + case BO_GE: return Success(CompareLHS >= CompareRHS, E); + case BO_EQ: return Success(CompareLHS == CompareRHS, E); + case BO_NE: return Success(CompareLHS != CompareRHS, E); } } } diff --git a/test/Sema/const-eval-64.c b/test/Sema/const-eval-64.c new file mode 100644 index 0000000000..5727a93e51 --- /dev/null +++ b/test/Sema/const-eval-64.c @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux %s + +#define EVAL_EXPR(testno, expr) int test##testno = sizeof(struct{char qq[expr];}); + +// +EVAL_EXPR(1, ((char*)-1LL) + 1 == 0 ? 1 : -1) +EVAL_EXPR(2, ((char*)-1LL) + 1 < (char*) -1 ? 1 : -1) diff --git a/test/Sema/const-eval.c b/test/Sema/const-eval.c index f1c0485bc1..3894d73d60 100644 --- a/test/Sema/const-eval.c +++ b/test/Sema/const-eval.c @@ -121,3 +121,7 @@ EVAL_EXPR(43, varfloat && constfloat) // expected-error {{must have a constant s // // (Make sure we continue to reject this.) EVAL_EXPR(44, "x"[0]); // expected-error {{variable length array}} + +// +EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1) +EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1) -- 2.40.0