1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -Wno-tautological-compare -analyzer-config eagerly-assume=false %s
3 void clang_analyzer_eval(bool);
6 #define INT_MAX (UINT_MAX & (UINT_MAX >> 1))
7 #define INT_MIN (-INT_MAX - 1)
13 void separateExpressions (int a
) {
17 clang_analyzer_eval(a
!= 0 && b
== 0); // expected-warning{{FALSE}}
20 void oneLongExpression (int a
) {
21 // Expression canonicalization should still allow this to work, even though
22 // the first term is on the left.
23 int b
= 15 + a
+ 15 - 10 - 20;
25 clang_analyzer_eval(a
!= 0 && b
== 0); // expected-warning{{FALSE}}
28 void mixedTypes (int a
) {
29 // Different additive types should not cause crashes when constant-folding.
30 // This is part of PR7406.
32 clang_analyzer_eval(a
!= 0 && (b
-1) == 0); // not crash, expected-warning{{FALSE}}
35 clang_analyzer_eval(a
!= 0 && (c
-1) == 0); // not crash, expected-warning{{FALSE}}
42 // Equality and inequality only
43 void eq_ne (unsigned a
) {
45 clang_analyzer_eval(a
+1 == 0); // expected-warning{{TRUE}}
46 clang_analyzer_eval(a
-1 == UINT_MAX
-1); // expected-warning{{TRUE}}
48 clang_analyzer_eval(a
+1 != 0); // expected-warning{{TRUE}}
49 clang_analyzer_eval(a
-1 != UINT_MAX
-1); // expected-warning{{TRUE}}
53 // Mixed typed inequalities (part of PR7406)
54 // These should not crash.
55 void mixed_eq_ne (int a
) {
57 clang_analyzer_eval(a
+1U == 2); // expected-warning{{TRUE}}
58 clang_analyzer_eval(a
-1U == 0); // expected-warning{{TRUE}}
60 clang_analyzer_eval(a
+1U != 2); // expected-warning{{TRUE}}
61 clang_analyzer_eval(a
-1U != 0); // expected-warning{{TRUE}}
66 // Simple order comparisons with no adjustment
67 void baselineGT (unsigned a
) {
69 clang_analyzer_eval(a
!= 0); // expected-warning{{TRUE}}
71 clang_analyzer_eval(a
== 0); // expected-warning{{TRUE}}
74 void baselineGE (unsigned a
) {
76 clang_analyzer_eval(a
== UINT_MAX
); // expected-warning{{TRUE}}
78 clang_analyzer_eval(a
!= UINT_MAX
); // expected-warning{{TRUE}}
81 void baselineLT (unsigned a
) {
83 clang_analyzer_eval(a
!= UINT_MAX
); // expected-warning{{TRUE}}
85 clang_analyzer_eval(a
== UINT_MAX
); // expected-warning{{TRUE}}
88 void baselineLE (unsigned a
) {
90 clang_analyzer_eval(a
== 0); // expected-warning{{TRUE}}
92 clang_analyzer_eval(a
!= 0); // expected-warning{{TRUE}}
96 // Adjustment gives each of these an extra solution!
97 void adjustedGT (unsigned a
) {
98 clang_analyzer_eval(a
-1 > UINT_MAX
-1); // expected-warning{{UNKNOWN}}
101 void adjustedGE (unsigned a
) {
102 clang_analyzer_eval(a
-1 > UINT_MAX
-1); // expected-warning{{UNKNOWN}}
104 if (a
-1 >= UINT_MAX
-1)
105 clang_analyzer_eval(a
== UINT_MAX
); // expected-warning{{UNKNOWN}}
108 void adjustedLT (unsigned a
) {
109 clang_analyzer_eval(a
+1 < 1); // expected-warning{{UNKNOWN}}
112 void adjustedLE (unsigned a
) {
113 clang_analyzer_eval(a
+1 <= 1); // expected-warning{{UNKNOWN}}
116 clang_analyzer_eval(a
== 0); // expected-warning{{UNKNOWN}}
121 // The negative forms are exercised as well
122 // because clang_analyzer_eval tests both possibilities.
123 void tautologies(unsigned a
) {
124 clang_analyzer_eval(a
<= UINT_MAX
); // expected-warning{{TRUE}}
125 clang_analyzer_eval(a
>= 0); // expected-warning{{TRUE}}
129 // Tautologies from outside the range of the symbol
130 void tautologiesOutside(unsigned char a
) {
131 clang_analyzer_eval(a
<= 0x100); // expected-warning{{TRUE}}
132 clang_analyzer_eval(a
< 0x100); // expected-warning{{TRUE}}
134 clang_analyzer_eval(a
!= 0x100); // expected-warning{{TRUE}}
135 clang_analyzer_eval(a
!= -1); // expected-warning{{TRUE}}
137 clang_analyzer_eval(a
> -1); // expected-warning{{TRUE}}
138 clang_analyzer_eval(a
>= -1); // expected-warning{{TRUE}}
142 // Wraparound with mixed types. Note that the analyzer assumes
143 // -fwrapv semantics.
144 void mixedWraparoundBasicCheck(int a
) {
149 clang_analyzer_eval(a
== max
&& b
!= min
); // expected-warning{{FALSE}}
152 void mixedWraparoundLE_GT(int a
) {
156 clang_analyzer_eval((a
+ 2) <= (max
+ 1LL)); // expected-warning{{TRUE}}
157 clang_analyzer_eval((a
- 2) > (min
- 1LL)); // expected-warning{{TRUE}}
158 clang_analyzer_eval((a
+ 2LL) <= max
); // expected-warning{{UNKNOWN}}
161 void mixedWraparoundGE_LT(int a
) {
165 clang_analyzer_eval((a
+ 2) < (max
+ 1LL)); // expected-warning{{TRUE}}
166 clang_analyzer_eval((a
- 2) >= (min
- 1LL)); // expected-warning{{TRUE}}
167 clang_analyzer_eval((a
- 2LL) >= min
); // expected-warning{{UNKNOWN}}
170 void mixedWraparoundEQ_NE(int a
) {
173 clang_analyzer_eval((a
+ 2) != (max
+ 1LL)); // expected-warning{{TRUE}}
174 clang_analyzer_eval((a
+ 2LL) == (max
+ 1LL)); // expected-warning{{UNKNOWN}}
178 // Mixed-signedness comparisons.
179 void mixedSignedness(int a
, unsigned b
) {
181 unsigned uMin
= INT_MIN
;
183 clang_analyzer_eval(a
== sMin
&& a
!= uMin
); // expected-warning{{FALSE}}
184 clang_analyzer_eval(b
== uMin
&& b
!= sMin
); // expected-warning{{FALSE}}
187 void mixedSignedness2(int a
) {
190 clang_analyzer_eval(a
== UINT_MAX
); // expected-warning{{TRUE}}
193 void mixedSignedness3(unsigned a
) {
196 clang_analyzer_eval(a
== -1); // expected-warning{{TRUE}}
200 void multiplicativeBasicTest(int x
) {
201 // At one point we were ignoring the *4 completely -- the constraint manager
202 // would see x < 8 and then declare the assertion to be known false.
206 clang_analyzer_eval(x
== 3); // expected-warning{{UNKNOWN}}
209 void additiveSymSymFolding(int x
, int y
) {
210 // We should simplify 'x - 1' to '0' and handle the comparison,
211 // despite both sides being complicated symbols.
215 clang_analyzer_eval(z
<= y
); // expected-warning{{TRUE}}