| 1 |
// RUN: %clang_analyze_cc1 -std=c++23 -triple x86_64-pc-linux-gnu \ |
| 2 |
// RUN: -analyzer-checker=core,debug.ExprInspection -verify %s |
| 3 |
|
| 4 |
void clang_analyzer_warnIfReached(); |
| 5 |
template <typename T> void clang_analyzer_dump(T); |
| 6 |
template <typename T> void clang_analyzer_value(T); |
| 7 |
|
| 8 |
int ternary_in_builtin_assume(int a, int b) { |
| 9 |
__builtin_assume(a > 10 ? b == 4 : b == 10); |
| 10 |
|
| 11 |
clang_analyzer_value(a); |
| 12 |
// expected-warning@-1 {{[-2147483648, 10]}} |
| 13 |
// expected-warning@-2 {{[11, 2147483647]}} |
| 14 |
|
| 15 |
clang_analyzer_dump(b); // expected-warning{{4}} expected-warning{{10}} |
| 16 |
|
| 17 |
if (a > 20) { |
| 18 |
clang_analyzer_dump(b + 100); // expected-warning {{104}} |
| 19 |
return 2; |
| 20 |
} |
| 21 |
if (a > 10) { |
| 22 |
clang_analyzer_dump(b + 200); // expected-warning {{204}} |
| 23 |
return 1; |
| 24 |
} |
| 25 |
clang_analyzer_dump(b + 300); // expected-warning {{310}} |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
// From: https://github.com/llvm/llvm-project/pull/116462#issuecomment-2517853226 |
| 30 |
int ternary_in_assume(int a, int b) { |
| 31 |
[[assume(a > 10 ? b == 4 : b == 10)]]; |
| 32 |
clang_analyzer_value(a); |
| 33 |
// expected-warning@-1 {{[-2147483648, 10]}} |
| 34 |
// expected-warning@-2 {{[11, 2147483647]}} |
| 35 |
|
| 36 |
clang_analyzer_dump(b); // expected-warning {{4}} expected-warning {{10}} |
| 37 |
|
| 38 |
if (a > 20) { |
| 39 |
clang_analyzer_dump(b + 100); // expected-warning {{104}} |
| 40 |
return 2; |
| 41 |
} |
| 42 |
if (a > 10) { |
| 43 |
clang_analyzer_dump(b + 200); // expected-warning {{204}} |
| 44 |
return 1; |
| 45 |
} |
| 46 |
clang_analyzer_dump(b + 300); // expected-warning {{310}} |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
int assume_and_fallthrough_at_the_same_attrstmt(int a, int b) { |
| 51 |
[[assume(a == 2)]]; |
| 52 |
clang_analyzer_dump(a); // expected-warning {{2 S32b}} |
| 53 |
// expected-warning-re@-1 {{reg_${{[0-9]+}}<int a>}} FIXME: We shouldn't have this dump. |
| 54 |
switch (a) { |
| 55 |
case 2: |
| 56 |
[[fallthrough, assume(b == 30)]]; |
| 57 |
case 4: { |
| 58 |
clang_analyzer_dump(b); // expected-warning {{30 S32b}} |
| 59 |
// expected-warning-re@-1 {{reg_${{[0-9]+}}<int b>}} FIXME: We shouldn't have this dump. |
| 60 |
return b; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// This code should be unreachable. |
| 65 |
clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}} |
| 66 |
|
| 67 |
[[assume(false)]]; // This should definitely make it so. |
| 68 |
clang_analyzer_warnIfReached(); // no-warning |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
void assume_opaque_gh151854_no_crash() { |
| 74 |
extern bool opaque(); |
| 75 |
[[assume(opaque())]]; // no-crash |
| 76 |
// expected-warning@-1 {{assumption is ignored because it contains (potential) side-effects}} |
| 77 |
} |