initializer.cpp revision 07c52d2813a6b5e4025276d3687bd25f75fd51b9
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config c++-inlining=constructors -std=c++11 -verify %s 2 3void clang_analyzer_eval(bool); 4 5class A { 6 int x; 7public: 8 A(); 9}; 10 11A::A() : x(0) { 12 clang_analyzer_eval(x == 0); // expected-warning{{TRUE}} 13} 14 15 16class DirectMember { 17 int x; 18public: 19 DirectMember(int value) : x(value) {} 20 21 int getX() { return x; } 22}; 23 24void testDirectMember() { 25 DirectMember obj(3); 26 clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} 27} 28 29 30class IndirectMember { 31 struct { 32 int x; 33 }; 34public: 35 IndirectMember(int value) : x(value) {} 36 37 int getX() { return x; } 38}; 39 40void testIndirectMember() { 41 IndirectMember obj(3); 42 clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} 43} 44 45 46struct DelegatingConstructor { 47 int x; 48 DelegatingConstructor(int y) { x = y; } 49 DelegatingConstructor() : DelegatingConstructor(42) {} 50}; 51 52void testDelegatingConstructor() { 53 DelegatingConstructor obj; 54 clang_analyzer_eval(obj.x == 42); // expected-warning{{TRUE}} 55} 56 57 58struct RefWrapper { 59 RefWrapper(int *p) : x(*p) {} 60 RefWrapper(int &r) : x(r) {} 61 int &x; 62}; 63 64void testReferenceMember() { 65 int *p = 0; 66 RefWrapper X(p); // expected-warning@-7 {{Dereference of null pointer}} 67} 68 69void testReferenceMember2() { 70 int *p = 0; 71 // FIXME: We should warn here, since we're creating the reference here. 72 RefWrapper X(*p); // expected-warning@-12 {{Dereference of null pointer}} 73} 74 75 76extern "C" char *strdup(const char *); 77 78class StringWrapper { 79 char *str; 80public: 81 StringWrapper(const char *input) : str(strdup(input)) {} // no-warning 82}; 83 84 85// PR15070 - Constructing a type containing a non-POD array mistakenly 86// tried to perform a bind instead of relying on the CXXConstructExpr, 87// which caused a cast<> failure in RegionStore. 88namespace DefaultConstructorWithCleanups { 89 class Element { 90 public: 91 int value; 92 93 class Helper { 94 public: 95 ~Helper(); 96 }; 97 Element(Helper h = Helper()); 98 }; 99 class Wrapper { 100 public: 101 Element arr[2]; 102 103 Wrapper(); 104 }; 105 106 Wrapper::Wrapper() /* initializers synthesized */ {} 107 108 int test() { 109 Wrapper w; 110 return w.arr[0].value; // no-warning 111 } 112} 113