blob: 0d749ec3cbaf080966a33b31ee8ca3f96a440e4b [file] [log] [blame]
Jordy Rose43d9f0d2012-05-16 16:01:10 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify -analyzer-constraints=basic -Wno-tautological-compare %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify -analyzer-constraints=range -Wno-tautological-compare %s
Jordy Roseba0f61c2010-06-18 22:49:11 +00003
Jordy Rose43d9f0d2012-05-16 16:01:10 +00004void clang_analyzer_eval(bool);
5
Jordy Rose9e607dd2012-05-03 07:33:56 +00006#define UINT_MAX (~0U)
Jordy Rose1d8db492012-05-08 03:27:16 +00007#define INT_MAX (UINT_MAX & (UINT_MAX >> 1))
8#define INT_MIN (-INT_MAX - 1)
Jordy Roseba0f61c2010-06-18 22:49:11 +00009
10//---------------
11// Plus/minus
12//---------------
13
14void separateExpressions (int a) {
15 int b = a + 1;
16 --b;
17
Jordy Rose43d9f0d2012-05-16 16:01:10 +000018 clang_analyzer_eval(a != 0 && b == 0); // expected-warning{{FALSE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +000019}
20
21void oneLongExpression (int a) {
22 // Expression canonicalization should still allow this to work, even though
23 // the first term is on the left.
24 int b = 15 + a + 15 - 10 - 20;
25
Jordy Rose43d9f0d2012-05-16 16:01:10 +000026 clang_analyzer_eval(a != 0 && b == 0); // expected-warning{{FALSE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +000027}
28
Jordy Roseb4954a42010-06-21 20:15:15 +000029void mixedTypes (int a) {
Jordy Roseb4954a42010-06-21 20:15:15 +000030 // Different additive types should not cause crashes when constant-folding.
31 // This is part of PR7406.
32 int b = a + 1LL;
Jordy Rose43d9f0d2012-05-16 16:01:10 +000033 clang_analyzer_eval(a != 0 && (b-1) == 0); // not crash, expected-warning{{FALSE}}
Jordy Roseb4954a42010-06-21 20:15:15 +000034
35 int c = a + 1U;
Jordy Rose43d9f0d2012-05-16 16:01:10 +000036 clang_analyzer_eval(a != 0 && (c-1) == 0); // not crash, expected-warning{{FALSE}}
Jordy Roseb4954a42010-06-21 20:15:15 +000037}
38
Jordy Roseba0f61c2010-06-18 22:49:11 +000039//---------------
40// Comparisons
41//---------------
42
43// Equality and inequality only
44void eq_ne (unsigned a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000045 if (a == UINT_MAX) {
46 clang_analyzer_eval(a+1 == 0); // expected-warning{{TRUE}}
47 clang_analyzer_eval(a-1 == UINT_MAX-1); // expected-warning{{TRUE}}
48 } else {
49 clang_analyzer_eval(a+1 != 0); // expected-warning{{TRUE}}
50 clang_analyzer_eval(a-1 != UINT_MAX-1); // expected-warning{{TRUE}}
51 }
Jordy Roseba0f61c2010-06-18 22:49:11 +000052}
53
Jordy Roseb4954a42010-06-21 20:15:15 +000054// Mixed typed inequalities (part of PR7406)
55// These should not crash.
56void mixed_eq_ne (int a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000057 if (a == 1) {
58 clang_analyzer_eval(a+1U == 2); // expected-warning{{TRUE}}
59 clang_analyzer_eval(a-1U == 0); // expected-warning{{TRUE}}
60 } else {
61 clang_analyzer_eval(a+1U != 2); // expected-warning{{TRUE}}
62 clang_analyzer_eval(a-1U != 0); // expected-warning{{TRUE}}
63 }
Jordy Roseb4954a42010-06-21 20:15:15 +000064}
65
Jordy Roseba0f61c2010-06-18 22:49:11 +000066
67// Simple order comparisons with no adjustment
68void baselineGT (unsigned a) {
Jordy Roseba0f61c2010-06-18 22:49:11 +000069 if (a > 0)
Jordy Rose43d9f0d2012-05-16 16:01:10 +000070 clang_analyzer_eval(a != 0); // expected-warning{{TRUE}}
71 else
72 clang_analyzer_eval(a == 0); // expected-warning{{TRUE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +000073}
74
75void baselineGE (unsigned a) {
Jordy Roseba0f61c2010-06-18 22:49:11 +000076 if (a >= UINT_MAX)
Jordy Rose43d9f0d2012-05-16 16:01:10 +000077 clang_analyzer_eval(a == UINT_MAX); // expected-warning{{TRUE}}
78 else
79 clang_analyzer_eval(a != UINT_MAX); // expected-warning{{TRUE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +000080}
81
82void baselineLT (unsigned a) {
Jordy Roseba0f61c2010-06-18 22:49:11 +000083 if (a < UINT_MAX)
Jordy Rose43d9f0d2012-05-16 16:01:10 +000084 clang_analyzer_eval(a != UINT_MAX); // expected-warning{{TRUE}}
85 else
86 clang_analyzer_eval(a == UINT_MAX); // expected-warning{{TRUE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +000087}
88
89void baselineLE (unsigned a) {
Jordy Roseba0f61c2010-06-18 22:49:11 +000090 if (a <= 0)
Jordy Rose43d9f0d2012-05-16 16:01:10 +000091 clang_analyzer_eval(a == 0); // expected-warning{{TRUE}}
92 else
93 clang_analyzer_eval(a != 0); // expected-warning{{TRUE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +000094}
95
96
97// Adjustment gives each of these an extra solution!
98void adjustedGT (unsigned a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000099 clang_analyzer_eval(a-1 > UINT_MAX-1); // expected-warning{{UNKNOWN}}
Jordy Roseba0f61c2010-06-18 22:49:11 +0000100}
101
102void adjustedGE (unsigned a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000103 clang_analyzer_eval(a-1 > UINT_MAX-1); // expected-warning{{UNKNOWN}}
104
Jordy Roseba0f61c2010-06-18 22:49:11 +0000105 if (a-1 >= UINT_MAX-1)
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000106 clang_analyzer_eval(a == UINT_MAX); // expected-warning{{UNKNOWN}}
Jordy Roseba0f61c2010-06-18 22:49:11 +0000107}
108
109void adjustedLT (unsigned a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000110 clang_analyzer_eval(a+1 < 1); // expected-warning{{UNKNOWN}}
Jordy Roseba0f61c2010-06-18 22:49:11 +0000111}
112
113void adjustedLE (unsigned a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000114 clang_analyzer_eval(a+1 <= 1); // expected-warning{{UNKNOWN}}
115
Jordy Roseba0f61c2010-06-18 22:49:11 +0000116 if (a+1 <= 1)
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000117 clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
Jordy Roseba0f61c2010-06-18 22:49:11 +0000118}
119
120
121// Tautologies
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000122// The negative forms are exercised as well
123// because clang_analyzer_eval tests both possibilities.
124void tautologies(unsigned a) {
125 clang_analyzer_eval(a <= UINT_MAX); // expected-warning{{TRUE}}
126 clang_analyzer_eval(a >= 0); // expected-warning{{TRUE}}
Jordy Roseba0f61c2010-06-18 22:49:11 +0000127}
Jordy Rose14d20b12012-05-03 07:34:01 +0000128
129
Jordy Rose1d8db492012-05-08 03:27:16 +0000130// Tautologies from outside the range of the symbol
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000131void tautologiesOutside(unsigned char a) {
132 clang_analyzer_eval(a <= 0x100); // expected-warning{{TRUE}}
133 clang_analyzer_eval(a < 0x100); // expected-warning{{TRUE}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000134
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000135 clang_analyzer_eval(a != 0x100); // expected-warning{{TRUE}}
136 clang_analyzer_eval(a != -1); // expected-warning{{TRUE}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000137
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000138 clang_analyzer_eval(a > -1); // expected-warning{{TRUE}}
139 clang_analyzer_eval(a >= -1); // expected-warning{{TRUE}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000140}
141
142
143// Wraparound with mixed types. Note that the analyzer assumes
144// -fwrapv semantics.
145void mixedWraparoundSanityCheck(int a) {
146 int max = INT_MAX;
147 int min = INT_MIN;
148
149 int b = a + 1;
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000150 clang_analyzer_eval(a == max && b != min); // expected-warning{{FALSE}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000151}
152
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000153void mixedWraparoundLE_GT(int a) {
Jordy Rose1d8db492012-05-08 03:27:16 +0000154 int max = INT_MAX;
155 int min = INT_MIN;
156
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000157 clang_analyzer_eval((a + 2) <= (max + 1LL)); // expected-warning{{TRUE}}
158 clang_analyzer_eval((a - 2) > (min - 1LL)); // expected-warning{{TRUE}}
159 clang_analyzer_eval((a + 2LL) <= max); // expected-warning{{UNKNOWN}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000160}
161
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000162void mixedWraparoundGE_LT(int a) {
Jordy Rose1d8db492012-05-08 03:27:16 +0000163 int max = INT_MAX;
164 int min = INT_MIN;
165
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000166 clang_analyzer_eval((a + 2) < (max + 1LL)); // expected-warning{{TRUE}}
167 clang_analyzer_eval((a - 2) >= (min - 1LL)); // expected-warning{{TRUE}}
168 clang_analyzer_eval((a - 2LL) >= min); // expected-warning{{UNKNOWN}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000169}
170
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000171void mixedWraparoundEQ_NE(int a) {
Jordy Rose1d8db492012-05-08 03:27:16 +0000172 int max = INT_MAX;
173
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000174 clang_analyzer_eval((a + 2) != (max + 1LL)); // expected-warning{{TRUE}}
175 clang_analyzer_eval((a + 2LL) == (max + 1LL)); // expected-warning{{UNKNOWN}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000176}
177
178
179// Mixed-signedness comparisons.
180void mixedSignedness(int a, unsigned b) {
181 int sMin = INT_MIN;
182 unsigned uMin = INT_MIN;
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000183
184 clang_analyzer_eval(a == sMin && a != uMin); // expected-warning{{FALSE}}
185 clang_analyzer_eval(b == uMin && b != sMin); // expected-warning{{FALSE}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000186}
187
188
Jordy Rose1d8db492012-05-08 03:27:16 +0000189void multiplicativeSanityTest(int x) {
190 // At one point we were ignoring the *4 completely -- the constraint manager
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000191 // would see x < 8 and then declare the assertion to be known false.
Jordy Rose1d8db492012-05-08 03:27:16 +0000192 if (x*4 < 8)
193 return;
Jordy Rose43d9f0d2012-05-16 16:01:10 +0000194
195 clang_analyzer_eval(x == 3); // expected-warning{{UNKNOWN}}
Jordy Rose1d8db492012-05-08 03:27:16 +0000196}