1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,debug.ExprInspection %s -std=c++11 -verify
2 
3 void clang_analyzer_eval(bool);
4 void clang_analyzer_warnIfReached();
5 
testAddressof(int x)6 void testAddressof(int x) {
7   clang_analyzer_eval(&x == __builtin_addressof(x)); // expected-warning{{TRUE}}
8 }
9 
testSize()10 void testSize() {
11   struct {
12     int x;
13     int y;
14     char z;
15   } object;
16   clang_analyzer_eval(__builtin_object_size(&object.y, 0) == sizeof(object) - sizeof(int)); // expected-warning{{TRUE}}
17 
18   // Clang can't actually evaluate these builtin "calls", but importantly they don't actually evaluate the argument expression either.
19   int i = 0;
20   char buf[10];
21   clang_analyzer_eval(__builtin_object_size(&buf[i++], 0) == sizeof(buf)); // expected-warning{{FALSE}}
22   clang_analyzer_eval(__builtin_object_size(&buf[++i], 0) == sizeof(buf) - 1); // expected-warning{{FALSE}}
23 
24   clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
25 }
26 
test_assume_aligned_1(char * p)27 void test_assume_aligned_1(char *p) {
28   char *q;
29 
30   q = (char*) __builtin_assume_aligned(p, 16);
31   clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
32 }
33 
test_assume_aligned_2(char * p)34 void test_assume_aligned_2(char *p) {
35   char *q;
36 
37   q = (char*) __builtin_assume_aligned(p, 16, 8);
38   clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
39 }
40 
test_assume_aligned_3(char * p)41 void test_assume_aligned_3(char *p) {
42   void *q;
43 
44   q = __builtin_assume_aligned(p, 16, 8);
45   clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
46 }
47 
test_assume_aligned_4(char * p)48 void test_assume_aligned_4(char *p) {
49   char *q;
50 
51   q = (char*) __builtin_assume_aligned(p + 1, 16);
52   clang_analyzer_eval(p == q); // expected-warning{{FALSE}}
53 }
54 
f(int i)55 void f(int i) {
56   __builtin_assume(i < 10);
57   clang_analyzer_eval(i < 15); // expected-warning {{TRUE}}
58 }
59 
g(int i)60 void g(int i) {
61   if (i > 5) {
62     __builtin_assume(i < 5);
63     clang_analyzer_warnIfReached(); // Assumtion contradicts constraints.
64                                     // We give up the analysis on this path.
65   }
66 }
67 
test_constant_p(void * ptr)68 void test_constant_p(void *ptr) {
69   int i = 1;
70   const int j = 2;
71   constexpr int k = 3;
72   clang_analyzer_eval(__builtin_constant_p(42) == 1); // expected-warning {{TRUE}}
73   clang_analyzer_eval(__builtin_constant_p(i) == 0); // expected-warning {{TRUE}}
74   clang_analyzer_eval(__builtin_constant_p(j) == 1); // expected-warning {{TRUE}}
75   clang_analyzer_eval(__builtin_constant_p(k) == 1); // expected-warning {{TRUE}}
76   clang_analyzer_eval(__builtin_constant_p(i + 42) == 0); // expected-warning {{TRUE}}
77   clang_analyzer_eval(__builtin_constant_p(j + 42) == 1); // expected-warning {{TRUE}}
78   clang_analyzer_eval(__builtin_constant_p(k + 42) == 1); // expected-warning {{TRUE}}
79   clang_analyzer_eval(__builtin_constant_p(" ") == 1); // expected-warning {{TRUE}}
80   clang_analyzer_eval(__builtin_constant_p(test_constant_p) == 0); // expected-warning {{TRUE}}
81   clang_analyzer_eval(__builtin_constant_p(k - 3) == 0); // expected-warning {{FALSE}}
82   clang_analyzer_eval(__builtin_constant_p(k - 3) == 1); // expected-warning {{TRUE}}
83   clang_analyzer_eval(__builtin_constant_p(ptr == 0)); // expected-warning {{FALSE}}
84 }
85