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();
19 //===----------------------------------------------------------------------===//
20 // Tests for fields properly guarded by asserts.
21 //===----------------------------------------------------------------------===//
23 class NoUnguardedFieldsTest
{
35 NoUnguardedFieldsTest(Kind K
) : K(K
) {
57 void fNoUnguardedFieldsTest() {
58 NoUnguardedFieldsTest
T1(NoUnguardedFieldsTest::Kind::A
);
59 NoUnguardedFieldsTest
T2(NoUnguardedFieldsTest::Kind::V
);
62 class NoUngardedFieldsNoReturnFuncCalledTest
{
74 NoUngardedFieldsNoReturnFuncCalledTest(Kind K
) : K(K
) {
96 void fNoUngardedFieldsNoReturnFuncCalledTest() {
97 NoUngardedFieldsNoReturnFuncCalledTest
98 T1(NoUngardedFieldsNoReturnFuncCalledTest::Kind::A
);
99 NoUngardedFieldsNoReturnFuncCalledTest
100 T2(NoUngardedFieldsNoReturnFuncCalledTest::Kind::V
);
103 class NoUnguardedFieldsWithUndefMethodTest
{
115 NoUnguardedFieldsWithUndefMethodTest(Kind K
) : K(K
) {
127 assert(K
== Kind::A
);
132 assert(K
== Kind::V
);
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
{
156 int Volume
, Area
; // expected-note {{uninitialized field 'this->Volume'}}
160 UnguardedFieldThroughMethodTest(Kind K
) : K(K
) {
166 Area
= 0; // expected-warning {{1 uninitialized field}}
172 assert(K
== Kind::A
);
181 void fUnguardedFieldThroughMethodTest() {
182 UnguardedFieldThroughMethodTest
T1(UnguardedFieldThroughMethodTest::Kind::A
);
185 class UnguardedPublicFieldsTest
{
193 // Note that fields are public.
194 int Volume
, Area
; // expected-note {{uninitialized field 'this->Volume'}}
198 UnguardedPublicFieldsTest(Kind K
) : K(K
) {
204 Area
= 0; // expected-warning {{1 uninitialized field}}
210 assert(K
== Kind::A
);
215 assert(K
== Kind::V
);
220 void fUnguardedPublicFieldsTest() {
221 UnguardedPublicFieldsTest
T1(UnguardedPublicFieldsTest::Kind::A
);
224 //===----------------------------------------------------------------------===//
225 // Highlights of some false negatives due to syntactic checking.
226 //===----------------------------------------------------------------------===//
228 class UnguardedFalseNegativeTest1
{
240 UnguardedFalseNegativeTest1(Kind K
) : K(K
) {
253 assert(K
== Kind::A
);
259 assert(K
== Kind::V
);
264 void fUnguardedFalseNegativeTest1() {
265 UnguardedFalseNegativeTest1
T1(UnguardedFalseNegativeTest1::Kind::A
);
268 class UnguardedFalseNegativeTest2
{
280 UnguardedFalseNegativeTest2(Kind K
) : K(K
) {
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
{
327 IfGuardedFieldsTest(Kind K
) : K(K
) {
351 void fIfGuardedFieldsTest() {
352 IfGuardedFieldsTest
T1(IfGuardedFieldsTest::Kind::A
);
353 IfGuardedFieldsTest
T2(IfGuardedFieldsTest::Kind::V
);
356 class SwitchGuardedFieldsTest
{
368 SwitchGuardedFieldsTest(Kind K
) : K(K
) {
398 void fSwitchGuardedFieldsTest() {
399 SwitchGuardedFieldsTest
T1(SwitchGuardedFieldsTest::Kind::A
);
400 SwitchGuardedFieldsTest
T2(SwitchGuardedFieldsTest::Kind::V
);
403 class ConditionalOperatorGuardedFieldsTest
{
415 ConditionalOperatorGuardedFieldsTest(Kind K
) : K(K
) {
427 return K
== Kind::A
? Area
: -1;
431 return K
== Kind::V
? Volume
: -1;
435 void fConditionalOperatorGuardedFieldsTest() {
436 ConditionalOperatorGuardedFieldsTest
437 T1(ConditionalOperatorGuardedFieldsTest::Kind::A
);
438 ConditionalOperatorGuardedFieldsTest
439 T2(ConditionalOperatorGuardedFieldsTest::Kind::V
);