| 1 |
// RUN: %clang_analyze_cc1 %s \ |
| 2 |
// RUN: -analyzer-checker=core \ |
| 3 |
// RUN: -analyzer-checker=debug.ExprInspection \ |
| 4 |
// RUN: -analyzer-config support-symbolic-integer-casts=true \ |
| 5 |
// RUN: -analyzer-config eagerly-assume=false \ |
| 6 |
// RUN: -triple x86_64-unknown-linux-gnu \ |
| 7 |
// RUN: -verify |
| 8 |
|
| 9 |
// Test that the SValBuilder is able to look up and use a constraint for an |
| 10 |
// operand of a SymbolCast, when the operand is constrained to a const value. |
| 11 |
|
| 12 |
void clang_analyzer_eval(bool); |
| 13 |
|
| 14 |
extern void abort() __attribute__((__noreturn__)); |
| 15 |
#define assert(expr) ((expr) ? (void)(0) : abort()) |
| 16 |
|
| 17 |
void test(int x) { |
| 18 |
// Constrain a SymSymExpr to a constant value. |
| 19 |
assert(x * x == 1); |
| 20 |
// It is expected to be able to get the constraint for the operand of the |
| 21 |
// cast. |
| 22 |
clang_analyzer_eval((char)(x * x) == 1); // expected-warning{{TRUE}} |
| 23 |
clang_analyzer_eval((long)(x * x) == 1); // expected-warning{{TRUE}} |
| 24 |
} |
| 25 |
|
| 26 |
void test1(int x, int y) { |
| 27 |
// Even if two lower bytes of `x` equal to zero, it doesn't mean that |
| 28 |
// the entire `x` is zero. We are not able to know the exact value of x. |
| 29 |
// It can be one of 65536 possible values like |
| 30 |
// [0, 65536, -65536, 131072, -131072, ...]. To avoid huge range sets we |
| 31 |
// still assume `x` in the range [INT_MIN, INT_MAX]. |
| 32 |
assert((short)x == 0); // Lower two bytes are set to 0. |
| 33 |
|
| 34 |
static_assert((short)65536 == 0, ""); |
| 35 |
static_assert((short)-65536 == 0, ""); |
| 36 |
static_assert((short)131072 == 0, ""); |
| 37 |
static_assert((short)-131072 == 0, ""); |
| 38 |
clang_analyzer_eval(x == 0); // expected-warning{{UNKNOWN}} |
| 39 |
|
| 40 |
// These are not truncated to short as zero. |
| 41 |
static_assert((short)1 != 0, ""); |
| 42 |
clang_analyzer_eval(x == 1); // expected-warning{{FALSE}} |
| 43 |
static_assert((short)-1 != 0, ""); |
| 44 |
clang_analyzer_eval(x == -1); // expected-warning{{FALSE}} |
| 45 |
static_assert((short)65537 != 0, ""); |
| 46 |
clang_analyzer_eval(x == 65537); // expected-warning{{FALSE}} |
| 47 |
static_assert((short)-65537 != 0, ""); |
| 48 |
clang_analyzer_eval(x == -65537); // expected-warning{{FALSE}} |
| 49 |
static_assert((short)131073 != 0, ""); |
| 50 |
clang_analyzer_eval(x == 131073); // expected-warning{{FALSE}} |
| 51 |
static_assert((short)-131073 != 0, ""); |
| 52 |
clang_analyzer_eval(x == -131073); // expected-warning{{FALSE}} |
| 53 |
|
| 54 |
// Check for implicit cast. |
| 55 |
short s = y; |
| 56 |
assert(s == 0); |
| 57 |
clang_analyzer_eval(y == 0); // expected-warning{{UNKNOWN}} |
| 58 |
} |