1*160798abSGabor Marton // RUN: %clang_analyze_cc1 %s \
2*160798abSGabor Marton // RUN:   -analyzer-checker=core \
3*160798abSGabor Marton // RUN:   -analyzer-checker=debug.ExprInspection \
4*160798abSGabor Marton // RUN:   -analyzer-config support-symbolic-integer-casts=true \
5*160798abSGabor Marton // RUN:   -analyzer-config eagerly-assume=false \
6*160798abSGabor Marton // RUN:   -triple x86_64-unknown-linux-gnu \
7*160798abSGabor Marton // RUN:   -verify
8*160798abSGabor Marton 
9*160798abSGabor Marton // Test that the SValBuilder is able to look up and use a constraint for an
10*160798abSGabor Marton // operand of a SymbolCast, when the operand is constrained to a const value.
11*160798abSGabor Marton 
12*160798abSGabor Marton void clang_analyzer_eval(bool);
13*160798abSGabor Marton 
14*160798abSGabor Marton extern void abort() __attribute__((__noreturn__));
15*160798abSGabor Marton #define assert(expr) ((expr) ? (void)(0) : abort())
16*160798abSGabor Marton 
test(int x)17*160798abSGabor Marton void test(int x) {
18*160798abSGabor Marton   // Constrain a SymSymExpr to a constant value.
19*160798abSGabor Marton   assert(x * x == 1);
20*160798abSGabor Marton   // It is expected to be able to get the constraint for the operand of the
21*160798abSGabor Marton   // cast.
22*160798abSGabor Marton   clang_analyzer_eval((char)(x * x) == 1); // expected-warning{{TRUE}}
23*160798abSGabor Marton   clang_analyzer_eval((long)(x * x) == 1); // expected-warning{{TRUE}}
24*160798abSGabor Marton }
25*160798abSGabor Marton 
test1(int x,int y)26*160798abSGabor Marton void test1(int x, int y) {
27*160798abSGabor Marton   // Even if two lower bytes of `x` equal to zero, it doesn't mean that
28*160798abSGabor Marton   // the entire `x` is zero. We are not able to know the exact value of x.
29*160798abSGabor Marton   // It can be one of  65536 possible values like
30*160798abSGabor Marton   // [0, 65536, -65536, 131072, -131072, ...]. To avoid huge range sets we
31*160798abSGabor Marton   // still assume `x` in the range [INT_MIN, INT_MAX].
32*160798abSGabor Marton   assert((short)x == 0); // Lower two bytes are set to 0.
33*160798abSGabor Marton 
34*160798abSGabor Marton   static_assert((short)65536 == 0, "");
35*160798abSGabor Marton   static_assert((short)-65536 == 0, "");
36*160798abSGabor Marton   static_assert((short)131072 == 0, "");
37*160798abSGabor Marton   static_assert((short)-131072 == 0, "");
38*160798abSGabor Marton   clang_analyzer_eval(x == 0);       // expected-warning{{UNKNOWN}}
39*160798abSGabor Marton 
40*160798abSGabor Marton   // These are not truncated to short as zero.
41*160798abSGabor Marton   static_assert((short)1 != 0, "");
42*160798abSGabor Marton   clang_analyzer_eval(x == 1);       // expected-warning{{FALSE}}
43*160798abSGabor Marton   static_assert((short)-1 != 0, "");
44*160798abSGabor Marton   clang_analyzer_eval(x == -1);      // expected-warning{{FALSE}}
45*160798abSGabor Marton   static_assert((short)65537 != 0, "");
46*160798abSGabor Marton   clang_analyzer_eval(x == 65537);   // expected-warning{{FALSE}}
47*160798abSGabor Marton   static_assert((short)-65537 != 0, "");
48*160798abSGabor Marton   clang_analyzer_eval(x == -65537);  // expected-warning{{FALSE}}
49*160798abSGabor Marton   static_assert((short)131073 != 0, "");
50*160798abSGabor Marton   clang_analyzer_eval(x == 131073);  // expected-warning{{FALSE}}
51*160798abSGabor Marton   static_assert((short)-131073 != 0, "");
52*160798abSGabor Marton   clang_analyzer_eval(x == -131073); // expected-warning{{FALSE}}
53*160798abSGabor Marton 
54*160798abSGabor Marton   // Check for implicit cast.
55*160798abSGabor Marton   short s = y;
56*160798abSGabor Marton   assert(s == 0);
57*160798abSGabor Marton   clang_analyzer_eval(y == 0); // expected-warning{{UNKNOWN}}
58*160798abSGabor Marton }
59