xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/inlining/containers.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -std=c++11 -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config c++-inlining=destructors -analyzer-config c++-container-inlining=false -verify %s
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -std=c++11 -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config c++-inlining=destructors -analyzer-config c++-container-inlining=true -DINLINE=1 -verify %s
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc #ifndef HEADER
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc void clang_analyzer_eval(bool);
7f4a2713aSLionel Sambuc void clang_analyzer_checkInlined(bool);
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc #define HEADER
10f4a2713aSLionel Sambuc #include "containers.cpp"
11f4a2713aSLionel Sambuc #undef HEADER
12f4a2713aSLionel Sambuc 
test()13f4a2713aSLionel Sambuc void test() {
14f4a2713aSLionel Sambuc   MySet set(0);
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc   clang_analyzer_eval(set.isEmpty());
17f4a2713aSLionel Sambuc #if INLINE
18f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
19f4a2713aSLionel Sambuc #else
20f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
21f4a2713aSLionel Sambuc #endif
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc   clang_analyzer_eval(set.raw_begin() == set.raw_end());
24f4a2713aSLionel Sambuc #if INLINE
25f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
26f4a2713aSLionel Sambuc #else
27f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
28f4a2713aSLionel Sambuc #endif
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc   clang_analyzer_eval(set.begin().impl == set.end().impl);
31f4a2713aSLionel Sambuc #if INLINE
32f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
33f4a2713aSLionel Sambuc #else
34f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
35f4a2713aSLionel Sambuc #endif
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
testSubclass(MySetSubclass & sub)38f4a2713aSLionel Sambuc void testSubclass(MySetSubclass &sub) {
39f4a2713aSLionel Sambuc   sub.useIterator(sub.begin());
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc   MySetSubclass local;
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc 
testWrappers(BeginOnlySet & w1,IteratorStructOnlySet & w2,IteratorTypedefOnlySet & w3,IteratorUsingOnlySet & w4)44f4a2713aSLionel Sambuc void testWrappers(BeginOnlySet &w1, IteratorStructOnlySet &w2,
45f4a2713aSLionel Sambuc                   IteratorTypedefOnlySet &w3, IteratorUsingOnlySet &w4) {
46f4a2713aSLionel Sambuc   BeginOnlySet local1;
47f4a2713aSLionel Sambuc   IteratorStructOnlySet local2;
48f4a2713aSLionel Sambuc   IteratorTypedefOnlySet local3;
49f4a2713aSLionel Sambuc   IteratorUsingOnlySet local4;
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   clang_analyzer_eval(w1.begin().impl.impl == w1.begin().impl.impl);
52f4a2713aSLionel Sambuc #if INLINE
53f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
54f4a2713aSLionel Sambuc #else
55f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
56f4a2713aSLionel Sambuc #endif
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc   clang_analyzer_eval(w2.start().impl == w2.start().impl);
59f4a2713aSLionel Sambuc #if INLINE
60f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
61f4a2713aSLionel Sambuc #else
62f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
63f4a2713aSLionel Sambuc #endif
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc   clang_analyzer_eval(w3.start().impl == w3.start().impl);
66f4a2713aSLionel Sambuc #if INLINE
67f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
68f4a2713aSLionel Sambuc #else
69f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
70f4a2713aSLionel Sambuc #endif
71f4a2713aSLionel Sambuc 
72f4a2713aSLionel Sambuc   clang_analyzer_eval(w4.start().impl == w4.start().impl);
73f4a2713aSLionel Sambuc #if INLINE
74f4a2713aSLionel Sambuc   // expected-warning@-2 {{TRUE}}
75f4a2713aSLionel Sambuc #else
76f4a2713aSLionel Sambuc   // expected-warning@-4 {{UNKNOWN}}
77f4a2713aSLionel Sambuc #endif
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc #else // HEADER
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc #include "../Inputs/system-header-simulator-cxx.h"
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc class MySet {
86f4a2713aSLionel Sambuc   int *storage;
87f4a2713aSLionel Sambuc   unsigned size;
88f4a2713aSLionel Sambuc public:
MySet()89f4a2713aSLionel Sambuc   MySet() : storage(0), size(0) {
90f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
91f4a2713aSLionel Sambuc #if INLINE
92f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
93f4a2713aSLionel Sambuc #endif
94f4a2713aSLionel Sambuc   }
95f4a2713aSLionel Sambuc 
MySet(unsigned n)96f4a2713aSLionel Sambuc   MySet(unsigned n) : storage(new int[n]), size(n) {
97f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
98f4a2713aSLionel Sambuc #if INLINE
99f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
100f4a2713aSLionel Sambuc #endif
101f4a2713aSLionel Sambuc   }
102f4a2713aSLionel Sambuc 
~MySet()103f4a2713aSLionel Sambuc   ~MySet() { delete[] storage; }
104f4a2713aSLionel Sambuc 
isEmpty()105f4a2713aSLionel Sambuc   bool isEmpty() {
106*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
107*0a6a1f1dSLionel Sambuc     #if INLINE
108*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
109*0a6a1f1dSLionel Sambuc     #endif
110f4a2713aSLionel Sambuc     return size == 0;
111f4a2713aSLionel Sambuc   }
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc   struct iterator {
114f4a2713aSLionel Sambuc     int *impl;
115f4a2713aSLionel Sambuc 
iteratorMySet::iterator116f4a2713aSLionel Sambuc     iterator(int *p) : impl(p) {}
117f4a2713aSLionel Sambuc   };
118f4a2713aSLionel Sambuc 
begin()119f4a2713aSLionel Sambuc   iterator begin() {
120*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
121*0a6a1f1dSLionel Sambuc     #if INLINE
122*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
123*0a6a1f1dSLionel Sambuc     #endif
124f4a2713aSLionel Sambuc     return iterator(storage);
125f4a2713aSLionel Sambuc   }
126f4a2713aSLionel Sambuc 
end()127f4a2713aSLionel Sambuc   iterator end() {
128*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
129*0a6a1f1dSLionel Sambuc     #if INLINE
130*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
131*0a6a1f1dSLionel Sambuc     #endif
132f4a2713aSLionel Sambuc     return iterator(storage+size);
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   typedef int *raw_iterator;
136f4a2713aSLionel Sambuc 
raw_begin()137f4a2713aSLionel Sambuc   raw_iterator raw_begin() {
138*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
139*0a6a1f1dSLionel Sambuc     #if INLINE
140*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
141*0a6a1f1dSLionel Sambuc     #endif
142f4a2713aSLionel Sambuc     return storage;
143f4a2713aSLionel Sambuc   }
raw_end()144f4a2713aSLionel Sambuc   raw_iterator raw_end() {
145*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
146*0a6a1f1dSLionel Sambuc     #if INLINE
147*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
148*0a6a1f1dSLionel Sambuc     #endif
149f4a2713aSLionel Sambuc     return storage + size;
150f4a2713aSLionel Sambuc   }
151f4a2713aSLionel Sambuc };
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc class MySetSubclass : public MySet {
154f4a2713aSLionel Sambuc public:
MySetSubclass()155f4a2713aSLionel Sambuc   MySetSubclass() {
156f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
157f4a2713aSLionel Sambuc #if INLINE
158f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
159f4a2713aSLionel Sambuc #endif
160f4a2713aSLionel Sambuc   }
161f4a2713aSLionel Sambuc 
useIterator(iterator i)162f4a2713aSLionel Sambuc   void useIterator(iterator i) {
163*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
164*0a6a1f1dSLionel Sambuc     #if INLINE
165*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
166*0a6a1f1dSLionel Sambuc     #endif
167f4a2713aSLionel Sambuc   }
168f4a2713aSLionel Sambuc };
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc class BeginOnlySet {
171f4a2713aSLionel Sambuc   MySet impl;
172f4a2713aSLionel Sambuc public:
173f4a2713aSLionel Sambuc   struct IterImpl {
174f4a2713aSLionel Sambuc     MySet::iterator impl;
175f4a2713aSLionel Sambuc     typedef std::forward_iterator_tag iterator_category;
176f4a2713aSLionel Sambuc 
IterImplBeginOnlySet::IterImpl177f4a2713aSLionel Sambuc     IterImpl(MySet::iterator i) : impl(i) {
178f4a2713aSLionel Sambuc       clang_analyzer_checkInlined(true);
179f4a2713aSLionel Sambuc #if INLINE
180f4a2713aSLionel Sambuc       // expected-warning@-2 {{TRUE}}
181f4a2713aSLionel Sambuc #endif
182f4a2713aSLionel Sambuc     }
183f4a2713aSLionel Sambuc   };
184f4a2713aSLionel Sambuc 
BeginOnlySet()185f4a2713aSLionel Sambuc   BeginOnlySet() {
186f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
187f4a2713aSLionel Sambuc #if INLINE
188f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
189f4a2713aSLionel Sambuc #endif
190f4a2713aSLionel Sambuc   }
191f4a2713aSLionel Sambuc 
192f4a2713aSLionel Sambuc   typedef IterImpl wrapped_iterator;
193f4a2713aSLionel Sambuc 
begin()194f4a2713aSLionel Sambuc   wrapped_iterator begin() {
195*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
196*0a6a1f1dSLionel Sambuc     #if INLINE
197*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
198*0a6a1f1dSLionel Sambuc     #endif
199f4a2713aSLionel Sambuc     return IterImpl(impl.begin());
200f4a2713aSLionel Sambuc   }
201f4a2713aSLionel Sambuc };
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc class IteratorTypedefOnlySet {
204f4a2713aSLionel Sambuc   MySet impl;
205f4a2713aSLionel Sambuc public:
206f4a2713aSLionel Sambuc 
IteratorTypedefOnlySet()207f4a2713aSLionel Sambuc   IteratorTypedefOnlySet() {
208f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
209f4a2713aSLionel Sambuc #if INLINE
210f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
211f4a2713aSLionel Sambuc #endif
212f4a2713aSLionel Sambuc   }
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc   typedef MySet::iterator iterator;
215f4a2713aSLionel Sambuc 
start()216f4a2713aSLionel Sambuc   iterator start() {
217*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
218*0a6a1f1dSLionel Sambuc #if INLINE
219*0a6a1f1dSLionel Sambuc     // expected-warning@-2 {{TRUE}}
220*0a6a1f1dSLionel Sambuc #endif
221f4a2713aSLionel Sambuc     return impl.begin();
222f4a2713aSLionel Sambuc   }
223f4a2713aSLionel Sambuc };
224f4a2713aSLionel Sambuc 
225f4a2713aSLionel Sambuc class IteratorUsingOnlySet {
226f4a2713aSLionel Sambuc   MySet impl;
227f4a2713aSLionel Sambuc public:
228f4a2713aSLionel Sambuc 
IteratorUsingOnlySet()229f4a2713aSLionel Sambuc   IteratorUsingOnlySet() {
230f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
231f4a2713aSLionel Sambuc #if INLINE
232f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
233f4a2713aSLionel Sambuc #endif
234f4a2713aSLionel Sambuc   }
235f4a2713aSLionel Sambuc 
236f4a2713aSLionel Sambuc   using iterator = MySet::iterator;
237f4a2713aSLionel Sambuc 
start()238f4a2713aSLionel Sambuc   iterator start() {
239*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
240*0a6a1f1dSLionel Sambuc     #if INLINE
241*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
242*0a6a1f1dSLionel Sambuc     #endif
243f4a2713aSLionel Sambuc     return impl.begin();
244f4a2713aSLionel Sambuc   }
245f4a2713aSLionel Sambuc };
246f4a2713aSLionel Sambuc 
247f4a2713aSLionel Sambuc class IteratorStructOnlySet {
248f4a2713aSLionel Sambuc   MySet impl;
249f4a2713aSLionel Sambuc public:
250f4a2713aSLionel Sambuc 
IteratorStructOnlySet()251f4a2713aSLionel Sambuc   IteratorStructOnlySet() {
252f4a2713aSLionel Sambuc     clang_analyzer_checkInlined(true);
253f4a2713aSLionel Sambuc #if INLINE
254f4a2713aSLionel Sambuc     // expected-warning@-2 {{TRUE}}
255f4a2713aSLionel Sambuc #endif
256f4a2713aSLionel Sambuc   }
257f4a2713aSLionel Sambuc 
258f4a2713aSLionel Sambuc   struct iterator {
259f4a2713aSLionel Sambuc     int *impl;
260f4a2713aSLionel Sambuc   };
261f4a2713aSLionel Sambuc 
start()262f4a2713aSLionel Sambuc   iterator start() {
263*0a6a1f1dSLionel Sambuc     clang_analyzer_checkInlined(true);
264*0a6a1f1dSLionel Sambuc     #if INLINE
265*0a6a1f1dSLionel Sambuc         // expected-warning@-2 {{TRUE}}
266*0a6a1f1dSLionel Sambuc     #endif
267f4a2713aSLionel Sambuc     return iterator{impl.begin().impl};
268f4a2713aSLionel Sambuc   }
269f4a2713aSLionel Sambuc };
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc #endif // HEADER
272