1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.cstring,debug.ExprInspection -analyzer-constraints=range -triple i386-apple-darwin9 -verify %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.cstring,debug.ExprInspection -analyzer-constraints=range -triple x86_64-apple-darwin9 -verify %s
3*f4a2713aSLionel Sambuc
4*f4a2713aSLionel Sambuc // This file runs in C++ mode so that the comparison type is 'bool', not 'int'.
5*f4a2713aSLionel Sambuc void clang_analyzer_eval(int);
6*f4a2713aSLionel Sambuc typedef typeof(sizeof(int)) size_t;
7*f4a2713aSLionel Sambuc
8*f4a2713aSLionel Sambuc // PR12206/12510 - When SimpleSValBuilder figures out that a symbol is fully
9*f4a2713aSLionel Sambuc // constrained, it should cast the value to the result type in a binary
10*f4a2713aSLionel Sambuc // operation...unless the binary operation is a comparison, in which case the
11*f4a2713aSLionel Sambuc // two arguments should be the same type, but won't match the result type.
12*f4a2713aSLionel Sambuc //
13*f4a2713aSLionel Sambuc // This is not directly related to additive folding, but we use SValBuilder's
14*f4a2713aSLionel Sambuc // additive folding to tickle the bug. ExprEngine will simplify fully-constrained
15*f4a2713aSLionel Sambuc // symbols, so SValBuilder will only see them if they are (a) part of an evaluated
16*f4a2713aSLionel Sambuc // SymExpr (e.g. with additive folding) or (b) generated by a checker (e.g.
17*f4a2713aSLionel Sambuc // unix.cstring's strlen() modelling).
PR12206(int x)18*f4a2713aSLionel Sambuc void PR12206(int x) {
19*f4a2713aSLionel Sambuc size_t comparisonSize = sizeof(1 == 1);
20*f4a2713aSLionel Sambuc
21*f4a2713aSLionel Sambuc // Sanity check. This test is useless if size_t isn't bigger than bool.
22*f4a2713aSLionel Sambuc clang_analyzer_eval(sizeof(size_t) > comparisonSize); // expected-warning{{TRUE}}
23*f4a2713aSLionel Sambuc
24*f4a2713aSLionel Sambuc // Build a SymIntExpr, dependent on x.
25*f4a2713aSLionel Sambuc int local = x - 1;
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc // Create a value that requires more bits to store than a comparison result.
28*f4a2713aSLionel Sambuc int value = 1;
29*f4a2713aSLionel Sambuc value <<= 8 * comparisonSize;
30*f4a2713aSLionel Sambuc value += 1;
31*f4a2713aSLionel Sambuc
32*f4a2713aSLionel Sambuc // Constrain the value of x.
33*f4a2713aSLionel Sambuc if (x != value) return;
34*f4a2713aSLionel Sambuc
35*f4a2713aSLionel Sambuc // Constant-folding will turn (local+1) back into the symbol for x.
36*f4a2713aSLionel Sambuc // The point of this dance is to make SValBuilder be responsible for
37*f4a2713aSLionel Sambuc // turning the symbol into a ConcreteInt, rather than ExprEngine.
38*f4a2713aSLionel Sambuc
39*f4a2713aSLionel Sambuc // Test relational operators.
40*f4a2713aSLionel Sambuc clang_analyzer_eval((local + 1) >= 2); // expected-warning{{TRUE}}
41*f4a2713aSLionel Sambuc clang_analyzer_eval(2 <= (local + 1)); // expected-warning{{TRUE}}
42*f4a2713aSLionel Sambuc
43*f4a2713aSLionel Sambuc // Test equality operators.
44*f4a2713aSLionel Sambuc clang_analyzer_eval((local + 1) != 1); // expected-warning{{TRUE}}
45*f4a2713aSLionel Sambuc clang_analyzer_eval(1 != (local + 1)); // expected-warning{{TRUE}}
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc
PR12206_truncation(signed char x)48*f4a2713aSLionel Sambuc void PR12206_truncation(signed char x) {
49*f4a2713aSLionel Sambuc // Build a SymIntExpr, dependent on x.
50*f4a2713aSLionel Sambuc signed char local = x - 1;
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuc // Constrain the value of x.
53*f4a2713aSLionel Sambuc if (x != 1) return;
54*f4a2713aSLionel Sambuc
55*f4a2713aSLionel Sambuc // Constant-folding will turn (local+1) back into the symbol for x.
56*f4a2713aSLionel Sambuc // The point of this dance is to make SValBuilder be responsible for
57*f4a2713aSLionel Sambuc // turning the symbol into a ConcreteInt, rather than ExprEngine.
58*f4a2713aSLionel Sambuc
59*f4a2713aSLionel Sambuc // Construct a value that cannot be represented by 'char',
60*f4a2713aSLionel Sambuc // but that has the same lower bits as x.
61*f4a2713aSLionel Sambuc signed int value = 1 + (1 << 8);
62*f4a2713aSLionel Sambuc
63*f4a2713aSLionel Sambuc // Test relational operators.
64*f4a2713aSLionel Sambuc clang_analyzer_eval((local + 1) < value); // expected-warning{{TRUE}}
65*f4a2713aSLionel Sambuc clang_analyzer_eval(value > (local + 1)); // expected-warning{{TRUE}}
66*f4a2713aSLionel Sambuc
67*f4a2713aSLionel Sambuc // Test equality operators.
68*f4a2713aSLionel Sambuc clang_analyzer_eval((local + 1) != value); // expected-warning{{TRUE}}
69*f4a2713aSLionel Sambuc clang_analyzer_eval(value != (local + 1)); // expected-warning{{TRUE}}
70*f4a2713aSLionel Sambuc }
71*f4a2713aSLionel Sambuc
72*f4a2713aSLionel Sambuc // This test is insurance in case we significantly change how SymExprs are
73*f4a2713aSLionel Sambuc // evaluated.
74*f4a2713aSLionel Sambuc size_t strlen(const char *s);
PR12206_strlen(const char * x)75*f4a2713aSLionel Sambuc void PR12206_strlen(const char *x) {
76*f4a2713aSLionel Sambuc size_t comparisonSize = sizeof(1 == 1);
77*f4a2713aSLionel Sambuc
78*f4a2713aSLionel Sambuc // Sanity check. This test is useless if size_t isn't bigger than bool.
79*f4a2713aSLionel Sambuc clang_analyzer_eval(sizeof(size_t) > comparisonSize); // expected-warning{{TRUE}}
80*f4a2713aSLionel Sambuc
81*f4a2713aSLionel Sambuc // Create a value that requires more bits to store than a comparison result.
82*f4a2713aSLionel Sambuc size_t value = 1UL;
83*f4a2713aSLionel Sambuc value <<= 8 * comparisonSize;
84*f4a2713aSLionel Sambuc value += 1;
85*f4a2713aSLionel Sambuc
86*f4a2713aSLionel Sambuc // Constrain the length of x.
87*f4a2713aSLionel Sambuc if (strlen(x) != value) return;
88*f4a2713aSLionel Sambuc
89*f4a2713aSLionel Sambuc // Test relational operators.
90*f4a2713aSLionel Sambuc clang_analyzer_eval(strlen(x) >= 2); // expected-warning{{TRUE}}
91*f4a2713aSLionel Sambuc clang_analyzer_eval(2 <= strlen(x)); // expected-warning{{TRUE}}
92*f4a2713aSLionel Sambuc
93*f4a2713aSLionel Sambuc // Test equality operators.
94*f4a2713aSLionel Sambuc clang_analyzer_eval(strlen(x) != 1); // expected-warning{{TRUE}}
95*f4a2713aSLionel Sambuc clang_analyzer_eval(1 != strlen(x)); // expected-warning{{TRUE}}
96*f4a2713aSLionel Sambuc }
97