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 // FIXME: Element region gets in the way, so these aren't the same symbols
222 // as they should be.
223 clang_analyzer_eval(pp.x == p4.x); // expected-warning{{UNKNOWN}}
227 PODWrapper w2 = w; // no-warning
228 clang_analyzer_eval(w2.p.y == 1); // expected-warning{{TRUE}}
229 PODWrapper w3 = move(w); // no-warning
230 clang_analyzer_eval(w3.p.y == 1); // expected-warning{{TRUE}}
232 // Use rvalues as well.
233 clang_analyzer_eval(PODWrapper(w3).p.y == 1); // expected-warning{{TRUE}}
242 void testNonPODMove() {
248 void testNonPODWrapper() {
251 NonPODWrapper w2 = w;
254 void testNonPODWrapperMove() {
257 NonPODWrapper w2 = move(w);
260 // Not strictly about constructors, but trivial assignment operators should
261 // essentially work the same way.
262 namespace AssignmentOperator {
267 p2 = p; // no-warning
268 clang_analyzer_eval(p2.x == 1); // expected-warning{{TRUE}}
270 p3 = move(p); // no-warning
271 clang_analyzer_eval(p3.x == 1); // expected-warning{{TRUE}}
276 w2 = w; // no-warning
277 clang_analyzer_eval(w2.p.y == 1); // expected-warning{{TRUE}}
279 w3 = move(w); // no-warning
280 clang_analyzer_eval(w3.p.y == 1); // expected-warning{{TRUE}}
283 void testReturnValue() {
287 clang_analyzer_eval(&(p2 = p) == &p2); // expected-warning{{TRUE}}
292 clang_analyzer_eval(&(w2 = w) == &w2); // expected-warning{{TRUE}}
302 void testNonPODMove() {
309 void testNonPODWrapper() {
316 void testNonPODWrapperMove() {
325 namespace ArrayMembers {
330 void testPrimitive() {
331 Primitive a = { { 1, 2, 3 } };
333 clang_analyzer_eval(a.values[0] == 1); // expected-warning{{TRUE}}
334 clang_analyzer_eval(a.values[1] == 2); // expected-warning{{TRUE}}
335 clang_analyzer_eval(a.values[2] == 3); // expected-warning{{TRUE}}
339 clang_analyzer_eval(b.values[0] == 1); // expected-warning{{TRUE}}
340 clang_analyzer_eval(b.values[1] == 2); // expected-warning{{TRUE}}
341 clang_analyzer_eval(b.values[2] == 3); // expected-warning{{TRUE}}
346 clang_analyzer_eval(c.values[0] == 1); // expected-warning{{TRUE}}
347 clang_analyzer_eval(c.values[1] == 2); // expected-warning{{TRUE}}
348 clang_analyzer_eval(c.values[2] == 3); // expected-warning{{TRUE}}
351 struct NestedPrimitive {
355 void testNestedPrimitive() {
356 NestedPrimitive a = { { { 0, 0, 0 }, { 1, 2, 3 } } };
358 clang_analyzer_eval(a.values[1][0] == 1); // expected-warning{{TRUE}}
359 clang_analyzer_eval(a.values[1][1] == 2); // expected-warning{{TRUE}}
360 clang_analyzer_eval(a.values[1][2] == 3); // expected-warning{{TRUE}}
362 NestedPrimitive b = a;
364 clang_analyzer_eval(b.values[1][0] == 1); // expected-warning{{TRUE}}
365 clang_analyzer_eval(b.values[1][1] == 2); // expected-warning{{TRUE}}
366 clang_analyzer_eval(b.values[1][2] == 3); // expected-warning{{TRUE}}
371 clang_analyzer_eval(c.values[1][0] == 1); // expected-warning{{TRUE}}
372 clang_analyzer_eval(c.values[1][1] == 2); // expected-warning{{TRUE}}
373 clang_analyzer_eval(c.values[1][2] == 3); // expected-warning{{TRUE}}
377 IntWrapper values[3];
381 POD a = { { { 1 }, { 2 }, { 3 } } };
383 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
384 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
385 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
389 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
390 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
391 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
396 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
397 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
398 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
402 IntWrapper values[2][3];
405 void testNestedPOD() {
406 NestedPOD a = { { { { 0 }, { 0 }, { 0 } }, { { 1 }, { 2 }, { 3 } } } };
408 clang_analyzer_eval(a.values[1][0].x == 1); // expected-warning{{TRUE}}
409 clang_analyzer_eval(a.values[1][1].x == 2); // expected-warning{{TRUE}}
410 clang_analyzer_eval(a.values[1][2].x == 3); // expected-warning{{TRUE}}
414 clang_analyzer_eval(b.values[1][0].x == 1); // expected-warning{{TRUE}}
415 clang_analyzer_eval(b.values[1][1].x == 2); // expected-warning{{TRUE}}
416 clang_analyzer_eval(b.values[1][2].x == 3); // expected-warning{{TRUE}}
421 clang_analyzer_eval(c.values[1][0].x == 1); // expected-warning{{TRUE}}
422 clang_analyzer_eval(c.values[1][1].x == 2); // expected-warning{{TRUE}}
423 clang_analyzer_eval(c.values[1][2].x == 3); // expected-warning{{TRUE}}
427 NonPODIntWrapper values[3];
436 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
437 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
438 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
442 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{UNKNOWN}}
443 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{UNKNOWN}}
444 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{UNKNOWN}}
449 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{UNKNOWN}}
450 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{UNKNOWN}}
451 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{UNKNOWN}}
454 struct NestedNonPOD {
455 NonPODIntWrapper values[2][3];
458 void testNestedNonPOD() {
460 a.values[0][0].x = 0;
461 a.values[0][1].x = 0;
462 a.values[0][2].x = 0;
463 a.values[1][0].x = 1;
464 a.values[1][1].x = 2;
465 a.values[1][2].x = 3;
467 clang_analyzer_eval(a.values[1][0].x == 1); // expected-warning{{TRUE}}
468 clang_analyzer_eval(a.values[1][1].x == 2); // expected-warning{{TRUE}}
469 clang_analyzer_eval(a.values[1][2].x == 3); // expected-warning{{TRUE}}
473 clang_analyzer_eval(b.values[1][0].x == 1); // expected-warning{{UNKNOWN}}
474 clang_analyzer_eval(b.values[1][1].x == 2); // expected-warning{{UNKNOWN}}
475 clang_analyzer_eval(b.values[1][2].x == 3); // expected-warning{{UNKNOWN}}
480 clang_analyzer_eval(c.values[1][0].x == 1); // expected-warning{{UNKNOWN}}
481 clang_analyzer_eval(c.values[1][1].x == 2); // expected-warning{{UNKNOWN}}
482 clang_analyzer_eval(c.values[1][2].x == 3); // expected-warning{{UNKNOWN}}
485 struct NonPODDefaulted {
486 NonPODIntWrapper values[3];
488 NonPODDefaulted() = default;
489 NonPODDefaulted(const NonPODDefaulted &) = default;
490 NonPODDefaulted &operator=(const NonPODDefaulted &) = default;
493 void testNonPODDefaulted() {
499 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
500 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
501 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
503 NonPODDefaulted b = a;
505 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{UNKNOWN}}
506 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{UNKNOWN}}
507 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{UNKNOWN}}
512 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{UNKNOWN}}
513 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{UNKNOWN}}
514 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{UNKNOWN}}
518 namespace VirtualInheritance {
527 struct virtual_subclass : public virtual base {
528 virtual_subclass() {}
531 struct double_subclass : public virtual_subclass {
538 clang_analyzer_eval(counter == 1); // expected-warning{{TRUE}}
541 struct double_virtual_subclass : public virtual virtual_subclass {
542 double_virtual_subclass() {}
547 double_virtual_subclass obj;
548 clang_analyzer_eval(counter == 1); // expected-warning{{TRUE}}
552 namespace ZeroInitialization {
560 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
561 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
564 void testTemporary() {
565 clang_analyzer_eval(raw_pair().p1 == 0); // expected-warning{{TRUE}}
566 clang_analyzer_eval(raw_pair().p2 == 0); // expected-warning{{TRUE}}
571 clang_analyzer_eval(p[0].p1 == 0); // expected-warning{{TRUE}}
572 clang_analyzer_eval(p[0].p2 == 0); // expected-warning{{TRUE}}
573 clang_analyzer_eval(p[1].p1 == 0); // expected-warning{{TRUE}}
574 clang_analyzer_eval(p[1].p2 == 0); // expected-warning{{TRUE}}
578 raw_pair *pp = new raw_pair();
579 clang_analyzer_eval(pp->p1 == 0); // expected-warning{{TRUE}}
580 clang_analyzer_eval(pp->p2 == 0); // expected-warning{{TRUE}}
583 void testArrayNew() {
584 // FIXME: Pending proper implementation of constructors for 'new[]'.
585 raw_pair *p = new raw_pair[2]();
586 clang_analyzer_eval(p[0].p1 == 0); // expected-warning{{UNKNOWN}}
587 clang_analyzer_eval(p[0].p2 == 0); // expected-warning{{UNKNOWN}}
588 clang_analyzer_eval(p[1].p1 == 0); // expected-warning{{UNKNOWN}}
589 clang_analyzer_eval(p[1].p2 == 0); // expected-warning{{UNKNOWN}}
592 struct initializing_pair {
596 initializing_pair() : x(), y() {}
599 void testFieldInitializers() {
601 clang_analyzer_eval(p.x == 0); // expected-warning{{TRUE}}
602 clang_analyzer_eval(p.y.p1 == 0); // expected-warning{{TRUE}}
603 clang_analyzer_eval(p.y.p2 == 0); // expected-warning{{TRUE}}
606 struct subclass : public raw_pair {
607 subclass() = default;
610 void testSubclass() {
612 clang_analyzer_eval(p.p1 == 0); // expected-warning{{garbage}}
615 struct initializing_subclass : public raw_pair {
616 initializing_subclass() : raw_pair() {}
619 void testInitializingSubclass() {
620 initializing_subclass p;
621 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
622 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
625 struct pair_wrapper {
626 pair_wrapper() : p() {}
630 struct virtual_subclass : public virtual pair_wrapper {
631 virtual_subclass() {}
634 struct double_virtual_subclass : public virtual_subclass {
635 double_virtual_subclass() {
636 // This previously caused a crash because the pair_wrapper subobject was
637 // initialized twice.
645 Empty(int x); // Body below.
648 class PairContainer : public Empty {
652 PairContainer() : Empty(), p() {
653 // This previously caused a crash because the empty base class looked
654 // like an initialization of 'p'.
656 PairContainer(int) : Empty(), p() {
657 // Test inlining something else here.
659 PairContainer(double): Empty(1), p() {
660 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
661 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
663 clang_analyzer_eval(q == 1); // expected-warning{{TRUE}}
665 // This one's indeed UNKNOWN. Definitely not TRUE.
666 clang_analyzer_eval(p.p2 == glob); // expected-warning{{UNKNOWN}}
670 Empty::Empty(int x) {
671 static_cast<PairContainer *>(this)->p.p1 = x;
672 static_cast<PairContainer *>(this)->q = x;
673 // Our static member will store the old garbage values of fields that aren't
674 // yet initialized. It's not certainly garbage though (i.e. the constructor
675 // could have been called on an initialized piece of memory), so no
676 // uninitialized value warning here, and it should be a symbol, not
677 // undefined value, for later comparison.
678 glob = static_cast<PairContainer *>(this)->p.p2;
683 static int glob_p1, glob_p2;
684 Empty2(); // Body below.
687 class PairDoubleEmptyContainer: public Empty, public Empty2 {
690 PairDoubleEmptyContainer(): Empty(), Empty2(), p() {
691 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
692 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
694 // This is indeed UNKNOWN.
695 clang_analyzer_eval(p.p1 == glob_p1); // expected-warning{{UNKNOWN}}
696 clang_analyzer_eval(p.p2 == glob_p2); // expected-warning{{UNKNOWN}}
701 glob_p1 = static_cast<PairDoubleEmptyContainer *>(this)->p.p1;
702 glob_p2 = static_cast<PairDoubleEmptyContainer *>(this)->p.p2;
705 class PairContainerContainer {
709 PairContainerContainer() : pc(1) {}
713 namespace InitializerList {
715 bool usedInitializerList;
717 List() : usedInitializerList(false) {}
718 List(std::initializer_list<int>) : usedInitializerList(true) {}
723 clang_analyzer_eval(!defaultCtor.usedInitializerList); // expected-warning{{TRUE}}
726 clang_analyzer_eval(list.usedInitializerList); // expected-warning{{TRUE}}
730 List *list = new List{1, 2};
731 clang_analyzer_eval(list->usedInitializerList); // expected-warning{{TRUE}}
742 extern void use(int);
743 use(a); // expected-warning{{uninitialized}}
752 // This order triggers the initialization of the inner "a" after the
753 // constructor for "C" is run, which used to confuse the analyzer
754 // (is "C()" the initialization of "a"?).
765 namespace NoCrashOnEmptyBaseOptimization {
766 struct NonEmptyBase {
768 explicit NonEmptyBase(int X) : X(X) {}
773 struct S : NonEmptyBase, EmptyBase {
774 S() : NonEmptyBase(0), EmptyBase() {}
777 void testSCtorNoCrash() {
782 namespace EmptyBaseAssign {
784 struct B2 { int x; };
785 struct D: public B1, public B2 {
786 const D &operator=(const D &d) {
798 clang_analyzer_eval(d2.x == 1); // expected-warning{{TRUE}}
802 namespace vbase_zero_init {
810 static int glob_y, glob_z, glob_w;
815 class C : virtual public A {
820 class D : public B, public C {
822 // 'z', unlike 'w', resides in an area that would have been within padding of
823 // base class 'C' if it wasn't part of 'D', but only on 64-bit systems.
825 // Initialization order: A(), B(), C().
827 clang_analyzer_eval(x == 1); // expected-warning{{TRUE}}
828 clang_analyzer_eval(y == 0); // expected-warning{{TRUE}}
830 clang_analyzer_eval(z == 3); // expected-warning{{TRUE}}
832 // FIXME: Should be TRUE. Initialized in B().
833 clang_analyzer_eval(z == 3); // expected-warning{{UNKNOWN}}
835 clang_analyzer_eval(w == 4); // expected-warning{{TRUE}}
837 // FIXME: Should be UNKNOWN. Changed in B() since glob_y was assigned.
838 clang_analyzer_eval(y == glob_y); // expected-warning{{TRUE}}
841 clang_analyzer_eval(z == glob_z); // expected-warning{{UNKNOWN}}
843 // FIXME: Should be UNKNOWN. Changed in B() since glob_z was assigned.
844 clang_analyzer_eval(z == glob_z); // expected-warning{{TRUE}}
847 clang_analyzer_eval(w == glob_w); // expected-warning{{UNKNOWN}}
852 // Our static members will store the old garbage values of fields that aren't
853 // yet initialized. These aren't certainly garbage though (i.e. the
854 // constructor could have been called on an initialized piece of memory),
855 // so no uninitialized value warning here, and these should be symbols, not
856 // undefined values, for later comparison.
857 glob_y = static_cast<D *>(this)->y;
858 glob_z = static_cast<D *>(this)->z;
859 glob_w = static_cast<D *>(this)->w;
860 static_cast<D *>(this)->y = 2;
861 static_cast<D *>(this)->z = 3;
862 static_cast<D *>(this)->w = 4;