1 // RUN: %clang_analyze_cc1 -std=c++14 -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=optin.cplusplus.UninitializedObject \
4 // RUN: -analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \
5 // RUN: -analyzer-config \
6 // RUN: optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true
8 // RUN: %clang_analyze_cc1 -std=c++14 -verify %s \
9 // RUN: -analyzer-checker=core \
10 // RUN: -analyzer-checker=optin.cplusplus.UninitializedObject \
11 // RUN: -analyzer-config \
12 // RUN: optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true
14 //===----------------------------------------------------------------------===//
15 // Default constructor test.
16 //===----------------------------------------------------------------------===//
18 class CompilerGeneratedConstructorTest
{
19 int a
, b
, c
, d
, e
, f
, g
, h
, i
, j
;
22 CompilerGeneratedConstructorTest() = default;
25 void fCompilerGeneratedConstructorTest() {
26 CompilerGeneratedConstructorTest();
30 class DefaultConstructorTest
{
31 int a
; // expected-note{{uninitialized field 'this->a'}}
34 DefaultConstructorTest();
37 DefaultConstructorTest::DefaultConstructorTest() = default;
39 void fDefaultConstructorTest() {
40 DefaultConstructorTest(); // expected-warning{{1 uninitialized field}}
43 class DefaultConstructorTest
{
47 DefaultConstructorTest();
50 DefaultConstructorTest::DefaultConstructorTest() = default;
52 void fDefaultConstructorTest() {
53 DefaultConstructorTest();
57 //===----------------------------------------------------------------------===//
58 // Initializer list test.
59 //===----------------------------------------------------------------------===//
73 void fInitListTest1() {
79 int b
; // expected-note{{uninitialized field 'this->b'}}
83 : a(3) {} // expected-warning{{1 uninitialized field}}
86 void fInitListTest2() {
91 int a
; // expected-note{{uninitialized field 'this->a'}}
96 : b(4) {} // expected-warning{{1 uninitialized field}}
99 void fInitListTest3() {
103 //===----------------------------------------------------------------------===//
104 // Constructor body test.
105 //===----------------------------------------------------------------------===//
107 class CtorBodyTest1
{
118 void fCtorBodyTest1() {
122 class CtorBodyTest2
{
124 int b
; // expected-note{{uninitialized field 'this->b'}}
128 a
= 7; // expected-warning{{1 uninitialized field}}
132 void fCtorBodyTest2() {
136 class CtorBodyTest3
{
137 int a
; // expected-note{{uninitialized field 'this->a'}}
142 b
= 8; // expected-warning{{1 uninitialized field}}
146 void fCtorBodyTest3() {
151 class CtorBodyTest4
{
152 int a
; // expected-note{{uninitialized field 'this->a'}}
153 int b
; // expected-note{{uninitialized field 'this->b'}}
159 void fCtorBodyTest4() {
160 CtorBodyTest4(); // expected-warning{{2 uninitialized fields}}
163 class CtorBodyTest4
{
171 void fCtorBodyTest4() {
176 //===----------------------------------------------------------------------===//
177 // Constructor delegation test.
178 //===----------------------------------------------------------------------===//
180 class CtorDelegationTest1
{
185 CtorDelegationTest1(int)
187 // leaves 'b' unintialized, but we'll never check this function
190 CtorDelegationTest1()
191 : CtorDelegationTest1(int{}) { // Initializing 'a'
197 void fCtorDelegationTest1() {
198 CtorDelegationTest1();
201 class CtorDelegationTest2
{
202 int a
; // expected-note{{uninitialized field 'this->a'}}
206 CtorDelegationTest2(int)
208 // leaves 'a' unintialized, but we'll never check this function
211 CtorDelegationTest2()
212 : CtorDelegationTest2(int{}) { // expected-warning{{1 uninitialized field}}
216 void fCtorDelegationTest2() {
217 CtorDelegationTest2();
220 //===----------------------------------------------------------------------===//
221 // Tests for classes containing records.
222 //===----------------------------------------------------------------------===//
224 class ContainsRecordTest1
{
232 ContainsRecordTest1()
240 void fContainsRecordTest1() {
241 ContainsRecordTest1();
244 class ContainsRecordTest2
{
247 int y
; // expected-note{{uninitialized field 'this->rec.y'}}
252 ContainsRecordTest2()
255 rec
.x
= 18; // expected-warning{{1 uninitialized field}}
259 void fContainsRecordTest2() {
260 ContainsRecordTest2();
263 class ContainsRecordTest3
{
265 int x
; // expected-note{{uninitialized field 'this->rec.x'}}
266 int y
; // expected-note{{uninitialized field 'this->rec.y'}}
271 ContainsRecordTest3()
273 d(20) { // expected-warning{{2 uninitialized fields}}
277 void fContainsRecordTest3() {
278 ContainsRecordTest3();
281 class ContainsRecordTest4
{
283 int x
; // expected-note{{uninitialized field 'this->rec.x'}}
284 int y
; // expected-note{{uninitialized field 'this->rec.y'}}
286 int c
, d
; // expected-note{{uninitialized field 'this->d'}}
289 ContainsRecordTest4()
290 : c(19) { // expected-warning{{3 uninitialized fields}}
294 void fContainsRecordTest4() {
295 ContainsRecordTest4();
298 //===----------------------------------------------------------------------===//
299 // Tests for template classes.
300 //===----------------------------------------------------------------------===//
303 class IntTemplateClassTest1
{
308 IntTemplateClassTest1(T i
) {
315 void fIntTemplateClassTest1() {
316 IntTemplateClassTest1
<int>(22);
320 class IntTemplateClassTest2
{
321 T t
; // expected-note{{uninitialized field 'this->t'}}
325 IntTemplateClassTest2() {
326 b
= 23; // expected-warning{{1 uninitialized field}}
330 void fIntTemplateClassTest2() {
331 IntTemplateClassTest2
<int>();
335 int x
; // expected-note{{uninitialized field 'this->t.x'}}
336 int y
; // expected-note{{uninitialized field 'this->t.y'}}
340 class RecordTemplateClassTest
{
345 RecordTemplateClassTest() {
346 b
= 24; // expected-warning{{2 uninitialized fields}}
350 void fRecordTemplateClassTest() {
351 RecordTemplateClassTest
<Record
>();
354 //===----------------------------------------------------------------------===//
355 // Tests involving functions with unknown implementations.
356 //===----------------------------------------------------------------------===//
359 void mayInitialize(T
&);
362 void wontInitialize(const T
&);
364 class PassingToUnknownFunctionTest1
{
368 PassingToUnknownFunctionTest1() {
374 PassingToUnknownFunctionTest1(int) {
379 PassingToUnknownFunctionTest1(int, int) {
380 mayInitialize(*this);
385 void fPassingToUnknownFunctionTest1() {
386 PassingToUnknownFunctionTest1();
387 PassingToUnknownFunctionTest1(int());
388 PassingToUnknownFunctionTest1(int(), int());
391 class PassingToUnknownFunctionTest2
{
392 int a
; // expected-note{{uninitialized field 'this->a'}}
396 PassingToUnknownFunctionTest2() {
398 b
= 4; // expected-warning{{1 uninitialized field}}
402 void fPassingToUnknownFunctionTest2() {
403 PassingToUnknownFunctionTest2();
406 //===----------------------------------------------------------------------===//
407 // Tests for classes containing unions.
408 //===----------------------------------------------------------------------===//
410 // FIXME: As of writing this checker, there is no good support for union types
411 // in the Static Analyzer. Here is non-exhaustive list of cases.
412 // Note that the rules for unions are different in C and C++.
413 // http://lists.llvm.org/pipermail/cfe-dev/2017-March/052910.html
415 class ContainsSimpleUnionTest1
{
423 ContainsSimpleUnionTest1() {
429 void fContainsSimpleUnionTest1() {
430 ContainsSimpleUnionTest1();
433 class ContainsSimpleUnionTest2
{
438 // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
442 ContainsSimpleUnionTest2() {}
445 void fContainsSimpleUnionTest2() {
446 // TODO: we'd expect the warning: {{1 uninitialized field}}
447 ContainsSimpleUnionTest2(); // no-warning
450 class UnionPointerTest1
{
462 UnionPointerTest1(SimpleUnion
*uptr
, int) : uptr(uptr
) {
467 void fUnionPointerTest1() {
468 UnionPointerTest1::SimpleUnion u
;
470 UnionPointerTest1(&u
, int());
473 class UnionPointerTest2
{
482 // TODO: we'd expect the note: {{uninitialized field 'this->uptr'}}
483 SimpleUnion
*uptr
; // no-note
486 UnionPointerTest2(SimpleUnion
*uptr
, char) : uptr(uptr
) {}
489 void fUnionPointerTest2() {
490 UnionPointerTest2::SimpleUnion u
;
491 // TODO: we'd expect the warning: {{1 uninitialized field}}
492 UnionPointerTest2(&u
, int()); // no-warning
495 class ContainsUnionWithRecordTest1
{
496 union UnionWithRecord
{
508 ContainsUnionWithRecordTest1() {
514 void fContainsUnionWithRecordTest1() {
515 ContainsUnionWithRecordTest1();
518 class ContainsUnionWithRecordTest2
{
519 union UnionWithRecord
{
531 ContainsUnionWithRecordTest2() {
532 u
.us
= UnionWithRecord::RecordType
{42, 43};
537 void fContainsUnionWithRecordTest2() {
538 ContainsUnionWithRecordTest1();
541 class ContainsUnionWithRecordTest3
{
542 union UnionWithRecord
{
551 // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
555 ContainsUnionWithRecordTest3() {
556 UnionWithRecord::RecordType rec
;
558 // TODO: we'd expect the warning: {{1 uninitialized field}}
559 u
.us
= rec
; // no-warning
563 void fContainsUnionWithRecordTest3() {
564 ContainsUnionWithRecordTest3();
567 class ContainsUnionWithSimpleUnionTest1
{
568 union UnionWithSimpleUnion
{
579 ContainsUnionWithSimpleUnionTest1() {
585 void fContainsUnionWithSimpleUnionTest1() {
586 ContainsUnionWithSimpleUnionTest1();
589 class ContainsUnionWithSimpleUnionTest2
{
590 union UnionWithSimpleUnion
{
598 // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
602 ContainsUnionWithSimpleUnionTest2() {}
605 void fContainsUnionWithSimpleUnionTest2() {
606 // TODO: we'd expect the warning: {{1 uninitialized field}}
607 ContainsUnionWithSimpleUnionTest2(); // no-warning
610 //===----------------------------------------------------------------------===//
611 // Zero initialization tests.
612 //===----------------------------------------------------------------------===//
614 struct GlobalVariableTest
{
617 GlobalVariableTest() {}
620 GlobalVariableTest gvt
; // no-warning
622 //===----------------------------------------------------------------------===//
623 // Copy and move constructor tests.
624 //===----------------------------------------------------------------------===//
627 void funcToSquelchCompilerWarnings(const T
&t
);
630 struct CopyConstructorTest
{
631 int i
; // expected-note{{uninitialized field 'this->i'}}
633 CopyConstructorTest() : i(1337) {}
634 CopyConstructorTest(const CopyConstructorTest
&other
) {}
637 void fCopyConstructorTest() {
638 CopyConstructorTest cct
;
639 CopyConstructorTest copy
= cct
; // expected-warning{{1 uninitialized field}}
640 funcToSquelchCompilerWarnings(copy
);
643 struct CopyConstructorTest
{
646 CopyConstructorTest() : i(1337) {}
647 CopyConstructorTest(const CopyConstructorTest
&other
) {}
650 void fCopyConstructorTest() {
651 CopyConstructorTest cct
;
652 CopyConstructorTest copy
= cct
;
653 funcToSquelchCompilerWarnings(copy
);
657 struct MoveConstructorTest
{
658 // TODO: we'd expect the note: {{uninitialized field 'this->i'}}
661 MoveConstructorTest() : i(1337) {}
662 MoveConstructorTest(const CopyConstructorTest
&other
) = delete;
663 MoveConstructorTest(const CopyConstructorTest
&&other
) {}
666 void fMoveConstructorTest() {
667 MoveConstructorTest cct
;
668 // TODO: we'd expect the warning: {{1 uninitialized field}}
669 MoveConstructorTest
copy(static_cast<MoveConstructorTest
&&>(cct
)); // no-warning
670 funcToSquelchCompilerWarnings(copy
);
673 //===----------------------------------------------------------------------===//
675 //===----------------------------------------------------------------------===//
677 struct IntArrayTest
{
685 void fIntArrayTest() {
689 struct RecordTypeArrayTest
{
694 RecordTypeArrayTest() {
699 void fRecordTypeArrayTest() {
700 RecordTypeArrayTest();
704 class CharArrayPointerTest
{
708 CharArrayPointerTest(T
*t
, int) : t(t
) {}
711 void fCharArrayPointerTest() {
712 char str
[16] = "012345678912345";
713 CharArrayPointerTest
<char[16]>(&str
, int());
716 //===----------------------------------------------------------------------===//
718 //===----------------------------------------------------------------------===//
724 __builtin_memset(this, 0, sizeof(decltype(*this)));
728 void fMemsetTest1() {
736 __builtin_memset(&a
, 0, sizeof(int));
740 void fMemsetTest2() {
744 //===----------------------------------------------------------------------===//
746 //===----------------------------------------------------------------------===//
748 template <class Callable
>
749 struct LambdaThisTest
{
752 LambdaThisTest(const Callable
&functor
, int) : functor(functor
) {
757 struct HasCapturableThis
{
758 void fLambdaThisTest() {
759 auto isEven
= [this](int a
) { return a
% 2 == 0; }; // no-crash
760 LambdaThisTest
<decltype(isEven
)>(isEven
, int());
764 template <class Callable
>
768 LambdaTest1(const Callable
&functor
, int) : functor(functor
) {
773 void fLambdaTest1() {
774 auto isEven
= [](int a
) { return a
% 2 == 0; };
775 LambdaTest1
<decltype(isEven
)>(isEven
, int());
779 template <class Callable
>
783 LambdaTest2(const Callable
&functor
, int) : functor(functor
) {} // expected-warning{{1 uninitialized field}}
786 void fLambdaTest2() {
788 auto equals
= [&b
](int a
) { return a
== b
; }; // expected-note{{uninitialized pointee 'this->functor./*captured variable*/b'}}
789 LambdaTest2
<decltype(equals
)>(equals
, int());
792 template <class Callable
>
796 LambdaTest2(const Callable
&functor
, int) : functor(functor
) {}
799 void fLambdaTest2() {
801 auto equals
= [&b
](int a
) { return a
== b
; };
802 LambdaTest2
<decltype(equals
)>(equals
, int());
807 namespace LT3Detail
{
810 int x
; // expected-note{{uninitialized field 'this->functor./*captured variable*/rec1.x'}}
811 int y
; // expected-note{{uninitialized field 'this->functor./*captured variable*/rec1.y'}}
814 } // namespace LT3Detail
815 template <class Callable
>
819 LambdaTest3(const Callable
&functor
, int) : functor(functor
) {} // expected-warning{{2 uninitialized fields}}
822 void fLambdaTest3() {
823 LT3Detail::RecordType rec1
;
824 auto equals
= [&rec1
](LT3Detail::RecordType rec2
) {
825 return rec1
.x
== rec2
.x
;
827 LambdaTest3
<decltype(equals
)>(equals
, int());
830 namespace LT3Detail
{
837 } // namespace LT3Detail
838 template <class Callable
>
842 LambdaTest3(const Callable
&functor
, int) : functor(functor
) {}
845 void fLambdaTest3() {
846 LT3Detail::RecordType rec1
;
847 auto equals
= [&rec1
](LT3Detail::RecordType rec2
) {
848 return rec1
.x
== rec2
.x
;
850 LambdaTest3
<decltype(equals
)>(equals
, int());
854 template <class Callable
>
855 struct MultipleLambdaCapturesTest1
{
857 int dontGetFilteredByNonPedanticMode
= 0;
859 MultipleLambdaCapturesTest1(const Callable
&functor
, int) : functor(functor
) {} // expected-warning{{2 uninitialized field}}
862 void fMultipleLambdaCapturesTest1() {
864 auto equals
= [&b1
, &b2
, &b3
](int a
) { return a
== b1
== b2
== b3
; }; // expected-note{{uninitialized pointee 'this->functor./*captured variable*/b1'}}
865 // expected-note@-1{{uninitialized pointee 'this->functor./*captured variable*/b3'}}
866 MultipleLambdaCapturesTest1
<decltype(equals
)>(equals
, int());
869 template <class Callable
>
870 struct MultipleLambdaCapturesTest2
{
872 int dontGetFilteredByNonPedanticMode
= 0;
874 MultipleLambdaCapturesTest2(const Callable
&functor
, int) : functor(functor
) {} // expected-warning{{1 uninitialized field}}
877 void fMultipleLambdaCapturesTest2() {
879 auto equals
= [b1
, &b2
, &b3
](int a
) { return a
== b1
== b2
== b3
; }; // expected-note{{uninitialized pointee 'this->functor./*captured variable*/b3'}}
880 MultipleLambdaCapturesTest2
<decltype(equals
)>(equals
, int());
883 struct LambdaWrapper
{
884 void *func
; // no-crash
885 int dontGetFilteredByNonPedanticMode
= 0;
887 LambdaWrapper(void *ptr
) : func(ptr
) {} // expected-warning{{1 uninitialized field}}
890 struct ThisCapturingLambdaFactory
{
891 int a
; // expected-note{{uninitialized field 'static_cast<decltype(a.ret()) *>(this->func)->/*'this' capture*/->a'}}
894 return [this] { (void)this; };
898 void fLambdaFieldWithInvalidThisCapture() {
901 ThisCapturingLambdaFactory a
;
902 decltype(a
.ret()) lambda
= a
.ret();
905 LambdaWrapper
t(ptr
);
908 //===----------------------------------------------------------------------===//
909 // System header tests.
910 //===----------------------------------------------------------------------===//
912 #include "Inputs/system-header-simulator-for-cxx-uninitialized-object.h"
914 struct SystemHeaderTest1
{
915 RecordInSystemHeader rec
; // defined in the system header simulator
917 SystemHeaderTest1() {
922 void fSystemHeaderTest1() {
927 struct SystemHeaderTest2
{
929 int x
; // expected-note{{uninitialized field 'this->container.t.x}}
930 int y
; // expected-note{{uninitialized field 'this->container.t.y}}
932 ContainerInSystemHeader
<RecordType
> container
;
934 SystemHeaderTest2(RecordType
&rec
, int) : container(rec
) {} // expected-warning{{2 uninitialized fields}}
937 void fSystemHeaderTest2() {
938 SystemHeaderTest2::RecordType rec
;
939 SystemHeaderTest2(rec
, int());
942 struct SystemHeaderTest2
{
947 ContainerInSystemHeader
<RecordType
> container
;
949 SystemHeaderTest2(RecordType
&rec
, int) : container(rec
) {}
952 void fSystemHeaderTest2() {
953 SystemHeaderTest2::RecordType rec
;
954 SystemHeaderTest2(rec
, int());
958 //===----------------------------------------------------------------------===//
959 // Incomplete type tests.
960 //===----------------------------------------------------------------------===//
962 struct IncompleteTypeTest1
{
965 RecordType
*recptr
; // expected-note{{uninitialized pointer 'this->recptr}}
966 int dontGetFilteredByNonPedanticMode
= 0;
968 IncompleteTypeTest1() {} // expected-warning{{1 uninitialized field}}
971 void fIncompleteTypeTest1() {
972 IncompleteTypeTest1();
975 struct IncompleteTypeTest2
{
977 RecordType
*recptr
; // no-crash
978 int dontGetFilteredByNonPedanticMode
= 0;
980 RecordType
*recordTypeFactory();
982 IncompleteTypeTest2() : recptr(recordTypeFactory()) {}
985 void fIncompleteTypeTest2() {
986 IncompleteTypeTest2();
989 struct IncompleteTypeTest3
{
991 RecordType
&recref
; // no-crash
992 int dontGetFilteredByNonPedanticMode
= 0;
994 RecordType
&recordTypeFactory();
996 IncompleteTypeTest3() : recref(recordTypeFactory()) {}
999 void fIncompleteTypeTest3() {
1000 IncompleteTypeTest3();
1003 //===----------------------------------------------------------------------===//
1004 // Builtin type or enumeration type related tests.
1005 //===----------------------------------------------------------------------===//
1007 struct IntegralTypeTest
{
1008 int a
; // expected-note{{uninitialized field 'this->a'}}
1009 int dontGetFilteredByNonPedanticMode
= 0;
1011 IntegralTypeTest() {} // expected-warning{{1 uninitialized field}}
1014 void fIntegralTypeTest() {
1018 struct FloatingTypeTest
{
1019 float a
; // expected-note{{uninitialized field 'this->a'}}
1020 int dontGetFilteredByNonPedanticMode
= 0;
1022 FloatingTypeTest() {} // expected-warning{{1 uninitialized field}}
1025 void fFloatingTypeTest() {
1029 struct NullptrTypeTypeTest
{
1030 decltype(nullptr) a
; // expected-note{{uninitialized field 'this->a'}}
1031 int dontGetFilteredByNonPedanticMode
= 0;
1033 NullptrTypeTypeTest() {} // expected-warning{{1 uninitialized field}}
1036 void fNullptrTypeTypeTest() {
1037 NullptrTypeTypeTest();
1044 } enum1
; // expected-note{{uninitialized field 'this->enum1'}}
1048 } enum2
; // expected-note{{uninitialized field 'this->enum2'}}
1049 int dontGetFilteredByNonPedanticMode
= 0;
1051 EnumTest() {} // expected-warning{{2 uninitialized fields}}
1058 //===----------------------------------------------------------------------===//
1059 // Tests for constructor calls within another cunstructor, without the two
1060 // records being in any relation.
1061 //===----------------------------------------------------------------------===//
1063 void halt() __attribute__((__noreturn__
));
1064 void assert(int b
) {
1069 // While a singleton would make more sense as a static variable, that would zero
1070 // initialize all of its fields, hence the not too practical implementation.
1072 int i
; // expected-note{{uninitialized field 'this->i'}}
1073 int dontGetFilteredByNonPedanticMode
= 0;
1076 assert(!isInstantiated
);
1077 isInstantiated
= true; // expected-warning{{1 uninitialized field}}
1081 isInstantiated
= false;
1084 static bool isInstantiated
;
1087 bool Singleton::isInstantiated
= false;
1089 struct SingletonTest
{
1090 int dontGetFilteredByNonPedanticMode
= 0;
1097 void fSingletonTest() {
1101 //===----------------------------------------------------------------------===//
1102 // C++11 member initializer tests.
1103 //===----------------------------------------------------------------------===//
1105 struct CXX11MemberInitTest1
{
1108 CXX11MemberInitTest1() : b(2) {
1113 void fCXX11MemberInitTest1() {
1114 CXX11MemberInitTest1();
1117 struct CXX11MemberInitTest2
{
1119 // TODO: we'd expect the note: {{uninitialized field 'this->rec.a'}}
1121 // TODO: we'd expect the note: {{uninitialized field 'this->rec.b'}}
1127 RecordType rec
= RecordType(int());
1128 int dontGetFilteredByNonPedanticMode
= 0;
1130 CXX11MemberInitTest2() {}
1133 void fCXX11MemberInitTest2() {
1134 // TODO: we'd expect the warning: {{2 uninitializeds field}}
1135 CXX11MemberInitTest2(); // no-warning
1138 //===----------------------------------------------------------------------===//
1139 // "Esoteric" primitive type tests.
1140 //===----------------------------------------------------------------------===//
1142 struct MyAtomicInt
{
1143 _Atomic(int) x
; // expected-note{{uninitialized field 'this->x'}}
1144 int dontGetFilteredByNonPedanticMode
= 0;
1146 MyAtomicInt() {} // expected-warning{{1 uninitialized field}}
1149 void _AtomicTest() {
1153 struct VectorSizeLong
{
1155 __attribute__((__vector_size__(16))) long x
;
1158 void __vector_size__LongTest() {
1159 // TODO: Warn for v.x.
1164 struct ComplexUninitTest
{
1165 ComplexUninitTest() {}
1166 __complex__
float x
;
1170 struct ComplexInitTest
{
1175 __complex__
float x
;
1179 void fComplexTest() {
1182 // TODO: we should emit a warning for x2.x and x2.y.
1183 ComplexUninitTest x2
;