2018-08-30 04:29:17 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -Wno-tautological-compare -analyzer-config eagerly-assume=false %s
|
2012-05-17 00:01:10 +08:00
|
|
|
|
|
|
|
void clang_analyzer_eval(bool);
|
|
|
|
|
2012-05-03 15:33:56 +08:00
|
|
|
#define UINT_MAX (~0U)
|
2012-05-08 11:27:16 +08:00
|
|
|
#define INT_MAX (UINT_MAX & (UINT_MAX >> 1))
|
|
|
|
#define INT_MIN (-INT_MAX - 1)
|
2010-06-19 06:49:11 +08:00
|
|
|
|
|
|
|
//---------------
|
|
|
|
// Plus/minus
|
|
|
|
//---------------
|
|
|
|
|
|
|
|
void separateExpressions (int a) {
|
|
|
|
int b = a + 1;
|
|
|
|
--b;
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != 0 && b == 0); // expected-warning{{FALSE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void oneLongExpression (int a) {
|
|
|
|
// Expression canonicalization should still allow this to work, even though
|
|
|
|
// the first term is on the left.
|
|
|
|
int b = 15 + a + 15 - 10 - 20;
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != 0 && b == 0); // expected-warning{{FALSE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
2010-06-22 04:15:15 +08:00
|
|
|
void mixedTypes (int a) {
|
|
|
|
// Different additive types should not cause crashes when constant-folding.
|
|
|
|
// This is part of PR7406.
|
|
|
|
int b = a + 1LL;
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != 0 && (b-1) == 0); // not crash, expected-warning{{FALSE}}
|
2010-06-22 04:15:15 +08:00
|
|
|
|
|
|
|
int c = a + 1U;
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != 0 && (c-1) == 0); // not crash, expected-warning{{FALSE}}
|
2010-06-22 04:15:15 +08:00
|
|
|
}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
//---------------
|
|
|
|
// Comparisons
|
|
|
|
//---------------
|
|
|
|
|
|
|
|
// Equality and inequality only
|
|
|
|
void eq_ne (unsigned a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
if (a == UINT_MAX) {
|
|
|
|
clang_analyzer_eval(a+1 == 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a-1 == UINT_MAX-1); // expected-warning{{TRUE}}
|
|
|
|
} else {
|
|
|
|
clang_analyzer_eval(a+1 != 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a-1 != UINT_MAX-1); // expected-warning{{TRUE}}
|
|
|
|
}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
2010-06-22 04:15:15 +08:00
|
|
|
// Mixed typed inequalities (part of PR7406)
|
|
|
|
// These should not crash.
|
|
|
|
void mixed_eq_ne (int a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
if (a == 1) {
|
|
|
|
clang_analyzer_eval(a+1U == 2); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a-1U == 0); // expected-warning{{TRUE}}
|
|
|
|
} else {
|
|
|
|
clang_analyzer_eval(a+1U != 2); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a-1U != 0); // expected-warning{{TRUE}}
|
|
|
|
}
|
2010-06-22 04:15:15 +08:00
|
|
|
}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
|
|
|
|
// Simple order comparisons with no adjustment
|
|
|
|
void baselineGT (unsigned a) {
|
|
|
|
if (a > 0)
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != 0); // expected-warning{{TRUE}}
|
|
|
|
else
|
|
|
|
clang_analyzer_eval(a == 0); // expected-warning{{TRUE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void baselineGE (unsigned a) {
|
|
|
|
if (a >= UINT_MAX)
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a == UINT_MAX); // expected-warning{{TRUE}}
|
|
|
|
else
|
|
|
|
clang_analyzer_eval(a != UINT_MAX); // expected-warning{{TRUE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void baselineLT (unsigned a) {
|
|
|
|
if (a < UINT_MAX)
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != UINT_MAX); // expected-warning{{TRUE}}
|
|
|
|
else
|
|
|
|
clang_analyzer_eval(a == UINT_MAX); // expected-warning{{TRUE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void baselineLE (unsigned a) {
|
|
|
|
if (a <= 0)
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a == 0); // expected-warning{{TRUE}}
|
|
|
|
else
|
|
|
|
clang_analyzer_eval(a != 0); // expected-warning{{TRUE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Adjustment gives each of these an extra solution!
|
|
|
|
void adjustedGT (unsigned a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a-1 > UINT_MAX-1); // expected-warning{{UNKNOWN}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void adjustedGE (unsigned a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a-1 > UINT_MAX-1); // expected-warning{{UNKNOWN}}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
if (a-1 >= UINT_MAX-1)
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a == UINT_MAX); // expected-warning{{UNKNOWN}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void adjustedLT (unsigned a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a+1 < 1); // expected-warning{{UNKNOWN}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void adjustedLE (unsigned a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a+1 <= 1); // expected-warning{{UNKNOWN}}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
if (a+1 <= 1)
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tautologies
|
2012-05-17 00:01:10 +08:00
|
|
|
// The negative forms are exercised as well
|
|
|
|
// because clang_analyzer_eval tests both possibilities.
|
|
|
|
void tautologies(unsigned a) {
|
|
|
|
clang_analyzer_eval(a <= UINT_MAX); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a >= 0); // expected-warning{{TRUE}}
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
2012-05-03 15:34:01 +08:00
|
|
|
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
// Tautologies from outside the range of the symbol
|
2012-05-17 00:01:10 +08:00
|
|
|
void tautologiesOutside(unsigned char a) {
|
2012-11-15 06:50:24 +08:00
|
|
|
clang_analyzer_eval(a <= 0x100); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a < 0x100); // expected-warning{{TRUE}}
|
2012-05-08 11:27:16 +08:00
|
|
|
|
2012-11-15 06:50:24 +08:00
|
|
|
clang_analyzer_eval(a != 0x100); // expected-warning{{TRUE}}
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a != -1); // expected-warning{{TRUE}}
|
2012-05-08 11:27:16 +08:00
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a > -1); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a >= -1); // expected-warning{{TRUE}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wraparound with mixed types. Note that the analyzer assumes
|
|
|
|
// -fwrapv semantics.
|
|
|
|
void mixedWraparoundSanityCheck(int a) {
|
|
|
|
int max = INT_MAX;
|
|
|
|
int min = INT_MIN;
|
|
|
|
|
|
|
|
int b = a + 1;
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a == max && b != min); // expected-warning{{FALSE}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
void mixedWraparoundLE_GT(int a) {
|
2012-05-08 11:27:16 +08:00
|
|
|
int max = INT_MAX;
|
|
|
|
int min = INT_MIN;
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((a + 2) <= (max + 1LL)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a - 2) > (min - 1LL)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a + 2LL) <= max); // expected-warning{{UNKNOWN}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
void mixedWraparoundGE_LT(int a) {
|
2012-05-08 11:27:16 +08:00
|
|
|
int max = INT_MAX;
|
|
|
|
int min = INT_MIN;
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((a + 2) < (max + 1LL)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a - 2) >= (min - 1LL)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a - 2LL) >= min); // expected-warning{{UNKNOWN}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
void mixedWraparoundEQ_NE(int a) {
|
2012-05-08 11:27:16 +08:00
|
|
|
int max = INT_MAX;
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((a + 2) != (max + 1LL)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a + 2LL) == (max + 1LL)); // expected-warning{{UNKNOWN}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Mixed-signedness comparisons.
|
|
|
|
void mixedSignedness(int a, unsigned b) {
|
|
|
|
int sMin = INT_MIN;
|
|
|
|
unsigned uMin = INT_MIN;
|
2012-05-17 00:01:10 +08:00
|
|
|
|
|
|
|
clang_analyzer_eval(a == sMin && a != uMin); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval(b == uMin && b != sMin); // expected-warning{{FALSE}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 09:21:33 +08:00
|
|
|
void mixedSignedness2(int a) {
|
|
|
|
if (a != -1)
|
|
|
|
return;
|
|
|
|
clang_analyzer_eval(a == UINT_MAX); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mixedSignedness3(unsigned a) {
|
|
|
|
if (a != UINT_MAX)
|
|
|
|
return;
|
|
|
|
clang_analyzer_eval(a == -1); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
|
|
|
|
void multiplicativeSanityTest(int x) {
|
|
|
|
// At one point we were ignoring the *4 completely -- the constraint manager
|
2012-05-17 00:01:10 +08:00
|
|
|
// would see x < 8 and then declare the assertion to be known false.
|
2012-05-08 11:27:16 +08:00
|
|
|
if (x*4 < 8)
|
|
|
|
return;
|
2012-05-17 00:01:10 +08:00
|
|
|
|
|
|
|
clang_analyzer_eval(x == 3); // expected-warning{{UNKNOWN}}
|
2012-05-08 11:27:16 +08:00
|
|
|
}
|
2017-04-13 15:20:04 +08:00
|
|
|
|
|
|
|
void additiveSymSymFolding(int x, int y) {
|
|
|
|
// We should simplify 'x - 1' to '0' and handle the comparison,
|
|
|
|
// despite both sides being complicated symbols.
|
|
|
|
int z = x - 1;
|
|
|
|
if (x == 1)
|
|
|
|
if (y >= 0)
|
|
|
|
clang_analyzer_eval(z <= y); // expected-warning{{TRUE}}
|
|
|
|
}
|