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