Skip to content

Commit

Permalink
Make constant evaluation for pointer comparisons work correctly for s…
Browse files Browse the repository at this point in the history
…ome uncommon cases. <rdar://problem/10962435>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154794 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
eefriedman committed Apr 16, 2012
1 parent 4762a2d commit a316988
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/Sema/const-eval-64.c
Original file line number Diff line number Diff line change
@@ -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];});

// <rdar://problem/10962435>
EVAL_EXPR(1, ((char*)-1LL) + 1 == 0 ? 1 : -1)
EVAL_EXPR(2, ((char*)-1LL) + 1 < (char*) -1 ? 1 : -1)
4 changes: 4 additions & 0 deletions test/Sema/const-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ EVAL_EXPR(43, varfloat && constfloat) // expected-error {{must have a constant s
// <rdar://problem/11205586>
// (Make sure we continue to reject this.)
EVAL_EXPR(44, "x"[0]); // expected-error {{variable length array}}

// <rdar://problem/10962435>
EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1)
EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1)

0 comments on commit a316988

Please sign in to comment.