xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/call-invalidation.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc void clang_analyzer_eval(bool);
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc void usePointer(int * const *);
6f4a2713aSLionel Sambuc void useReference(int * const &);
7f4a2713aSLionel Sambuc 
testPointer()8f4a2713aSLionel Sambuc void testPointer() {
9f4a2713aSLionel Sambuc   int x;
10f4a2713aSLionel Sambuc   int *p;
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc   p = &x;
13f4a2713aSLionel Sambuc   x = 42;
14f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
15f4a2713aSLionel Sambuc   usePointer(&p);
16f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc   p = &x;
19f4a2713aSLionel Sambuc   x = 42;
20f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
21f4a2713aSLionel Sambuc   useReference(p);
22f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc   int * const cp1 = &x;
25f4a2713aSLionel Sambuc   x = 42;
26f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
27f4a2713aSLionel Sambuc   usePointer(&cp1);
28f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc   int * const cp2 = &x;
31f4a2713aSLionel Sambuc   x = 42;
32f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
33f4a2713aSLionel Sambuc   useReference(cp2);
34f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc struct Wrapper {
39f4a2713aSLionel Sambuc   int *ptr;
40f4a2713aSLionel Sambuc };
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc void useStruct(Wrapper &w);
43f4a2713aSLionel Sambuc void useConstStruct(const Wrapper &w);
44f4a2713aSLionel Sambuc 
testPointerStruct()45f4a2713aSLionel Sambuc void testPointerStruct() {
46f4a2713aSLionel Sambuc   int x;
47f4a2713aSLionel Sambuc   Wrapper w;
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   w.ptr = &x;
50f4a2713aSLionel Sambuc   x = 42;
51f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
52f4a2713aSLionel Sambuc   useStruct(w);
53f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc   w.ptr = &x;
56f4a2713aSLionel Sambuc   x = 42;
57f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
58f4a2713aSLionel Sambuc   useConstStruct(w);
59f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc struct RefWrapper {
64f4a2713aSLionel Sambuc   int &ref;
65f4a2713aSLionel Sambuc };
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc void useStruct(RefWrapper &w);
68f4a2713aSLionel Sambuc void useConstStruct(const RefWrapper &w);
69f4a2713aSLionel Sambuc 
testReferenceStruct()70f4a2713aSLionel Sambuc void testReferenceStruct() {
71f4a2713aSLionel Sambuc   int x;
72f4a2713aSLionel Sambuc   RefWrapper w = { x };
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   x = 42;
75f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
76f4a2713aSLionel Sambuc   useStruct(w);
77f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc // FIXME: This test is split into two functions because region invalidation
81f4a2713aSLionel Sambuc // does not preserve reference bindings. <rdar://problem/13320347>
testConstReferenceStruct()82f4a2713aSLionel Sambuc void testConstReferenceStruct() {
83f4a2713aSLionel Sambuc   int x;
84f4a2713aSLionel Sambuc   RefWrapper w = { x };
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   x = 42;
87f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
88f4a2713aSLionel Sambuc   useConstStruct(w);
89f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc void usePointerPure(int * const *) __attribute__((pure));
94*0a6a1f1dSLionel Sambuc void usePointerConst(int * const *) __attribute__((const));
95*0a6a1f1dSLionel Sambuc 
testPureConst()96*0a6a1f1dSLionel Sambuc void testPureConst() {
97*0a6a1f1dSLionel Sambuc   extern int global;
98*0a6a1f1dSLionel Sambuc   int x;
99*0a6a1f1dSLionel Sambuc   int *p;
100*0a6a1f1dSLionel Sambuc 
101*0a6a1f1dSLionel Sambuc   p = &x;
102*0a6a1f1dSLionel Sambuc   x = 42;
103*0a6a1f1dSLionel Sambuc   global = -5;
104*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
105*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc   usePointerPure(&p);
108*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
109*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
110*0a6a1f1dSLionel Sambuc 
111*0a6a1f1dSLionel Sambuc   usePointerConst(&p);
112*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
113*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc   usePointer(&p);
116*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
117*0a6a1f1dSLionel Sambuc   clang_analyzer_eval(global == -5); // expected-warning{{UNKNOWN}}
118*0a6a1f1dSLionel Sambuc }
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc 
121