1 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -DI386 -analyzer-checker=core,debug.ExprInspection -fobjc-arc -analyzer-config c++-inlining=constructors -Wno-null-dereference -std=c++11 -verify -analyzer-config eagerly-assume=false %s
2 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -DI386 -analyzer-checker=core,debug.ExprInspection -fobjc-arc -analyzer-config c++-inlining=constructors -Wno-null-dereference -std=c++11 -verify -DTEST_INLINABLE_ALLOCATORS -analyzer-config eagerly-assume=false %s
3 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin12 -analyzer-checker=core,debug.ExprInspection -fobjc-arc -analyzer-config c++-inlining=constructors -Wno-null-dereference -std=c++11 -verify -analyzer-config eagerly-assume=false %s
4 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin12 -analyzer-checker=core,debug.ExprInspection -fobjc-arc -analyzer-config c++-inlining=constructors -Wno-null-dereference -std=c++11 -verify -DTEST_INLINABLE_ALLOCATORS -analyzer-config eagerly-assume=false %s
6 #include "Inputs/system-header-simulator-cxx.h"
8 void clang_analyzer_eval(bool);
9 void clang_analyzer_checkInlined(bool);
11 // A simplified version of std::move.
14 return static_cast<T &&>(obj);
25 *(char *)0 = 1; // expected-warning{{Dereference of null pointer}}
33 void testCopyConstructor() {
38 clang_analyzer_eval(b.x == 42); // expected-warning{{TRUE}}
41 struct NonPODIntWrapper {
47 void testNonPODCopyConstructor() {
51 NonPODIntWrapper b(a);
52 clang_analyzer_eval(b.x == 42); // expected-warning{{TRUE}}
56 namespace ConstructorVirtualCalls {
59 int *out1, *out2, *out3;
61 virtual int get() { return 1; }
70 virtual int get() { return 2; }
72 B(int *out1, int *out2) : A(out1) {
79 virtual int get() { return 3; }
81 C(int *out1, int *out2, int *out3) : B(out1, out2) {
90 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
91 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
92 clang_analyzer_eval(c == 3); // expected-warning{{TRUE}}
94 clang_analyzer_eval(obj.get() == 3); // expected-warning{{TRUE}}
96 // Correctness check for devirtualization.
98 clang_analyzer_eval(base->get() == 3); // expected-warning{{TRUE}}
102 namespace TemporaryConstructor {
106 clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
113 // PR13717 - Don't crash when a CXXTemporaryObjectExpr is inlined.
114 if (BoolWrapper().value)
120 namespace ConstructorUsedAsRValue {
121 using TemporaryConstructor::BoolWrapper;
123 bool extractValue(BoolWrapper b) {
128 bool result = extractValue(BoolWrapper());
129 clang_analyzer_eval(result); // expected-warning{{TRUE}}
133 namespace PODUninitialized {
149 NonPOD(const NonPOD &Other)
150 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
153 NonPOD(NonPOD &&Other)
154 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
158 NonPOD &operator=(const NonPOD &Other)
161 y = Other.y; // expected-warning {{undefined}}
164 NonPOD &operator=(NonPOD &&Other)
167 y = Other.y; // expected-warning {{undefined}}
172 class NonPODWrapper {
179 Inner(const Inner &Other)
180 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
184 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
188 Inner &operator=(const Inner &Other)
190 x = Other.x; // expected-warning {{undefined}}
194 Inner &operator=(Inner &&Other)
196 x = Other.x; // expected-warning {{undefined}}
205 void testPOD(const POD &pp) {
208 POD p2 = p; // no-warning
209 clang_analyzer_eval(p2.x == 1); // expected-warning{{TRUE}}
210 POD p3 = move(p); // no-warning
211 clang_analyzer_eval(p3.x == 1); // expected-warning{{TRUE}}
213 // Use rvalues as well.
214 clang_analyzer_eval(POD(p3).x == 1); // expected-warning{{TRUE}}
216 // Copy from symbolic references correctly.
218 // Make sure that p4.x contains a symbol after copy.
220 clang_analyzer_eval(p4.x > 0); // expected-warning{{TRUE}}
221 clang_analyzer_eval(pp.x == p4.x); // expected-warning{{TRUE}}
225 PODWrapper w2 = w; // no-warning
226 clang_analyzer_eval(w2.p.y == 1); // expected-warning{{TRUE}}
227 PODWrapper w3 = move(w); // no-warning
228 clang_analyzer_eval(w3.p.y == 1); // expected-warning{{TRUE}}
230 // Use rvalues as well.
231 clang_analyzer_eval(PODWrapper(w3).p.y == 1); // expected-warning{{TRUE}}
240 void testNonPODMove() {
246 void testNonPODWrapper() {
249 NonPODWrapper w2 = w;
252 void testNonPODWrapperMove() {
255 NonPODWrapper w2 = move(w);
258 // Not strictly about constructors, but trivial assignment operators should
259 // essentially work the same way.
260 namespace AssignmentOperator {
265 p2 = p; // no-warning
266 clang_analyzer_eval(p2.x == 1); // expected-warning{{TRUE}}
268 p3 = move(p); // no-warning
269 clang_analyzer_eval(p3.x == 1); // expected-warning{{TRUE}}
274 w2 = w; // no-warning
275 clang_analyzer_eval(w2.p.y == 1); // expected-warning{{TRUE}}
277 w3 = move(w); // no-warning
278 clang_analyzer_eval(w3.p.y == 1); // expected-warning{{TRUE}}
281 void testReturnValue() {
285 clang_analyzer_eval(&(p2 = p) == &p2); // expected-warning{{TRUE}}
290 clang_analyzer_eval(&(w2 = w) == &w2); // expected-warning{{TRUE}}
300 void testNonPODMove() {
307 void testNonPODWrapper() {
314 void testNonPODWrapperMove() {
323 namespace ArrayMembers {
328 void testPrimitive() {
329 Primitive a = { { 1, 2, 3 } };
331 clang_analyzer_eval(a.values[0] == 1); // expected-warning{{TRUE}}
332 clang_analyzer_eval(a.values[1] == 2); // expected-warning{{TRUE}}
333 clang_analyzer_eval(a.values[2] == 3); // expected-warning{{TRUE}}
337 clang_analyzer_eval(b.values[0] == 1); // expected-warning{{TRUE}}
338 clang_analyzer_eval(b.values[1] == 2); // expected-warning{{TRUE}}
339 clang_analyzer_eval(b.values[2] == 3); // expected-warning{{TRUE}}
344 clang_analyzer_eval(c.values[0] == 1); // expected-warning{{TRUE}}
345 clang_analyzer_eval(c.values[1] == 2); // expected-warning{{TRUE}}
346 clang_analyzer_eval(c.values[2] == 3); // expected-warning{{TRUE}}
349 struct NestedPrimitive {
353 void testNestedPrimitive() {
354 NestedPrimitive a = { { { 0, 0, 0 }, { 1, 2, 3 } } };
356 clang_analyzer_eval(a.values[1][0] == 1); // expected-warning{{TRUE}}
357 clang_analyzer_eval(a.values[1][1] == 2); // expected-warning{{TRUE}}
358 clang_analyzer_eval(a.values[1][2] == 3); // expected-warning{{TRUE}}
360 NestedPrimitive b = a;
362 clang_analyzer_eval(b.values[1][0] == 1); // expected-warning{{TRUE}}
363 clang_analyzer_eval(b.values[1][1] == 2); // expected-warning{{TRUE}}
364 clang_analyzer_eval(b.values[1][2] == 3); // expected-warning{{TRUE}}
369 clang_analyzer_eval(c.values[1][0] == 1); // expected-warning{{TRUE}}
370 clang_analyzer_eval(c.values[1][1] == 2); // expected-warning{{TRUE}}
371 clang_analyzer_eval(c.values[1][2] == 3); // expected-warning{{TRUE}}
375 IntWrapper values[3];
379 POD a = { { { 1 }, { 2 }, { 3 } } };
381 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
382 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
383 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
387 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
388 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
389 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
394 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
395 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
396 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
400 IntWrapper values[2][3];
403 void testNestedPOD() {
404 NestedPOD a = { { { { 0 }, { 0 }, { 0 } }, { { 1 }, { 2 }, { 3 } } } };
406 clang_analyzer_eval(a.values[1][0].x == 1); // expected-warning{{TRUE}}
407 clang_analyzer_eval(a.values[1][1].x == 2); // expected-warning{{TRUE}}
408 clang_analyzer_eval(a.values[1][2].x == 3); // expected-warning{{TRUE}}
412 clang_analyzer_eval(b.values[1][0].x == 1); // expected-warning{{TRUE}}
413 clang_analyzer_eval(b.values[1][1].x == 2); // expected-warning{{TRUE}}
414 clang_analyzer_eval(b.values[1][2].x == 3); // expected-warning{{TRUE}}
419 clang_analyzer_eval(c.values[1][0].x == 1); // expected-warning{{TRUE}}
420 clang_analyzer_eval(c.values[1][1].x == 2); // expected-warning{{TRUE}}
421 clang_analyzer_eval(c.values[1][2].x == 3); // expected-warning{{TRUE}}
425 NonPODIntWrapper values[3];
434 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
435 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
436 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
440 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
441 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
442 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
447 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
448 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
449 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
452 struct NestedNonPOD {
453 NonPODIntWrapper values[2][3];
456 void testNestedNonPOD() {
458 a.values[0][0].x = 0;
459 a.values[0][1].x = 0;
460 a.values[0][2].x = 0;
461 a.values[1][0].x = 1;
462 a.values[1][1].x = 2;
463 a.values[1][2].x = 3;
465 clang_analyzer_eval(a.values[1][0].x == 1); // expected-warning{{TRUE}}
466 clang_analyzer_eval(a.values[1][1].x == 2); // expected-warning{{TRUE}}
467 clang_analyzer_eval(a.values[1][2].x == 3); // expected-warning{{TRUE}}
471 clang_analyzer_eval(b.values[1][0].x == 1); // expected-warning{{UNKNOWN}}
472 clang_analyzer_eval(b.values[1][1].x == 2); // expected-warning{{UNKNOWN}}
473 clang_analyzer_eval(b.values[1][2].x == 3); // expected-warning{{UNKNOWN}}
478 clang_analyzer_eval(c.values[1][0].x == 1); // expected-warning{{UNKNOWN}}
479 clang_analyzer_eval(c.values[1][1].x == 2); // expected-warning{{UNKNOWN}}
480 clang_analyzer_eval(c.values[1][2].x == 3); // expected-warning{{UNKNOWN}}
483 struct NonPODDefaulted {
484 NonPODIntWrapper values[3];
486 NonPODDefaulted() = default;
487 NonPODDefaulted(const NonPODDefaulted &) = default;
488 NonPODDefaulted &operator=(const NonPODDefaulted &) = default;
491 void testNonPODDefaulted() {
497 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
498 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
499 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
501 NonPODDefaulted b = a;
503 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
504 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
505 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
510 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
511 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
512 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
516 namespace VirtualInheritance {
525 struct virtual_subclass : public virtual base {
526 virtual_subclass() {}
529 struct double_subclass : public virtual_subclass {
536 clang_analyzer_eval(counter == 1); // expected-warning{{TRUE}}
539 struct double_virtual_subclass : public virtual virtual_subclass {
540 double_virtual_subclass() {}
545 double_virtual_subclass obj;
546 clang_analyzer_eval(counter == 1); // expected-warning{{TRUE}}
550 namespace ZeroInitialization {
558 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
559 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
562 void testTemporary() {
563 clang_analyzer_eval(raw_pair().p1 == 0); // expected-warning{{TRUE}}
564 clang_analyzer_eval(raw_pair().p2 == 0); // expected-warning{{TRUE}}
569 clang_analyzer_eval(p[0].p1 == 0); // expected-warning{{TRUE}}
570 clang_analyzer_eval(p[0].p2 == 0); // expected-warning{{TRUE}}
571 clang_analyzer_eval(p[1].p1 == 0); // expected-warning{{TRUE}}
572 clang_analyzer_eval(p[1].p2 == 0); // expected-warning{{TRUE}}
576 raw_pair *pp = new raw_pair();
577 clang_analyzer_eval(pp->p1 == 0); // expected-warning{{TRUE}}
578 clang_analyzer_eval(pp->p2 == 0); // expected-warning{{TRUE}}
581 void testArrayNew() {
582 raw_pair *p = new raw_pair[2]();
583 clang_analyzer_eval(p[0].p1 == 0); // expected-warning{{TRUE}}
584 clang_analyzer_eval(p[0].p2 == 0); // expected-warning{{TRUE}}
585 clang_analyzer_eval(p[1].p1 == 0); // expected-warning{{TRUE}}
586 clang_analyzer_eval(p[1].p2 == 0); // expected-warning{{TRUE}}
589 struct initializing_pair {
593 initializing_pair() : x(), y() {}
596 void testFieldInitializers() {
598 clang_analyzer_eval(p.x == 0); // expected-warning{{TRUE}}
599 clang_analyzer_eval(p.y.p1 == 0); // expected-warning{{TRUE}}
600 clang_analyzer_eval(p.y.p2 == 0); // expected-warning{{TRUE}}
603 struct subclass : public raw_pair {
604 subclass() = default;
607 void testSubclass() {
609 clang_analyzer_eval(p.p1 == 0); // expected-warning{{garbage}}
612 struct initializing_subclass : public raw_pair {
613 initializing_subclass() : raw_pair() {}
616 void testInitializingSubclass() {
617 initializing_subclass p;
618 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
619 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
622 struct pair_wrapper {
623 pair_wrapper() : p() {}
627 struct virtual_subclass : public virtual pair_wrapper {
628 virtual_subclass() {}
631 struct double_virtual_subclass : public virtual_subclass {
632 double_virtual_subclass() {
633 // This previously caused a crash because the pair_wrapper subobject was
634 // initialized twice.
642 Empty(int x); // Body below.
645 class PairContainer : public Empty {
649 PairContainer() : Empty(), p() {
650 // This previously caused a crash because the empty base class looked
651 // like an initialization of 'p'.
653 PairContainer(int) : Empty(), p() {
654 // Test inlining something else here.
656 PairContainer(double): Empty(1), p() {
657 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
658 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
660 clang_analyzer_eval(q == 1); // expected-warning{{TRUE}}
662 // This one's indeed UNKNOWN. Definitely not TRUE.
663 clang_analyzer_eval(p.p2 == glob); // expected-warning{{UNKNOWN}}
667 Empty::Empty(int x) {
668 static_cast<PairContainer *>(this)->p.p1 = x;
669 static_cast<PairContainer *>(this)->q = x;
670 // Our static member will store the old garbage values of fields that aren't
671 // yet initialized. It's not certainly garbage though (i.e. the constructor
672 // could have been called on an initialized piece of memory), so no
673 // uninitialized value warning here, and it should be a symbol, not
674 // undefined value, for later comparison.
675 glob = static_cast<PairContainer *>(this)->p.p2;
680 static int glob_p1, glob_p2;
681 Empty2(); // Body below.
684 class PairDoubleEmptyContainer: public Empty, public Empty2 {
687 PairDoubleEmptyContainer(): Empty(), Empty2(), p() {
688 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
689 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
691 // This is indeed UNKNOWN.
692 clang_analyzer_eval(p.p1 == glob_p1); // expected-warning{{UNKNOWN}}
693 clang_analyzer_eval(p.p2 == glob_p2); // expected-warning{{UNKNOWN}}
698 glob_p1 = static_cast<PairDoubleEmptyContainer *>(this)->p.p1;
699 glob_p2 = static_cast<PairDoubleEmptyContainer *>(this)->p.p2;
702 class PairContainerContainer {
706 PairContainerContainer() : pc(1) {}
710 namespace InitializerList {
712 bool usedInitializerList;
714 List() : usedInitializerList(false) {}
715 List(std::initializer_list<int>) : usedInitializerList(true) {}
720 clang_analyzer_eval(!defaultCtor.usedInitializerList); // expected-warning{{TRUE}}
723 clang_analyzer_eval(list.usedInitializerList); // expected-warning{{TRUE}}
727 List *list = new List{1, 2};
728 clang_analyzer_eval(list->usedInitializerList); // expected-warning{{TRUE}}
739 extern void use(int);
740 use(a); // expected-warning{{uninitialized}}
749 // This order triggers the initialization of the inner "a" after the
750 // constructor for "C" is run, which used to confuse the analyzer
751 // (is "C()" the initialization of "a"?).
762 namespace NoCrashOnEmptyBaseOptimization {
763 struct NonEmptyBase {
765 explicit NonEmptyBase(int X) : X(X) {}
770 struct S : NonEmptyBase, EmptyBase {
771 S() : NonEmptyBase(0), EmptyBase() {}
774 void testSCtorNoCrash() {
779 namespace EmptyBaseAssign {
781 struct B2 { int x; };
782 struct D: public B1, public B2 {
783 const D &operator=(const D &d) {
795 clang_analyzer_eval(d2.x == 1); // expected-warning{{TRUE}}
799 namespace vbase_zero_init {
807 static int glob_y, glob_z, glob_w;
812 class C : virtual public A {
817 class D : public B, public C {
819 // 'z', unlike 'w', resides in an area that would have been within padding of
820 // base class 'C' if it wasn't part of 'D', but only on 64-bit systems.
822 // Initialization order: A(), B(), C().
824 clang_analyzer_eval(x == 1); // expected-warning{{TRUE}}
825 clang_analyzer_eval(y == 0); // expected-warning{{TRUE}}
827 clang_analyzer_eval(z == 3); // expected-warning{{TRUE}}
829 // FIXME: Should be TRUE. Initialized in B().
830 clang_analyzer_eval(z == 3); // expected-warning{{UNKNOWN}}
832 clang_analyzer_eval(w == 4); // expected-warning{{TRUE}}
834 // FIXME: Should be UNKNOWN. Changed in B() since glob_y was assigned.
835 clang_analyzer_eval(y == glob_y); // expected-warning{{TRUE}}
838 clang_analyzer_eval(z == glob_z); // expected-warning{{UNKNOWN}}
840 // FIXME: Should be UNKNOWN. Changed in B() since glob_z was assigned.
841 clang_analyzer_eval(z == glob_z); // expected-warning{{TRUE}}
844 clang_analyzer_eval(w == glob_w); // expected-warning{{UNKNOWN}}
849 // Our static members will store the old garbage values of fields that aren't
850 // yet initialized. These aren't certainly garbage though (i.e. the
851 // constructor could have been called on an initialized piece of memory),
852 // so no uninitialized value warning here, and these should be symbols, not
853 // undefined values, for later comparison.
854 glob_y = static_cast<D *>(this)->y;
855 glob_z = static_cast<D *>(this)->z;
856 glob_w = static_cast<D *>(this)->w;
857 static_cast<D *>(this)->y = 2;
858 static_cast<D *>(this)->z = 3;
859 static_cast<D *>(this)->w = 4;