[clang-cl] Ignore /Wv and /Wv:17 flags
[llvm-project.git] / clang / test / Analysis / cxx-uninitialized-object-unguarded-access.cpp
blob53e72e7c5f4f7c01d121474d9a79c6d833632073
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,optin.cplusplus.UninitializedObject \
2 // RUN: -analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \
3 // RUN: -analyzer-config optin.cplusplus.UninitializedObject:IgnoreGuardedFields=true \
4 // RUN: -std=c++11 -verify %s
6 //===----------------------------------------------------------------------===//
7 // Helper functions for tests.
8 //===----------------------------------------------------------------------===//
10 [[noreturn]] void halt();
12 void assert(int b) {
13 if (!b)
14 halt();
17 int rand();
19 //===----------------------------------------------------------------------===//
20 // Tests for fields properly guarded by asserts.
21 //===----------------------------------------------------------------------===//
23 class NoUnguardedFieldsTest {
24 public:
25 enum Kind {
30 private:
31 int Volume, Area;
32 Kind K;
34 public:
35 NoUnguardedFieldsTest(Kind K) : K(K) {
36 switch (K) {
37 case V:
38 Volume = 0;
39 break;
40 case A:
41 Area = 0;
42 break;
46 void operator-() {
47 assert(K == Kind::A);
48 (void)Area;
51 void operator+() {
52 assert(K == Kind::V);
53 (void)Volume;
57 void fNoUnguardedFieldsTest() {
58 NoUnguardedFieldsTest T1(NoUnguardedFieldsTest::Kind::A);
59 NoUnguardedFieldsTest T2(NoUnguardedFieldsTest::Kind::V);
62 class NoUngardedFieldsNoReturnFuncCalledTest {
63 public:
64 enum Kind {
69 private:
70 int Volume, Area;
71 Kind K;
73 public:
74 NoUngardedFieldsNoReturnFuncCalledTest(Kind K) : K(K) {
75 switch (K) {
76 case V:
77 Volume = 0;
78 break;
79 case A:
80 Area = 0;
81 break;
85 void operator-() {
86 halt();
87 (void)Area;
90 void operator+() {
91 halt();
92 (void)Volume;
96 void fNoUngardedFieldsNoReturnFuncCalledTest() {
97 NoUngardedFieldsNoReturnFuncCalledTest
98 T1(NoUngardedFieldsNoReturnFuncCalledTest::Kind::A);
99 NoUngardedFieldsNoReturnFuncCalledTest
100 T2(NoUngardedFieldsNoReturnFuncCalledTest::Kind::V);
103 class NoUnguardedFieldsWithUndefMethodTest {
104 public:
105 enum Kind {
110 private:
111 int Volume, Area;
112 Kind K;
114 public:
115 NoUnguardedFieldsWithUndefMethodTest(Kind K) : K(K) {
116 switch (K) {
117 case V:
118 Volume = 0;
119 break;
120 case A:
121 Area = 0;
122 break;
126 void operator-() {
127 assert(K == Kind::A);
128 (void)Area;
131 void operator+() {
132 assert(K == Kind::V);
133 (void)Volume;
136 // We're checking method definitions for guards, so this is a no-crash test
137 // whether we handle methods without definitions.
138 void methodWithoutDefinition();
141 void fNoUnguardedFieldsWithUndefMethodTest() {
142 NoUnguardedFieldsWithUndefMethodTest
143 T1(NoUnguardedFieldsWithUndefMethodTest::Kind::A);
144 NoUnguardedFieldsWithUndefMethodTest
145 T2(NoUnguardedFieldsWithUndefMethodTest::Kind::V);
148 class UnguardedFieldThroughMethodTest {
149 public:
150 enum Kind {
155 private:
156 int Volume, Area; // expected-note {{uninitialized field 'this->Volume'}}
157 Kind K;
159 public:
160 UnguardedFieldThroughMethodTest(Kind K) : K(K) {
161 switch (K) {
162 case V:
163 Volume = 0;
164 break;
165 case A:
166 Area = 0; // expected-warning {{1 uninitialized field}}
167 break;
171 void operator-() {
172 assert(K == Kind::A);
173 (void)Area;
176 void operator+() {
177 (void)Volume;
181 void fUnguardedFieldThroughMethodTest() {
182 UnguardedFieldThroughMethodTest T1(UnguardedFieldThroughMethodTest::Kind::A);
185 class UnguardedPublicFieldsTest {
186 public:
187 enum Kind {
192 public:
193 // Note that fields are public.
194 int Volume, Area; // expected-note {{uninitialized field 'this->Volume'}}
195 Kind K;
197 public:
198 UnguardedPublicFieldsTest(Kind K) : K(K) {
199 switch (K) {
200 case V:
201 Volume = 0;
202 break;
203 case A:
204 Area = 0; // expected-warning {{1 uninitialized field}}
205 break;
209 void operator-() {
210 assert(K == Kind::A);
211 (void)Area;
214 void operator+() {
215 assert(K == Kind::V);
216 (void)Volume;
220 void fUnguardedPublicFieldsTest() {
221 UnguardedPublicFieldsTest T1(UnguardedPublicFieldsTest::Kind::A);
224 //===----------------------------------------------------------------------===//
225 // Highlights of some false negatives due to syntactic checking.
226 //===----------------------------------------------------------------------===//
228 class UnguardedFalseNegativeTest1 {
229 public:
230 enum Kind {
235 private:
236 int Volume, Area;
237 Kind K;
239 public:
240 UnguardedFalseNegativeTest1(Kind K) : K(K) {
241 switch (K) {
242 case V:
243 Volume = 0;
244 break;
245 case A:
246 Area = 0;
247 break;
251 void operator-() {
252 if (rand())
253 assert(K == Kind::A);
254 (void)Area;
257 void operator+() {
258 if (rand())
259 assert(K == Kind::V);
260 (void)Volume;
264 void fUnguardedFalseNegativeTest1() {
265 UnguardedFalseNegativeTest1 T1(UnguardedFalseNegativeTest1::Kind::A);
268 class UnguardedFalseNegativeTest2 {
269 public:
270 enum Kind {
275 private:
276 int Volume, Area;
277 Kind K;
279 public:
280 UnguardedFalseNegativeTest2(Kind K) : K(K) {
281 switch (K) {
282 case V:
283 Volume = 0;
284 break;
285 case A:
286 Area = 0;
287 break;
291 void operator-() {
292 assert(rand());
293 (void)Area;
296 void operator+() {
297 assert(rand());
298 (void)Volume;
302 void fUnguardedFalseNegativeTest2() {
303 UnguardedFalseNegativeTest2 T1(UnguardedFalseNegativeTest2::Kind::A);
306 //===----------------------------------------------------------------------===//
307 // Tests for other guards. These won't be as thorough, as other guards are
308 // matched the same way as asserts, so if they are recognized, they are expected
309 // to work as well as asserts do.
311 // None of these tests expect warnings, since the flag works correctly if these
312 // fields are regarded properly guarded.
313 //===----------------------------------------------------------------------===//
315 class IfGuardedFieldsTest {
316 public:
317 enum Kind {
322 private:
323 int Volume, Area;
324 Kind K;
326 public:
327 IfGuardedFieldsTest(Kind K) : K(K) {
328 switch (K) {
329 case V:
330 Volume = 0;
331 break;
332 case A:
333 Area = 0;
334 break;
338 void operator-() {
339 if (K != Kind::A)
340 return;
341 (void)Area;
344 void operator+() {
345 if (K != Kind::V)
346 return;
347 (void)Volume;
351 void fIfGuardedFieldsTest() {
352 IfGuardedFieldsTest T1(IfGuardedFieldsTest::Kind::A);
353 IfGuardedFieldsTest T2(IfGuardedFieldsTest::Kind::V);
356 class SwitchGuardedFieldsTest {
357 public:
358 enum Kind {
363 private:
364 int Volume, Area;
365 Kind K;
367 public:
368 SwitchGuardedFieldsTest(Kind K) : K(K) {
369 switch (K) {
370 case V:
371 Volume = 0;
372 break;
373 case A:
374 Area = 0;
375 break;
379 int operator-() {
380 switch (K) {
381 case Kind::A:
382 return Area;
383 case Kind::V:
384 return -1;
388 int operator+() {
389 switch (K) {
390 case Kind::A:
391 return Area;
392 case Kind::V:
393 return -1;
398 void fSwitchGuardedFieldsTest() {
399 SwitchGuardedFieldsTest T1(SwitchGuardedFieldsTest::Kind::A);
400 SwitchGuardedFieldsTest T2(SwitchGuardedFieldsTest::Kind::V);
403 class ConditionalOperatorGuardedFieldsTest {
404 public:
405 enum Kind {
410 private:
411 int Volume, Area;
412 Kind K;
414 public:
415 ConditionalOperatorGuardedFieldsTest(Kind K) : K(K) {
416 switch (K) {
417 case V:
418 Volume = 0;
419 break;
420 case A:
421 Area = 0;
422 break;
426 int operator-() {
427 return K == Kind::A ? Area : -1;
430 int operator+() {
431 return K == Kind::V ? Volume : -1;
435 void fConditionalOperatorGuardedFieldsTest() {
436 ConditionalOperatorGuardedFieldsTest
437 T1(ConditionalOperatorGuardedFieldsTest::Kind::A);
438 ConditionalOperatorGuardedFieldsTest
439 T2(ConditionalOperatorGuardedFieldsTest::Kind::V);