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 virtual int get() { return 1; }
68 virtual int get() { return 2; }
70 B(int *out1, int *out2) : A(out1) {
77 virtual int get() { return 3; }
79 C(int *out1, int *out2, int *out3) : B(out1, out2) {
88 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
89 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
90 clang_analyzer_eval(c == 3); // expected-warning{{TRUE}}
92 clang_analyzer_eval(obj.get() == 3); // expected-warning{{TRUE}}
94 // Correctness check for devirtualization.
96 clang_analyzer_eval(base->get() == 3); // expected-warning{{TRUE}}
100 namespace TemporaryConstructor {
104 clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
111 // PR13717 - Don't crash when a CXXTemporaryObjectExpr is inlined.
112 if (BoolWrapper().value)
118 namespace ConstructorUsedAsRValue {
119 using TemporaryConstructor::BoolWrapper;
121 bool extractValue(BoolWrapper b) {
126 bool result = extractValue(BoolWrapper());
127 clang_analyzer_eval(result); // expected-warning{{TRUE}}
131 namespace PODUninitialized {
147 NonPOD(const NonPOD &Other)
148 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
151 NonPOD(NonPOD &&Other)
152 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
156 NonPOD &operator=(const NonPOD &Other)
159 y = Other.y; // expected-warning {{undefined}}
162 NonPOD &operator=(NonPOD &&Other)
165 y = Other.y; // expected-warning {{undefined}}
170 class NonPODWrapper {
177 Inner(const Inner &Other)
178 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
182 : x(Other.x), y(Other.y) // expected-warning {{undefined}}
186 Inner &operator=(const Inner &Other)
188 x = Other.x; // expected-warning {{undefined}}
192 Inner &operator=(Inner &&Other)
194 x = Other.x; // expected-warning {{undefined}}
203 void testPOD(const POD &pp) {
206 POD p2 = p; // no-warning
207 clang_analyzer_eval(p2.x == 1); // expected-warning{{TRUE}}
208 POD p3 = move(p); // no-warning
209 clang_analyzer_eval(p3.x == 1); // expected-warning{{TRUE}}
211 // Use rvalues as well.
212 clang_analyzer_eval(POD(p3).x == 1); // expected-warning{{TRUE}}
214 // Copy from symbolic references correctly.
216 // Make sure that p4.x contains a symbol after copy.
218 clang_analyzer_eval(p4.x > 0); // expected-warning{{TRUE}}
219 clang_analyzer_eval(pp.x == p4.x); // expected-warning{{TRUE}}
223 PODWrapper w2 = w; // no-warning
224 clang_analyzer_eval(w2.p.y == 1); // expected-warning{{TRUE}}
225 PODWrapper w3 = move(w); // no-warning
226 clang_analyzer_eval(w3.p.y == 1); // expected-warning{{TRUE}}
228 // Use rvalues as well.
229 clang_analyzer_eval(PODWrapper(w3).p.y == 1); // expected-warning{{TRUE}}
238 void testNonPODMove() {
244 void testNonPODWrapper() {
247 NonPODWrapper w2 = w;
250 void testNonPODWrapperMove() {
253 NonPODWrapper w2 = move(w);
256 // Not strictly about constructors, but trivial assignment operators should
257 // essentially work the same way.
258 namespace AssignmentOperator {
263 p2 = p; // no-warning
264 clang_analyzer_eval(p2.x == 1); // expected-warning{{TRUE}}
266 p3 = move(p); // no-warning
267 clang_analyzer_eval(p3.x == 1); // expected-warning{{TRUE}}
272 w2 = w; // no-warning
273 clang_analyzer_eval(w2.p.y == 1); // expected-warning{{TRUE}}
275 w3 = move(w); // no-warning
276 clang_analyzer_eval(w3.p.y == 1); // expected-warning{{TRUE}}
279 void testReturnValue() {
283 clang_analyzer_eval(&(p2 = p) == &p2); // expected-warning{{TRUE}}
288 clang_analyzer_eval(&(w2 = w) == &w2); // expected-warning{{TRUE}}
298 void testNonPODMove() {
305 void testNonPODWrapper() {
312 void testNonPODWrapperMove() {
321 namespace ArrayMembers {
326 void testPrimitive() {
327 Primitive a = { { 1, 2, 3 } };
329 clang_analyzer_eval(a.values[0] == 1); // expected-warning{{TRUE}}
330 clang_analyzer_eval(a.values[1] == 2); // expected-warning{{TRUE}}
331 clang_analyzer_eval(a.values[2] == 3); // expected-warning{{TRUE}}
335 clang_analyzer_eval(b.values[0] == 1); // expected-warning{{TRUE}}
336 clang_analyzer_eval(b.values[1] == 2); // expected-warning{{TRUE}}
337 clang_analyzer_eval(b.values[2] == 3); // expected-warning{{TRUE}}
342 clang_analyzer_eval(c.values[0] == 1); // expected-warning{{TRUE}}
343 clang_analyzer_eval(c.values[1] == 2); // expected-warning{{TRUE}}
344 clang_analyzer_eval(c.values[2] == 3); // expected-warning{{TRUE}}
347 struct NestedPrimitive {
351 void testNestedPrimitive() {
352 NestedPrimitive a = { { { 0, 0, 0 }, { 1, 2, 3 } } };
354 clang_analyzer_eval(a.values[1][0] == 1); // expected-warning{{TRUE}}
355 clang_analyzer_eval(a.values[1][1] == 2); // expected-warning{{TRUE}}
356 clang_analyzer_eval(a.values[1][2] == 3); // expected-warning{{TRUE}}
358 NestedPrimitive b = a;
360 clang_analyzer_eval(b.values[1][0] == 1); // expected-warning{{TRUE}}
361 clang_analyzer_eval(b.values[1][1] == 2); // expected-warning{{TRUE}}
362 clang_analyzer_eval(b.values[1][2] == 3); // expected-warning{{TRUE}}
367 clang_analyzer_eval(c.values[1][0] == 1); // expected-warning{{TRUE}}
368 clang_analyzer_eval(c.values[1][1] == 2); // expected-warning{{TRUE}}
369 clang_analyzer_eval(c.values[1][2] == 3); // expected-warning{{TRUE}}
373 IntWrapper values[3];
377 POD a = { { { 1 }, { 2 }, { 3 } } };
379 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
380 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
381 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
385 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
386 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
387 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
392 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
393 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
394 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
398 IntWrapper values[2][3];
401 void testNestedPOD() {
402 NestedPOD a = { { { { 0 }, { 0 }, { 0 } }, { { 1 }, { 2 }, { 3 } } } };
404 clang_analyzer_eval(a.values[1][0].x == 1); // expected-warning{{TRUE}}
405 clang_analyzer_eval(a.values[1][1].x == 2); // expected-warning{{TRUE}}
406 clang_analyzer_eval(a.values[1][2].x == 3); // expected-warning{{TRUE}}
410 clang_analyzer_eval(b.values[1][0].x == 1); // expected-warning{{TRUE}}
411 clang_analyzer_eval(b.values[1][1].x == 2); // expected-warning{{TRUE}}
412 clang_analyzer_eval(b.values[1][2].x == 3); // expected-warning{{TRUE}}
417 clang_analyzer_eval(c.values[1][0].x == 1); // expected-warning{{TRUE}}
418 clang_analyzer_eval(c.values[1][1].x == 2); // expected-warning{{TRUE}}
419 clang_analyzer_eval(c.values[1][2].x == 3); // expected-warning{{TRUE}}
423 NonPODIntWrapper values[3];
432 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
433 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
434 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
438 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
439 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
440 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
445 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
446 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
447 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
450 struct NestedNonPOD {
451 NonPODIntWrapper values[2][3];
454 void testNestedNonPOD() {
456 a.values[0][0].x = 0;
457 a.values[0][1].x = 0;
458 a.values[0][2].x = 0;
459 a.values[1][0].x = 1;
460 a.values[1][1].x = 2;
461 a.values[1][2].x = 3;
463 clang_analyzer_eval(a.values[1][0].x == 1); // expected-warning{{TRUE}}
464 clang_analyzer_eval(a.values[1][1].x == 2); // expected-warning{{TRUE}}
465 clang_analyzer_eval(a.values[1][2].x == 3); // expected-warning{{TRUE}}
469 clang_analyzer_eval(b.values[1][0].x == 1); // expected-warning{{UNKNOWN}}
470 clang_analyzer_eval(b.values[1][1].x == 2); // expected-warning{{UNKNOWN}}
471 clang_analyzer_eval(b.values[1][2].x == 3); // expected-warning{{UNKNOWN}}
476 clang_analyzer_eval(c.values[1][0].x == 1); // expected-warning{{UNKNOWN}}
477 clang_analyzer_eval(c.values[1][1].x == 2); // expected-warning{{UNKNOWN}}
478 clang_analyzer_eval(c.values[1][2].x == 3); // expected-warning{{UNKNOWN}}
481 struct NonPODDefaulted {
482 NonPODIntWrapper values[3];
484 NonPODDefaulted() = default;
485 NonPODDefaulted(const NonPODDefaulted &) = default;
486 NonPODDefaulted &operator=(const NonPODDefaulted &) = default;
489 void testNonPODDefaulted() {
495 clang_analyzer_eval(a.values[0].x == 1); // expected-warning{{TRUE}}
496 clang_analyzer_eval(a.values[1].x == 2); // expected-warning{{TRUE}}
497 clang_analyzer_eval(a.values[2].x == 3); // expected-warning{{TRUE}}
499 NonPODDefaulted b = a;
501 clang_analyzer_eval(b.values[0].x == 1); // expected-warning{{TRUE}}
502 clang_analyzer_eval(b.values[1].x == 2); // expected-warning{{TRUE}}
503 clang_analyzer_eval(b.values[2].x == 3); // expected-warning{{TRUE}}
508 clang_analyzer_eval(c.values[0].x == 1); // expected-warning{{TRUE}}
509 clang_analyzer_eval(c.values[1].x == 2); // expected-warning{{TRUE}}
510 clang_analyzer_eval(c.values[2].x == 3); // expected-warning{{TRUE}}
514 namespace VirtualInheritance {
523 struct virtual_subclass : public virtual base {
524 virtual_subclass() {}
527 struct double_subclass : public virtual_subclass {
534 clang_analyzer_eval(counter == 1); // expected-warning{{TRUE}}
537 struct double_virtual_subclass : public virtual virtual_subclass {
538 double_virtual_subclass() {}
543 double_virtual_subclass obj;
544 clang_analyzer_eval(counter == 1); // expected-warning{{TRUE}}
548 namespace ZeroInitialization {
556 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
557 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
560 void testTemporary() {
561 clang_analyzer_eval(raw_pair().p1 == 0); // expected-warning{{TRUE}}
562 clang_analyzer_eval(raw_pair().p2 == 0); // expected-warning{{TRUE}}
567 clang_analyzer_eval(p[0].p1 == 0); // expected-warning{{TRUE}}
568 clang_analyzer_eval(p[0].p2 == 0); // expected-warning{{TRUE}}
569 clang_analyzer_eval(p[1].p1 == 0); // expected-warning{{TRUE}}
570 clang_analyzer_eval(p[1].p2 == 0); // expected-warning{{TRUE}}
574 raw_pair *pp = new raw_pair();
575 clang_analyzer_eval(pp->p1 == 0); // expected-warning{{TRUE}}
576 clang_analyzer_eval(pp->p2 == 0); // expected-warning{{TRUE}}
579 void testArrayNew() {
580 raw_pair *p = new raw_pair[2]();
581 clang_analyzer_eval(p[0].p1 == 0); // expected-warning{{TRUE}}
582 clang_analyzer_eval(p[0].p2 == 0); // expected-warning{{TRUE}}
583 clang_analyzer_eval(p[1].p1 == 0); // expected-warning{{TRUE}}
584 clang_analyzer_eval(p[1].p2 == 0); // expected-warning{{TRUE}}
587 struct initializing_pair {
591 initializing_pair() : x(), y() {}
594 void testFieldInitializers() {
596 clang_analyzer_eval(p.x == 0); // expected-warning{{TRUE}}
597 clang_analyzer_eval(p.y.p1 == 0); // expected-warning{{TRUE}}
598 clang_analyzer_eval(p.y.p2 == 0); // expected-warning{{TRUE}}
601 struct subclass : public raw_pair {
602 subclass() = default;
605 void testSubclass() {
607 clang_analyzer_eval(p.p1 == 0); // expected-warning{{garbage}}
610 struct initializing_subclass : public raw_pair {
611 initializing_subclass() : raw_pair() {}
614 void testInitializingSubclass() {
615 initializing_subclass p;
616 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
617 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
620 struct pair_wrapper {
621 pair_wrapper() : p() {}
625 struct virtual_subclass : public virtual pair_wrapper {
626 virtual_subclass() {}
629 struct double_virtual_subclass : public virtual_subclass {
630 double_virtual_subclass() {
631 // This previously caused a crash because the pair_wrapper subobject was
632 // initialized twice.
640 Empty(int x); // Body below.
643 class PairContainer : public Empty {
647 PairContainer() : Empty(), p() {
648 // This previously caused a crash because the empty base class looked
649 // like an initialization of 'p'.
651 PairContainer(int) : Empty(), p() {
652 // Test inlining something else here.
654 PairContainer(double): Empty(1), p() {
655 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
656 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
658 clang_analyzer_eval(q == 1); // expected-warning{{TRUE}}
660 // This one's indeed UNKNOWN. Definitely not TRUE.
661 clang_analyzer_eval(p.p2 == glob); // expected-warning{{UNKNOWN}}
665 Empty::Empty(int x) {
666 static_cast<PairContainer *>(this)->p.p1 = x;
667 static_cast<PairContainer *>(this)->q = x;
668 // Our static member will store the old garbage values of fields that aren't
669 // yet initialized. It's not certainly garbage though (i.e. the constructor
670 // could have been called on an initialized piece of memory), so no
671 // uninitialized value warning here, and it should be a symbol, not
672 // undefined value, for later comparison.
673 glob = static_cast<PairContainer *>(this)->p.p2;
678 static int glob_p1, glob_p2;
679 Empty2(); // Body below.
682 class PairDoubleEmptyContainer: public Empty, public Empty2 {
685 PairDoubleEmptyContainer(): Empty(), Empty2(), p() {
686 clang_analyzer_eval(p.p1 == 0); // expected-warning{{TRUE}}
687 clang_analyzer_eval(p.p2 == 0); // expected-warning{{TRUE}}
689 // This is indeed UNKNOWN.
690 clang_analyzer_eval(p.p1 == glob_p1); // expected-warning{{UNKNOWN}}
691 clang_analyzer_eval(p.p2 == glob_p2); // expected-warning{{UNKNOWN}}
696 glob_p1 = static_cast<PairDoubleEmptyContainer *>(this)->p.p1;
697 glob_p2 = static_cast<PairDoubleEmptyContainer *>(this)->p.p2;
700 class PairContainerContainer {
704 PairContainerContainer() : pc(1) {}
708 namespace InitializerList {
710 bool usedInitializerList;
712 List() : usedInitializerList(false) {}
713 List(std::initializer_list<int>) : usedInitializerList(true) {}
718 clang_analyzer_eval(!defaultCtor.usedInitializerList); // expected-warning{{TRUE}}
721 clang_analyzer_eval(list.usedInitializerList); // expected-warning{{TRUE}}
725 List *list = new List{1, 2};
726 clang_analyzer_eval(list->usedInitializerList); // expected-warning{{TRUE}}
737 extern void use(int);
738 use(a); // expected-warning{{uninitialized}}
747 // This order triggers the initialization of the inner "a" after the
748 // constructor for "C" is run, which used to confuse the analyzer
749 // (is "C()" the initialization of "a"?).
760 namespace NoCrashOnEmptyBaseOptimization {
761 struct NonEmptyBase {
763 explicit NonEmptyBase(int X) : X(X) {}
768 struct S : NonEmptyBase, EmptyBase {
769 S() : NonEmptyBase(0), EmptyBase() {}
772 void testSCtorNoCrash() {
777 namespace EmptyBaseAssign {
779 struct B2 { int x; };
780 struct D: public B1, public B2 {
781 const D &operator=(const D &d) {
793 clang_analyzer_eval(d2.x == 1); // expected-warning{{TRUE}}
797 namespace vbase_zero_init {
805 static int glob_y, glob_z, glob_w;
810 class C : virtual public A {
815 class D : public B, public C {
817 // 'z', unlike 'w', resides in an area that would have been within padding of
818 // base class 'C' if it wasn't part of 'D', but only on 64-bit systems.
820 // Initialization order: A(), B(), C().
822 clang_analyzer_eval(x == 1); // expected-warning{{TRUE}}
823 clang_analyzer_eval(y == 0); // expected-warning{{TRUE}}
825 clang_analyzer_eval(z == 3); // expected-warning{{TRUE}}
827 // FIXME: Should be TRUE. Initialized in B().
828 clang_analyzer_eval(z == 3); // expected-warning{{UNKNOWN}}
830 clang_analyzer_eval(w == 4); // expected-warning{{TRUE}}
832 // FIXME: Should be UNKNOWN. Changed in B() since glob_y was assigned.
833 clang_analyzer_eval(y == glob_y); // expected-warning{{TRUE}}
836 clang_analyzer_eval(z == glob_z); // expected-warning{{UNKNOWN}}
838 // FIXME: Should be UNKNOWN. Changed in B() since glob_z was assigned.
839 clang_analyzer_eval(z == glob_z); // expected-warning{{TRUE}}
842 clang_analyzer_eval(w == glob_w); // expected-warning{{UNKNOWN}}
847 // Our static members will store the old garbage values of fields that aren't
848 // yet initialized. These aren't certainly garbage though (i.e. the
849 // constructor could have been called on an initialized piece of memory),
850 // so no uninitialized value warning here, and these should be symbols, not
851 // undefined values, for later comparison.
852 glob_y = static_cast<D *>(this)->y;
853 glob_z = static_cast<D *>(this)->z;
854 glob_w = static_cast<D *>(this)->w;
855 static_cast<D *>(this)->y = 2;
856 static_cast<D *>(this)->z = 3;
857 static_cast<D *>(this)->w = 4;