Anna Zaks | bfa9ab8 | 2013-01-24 23:15:30 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config c++-inlining=constructors -std=c++11 -verify %s |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 2 | |
| 3 | void clang_analyzer_eval(bool); |
Zhongxing Xu | 9dc84c9 | 2010-11-16 07:52:17 +0000 | [diff] [blame] | 4 | |
| 5 | class A { |
| 6 | int x; |
| 7 | public: |
| 8 | A(); |
| 9 | }; |
| 10 | |
| 11 | A::A() : x(0) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 12 | clang_analyzer_eval(x == 0); // expected-warning{{TRUE}} |
Zhongxing Xu | 9dc84c9 | 2010-11-16 07:52:17 +0000 | [diff] [blame] | 13 | } |
Jordan Rose | 3a0a9e3 | 2012-07-26 20:04:21 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | class DirectMember { |
| 17 | int x; |
| 18 | public: |
| 19 | DirectMember(int value) : x(value) {} |
| 20 | |
| 21 | int getX() { return x; } |
| 22 | }; |
| 23 | |
| 24 | void testDirectMember() { |
| 25 | DirectMember obj(3); |
| 26 | clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} |
| 27 | } |
| 28 | |
| 29 | |
| 30 | class IndirectMember { |
| 31 | struct { |
| 32 | int x; |
| 33 | }; |
| 34 | public: |
| 35 | IndirectMember(int value) : x(value) {} |
| 36 | |
| 37 | int getX() { return x; } |
| 38 | }; |
| 39 | |
| 40 | void testIndirectMember() { |
| 41 | IndirectMember obj(3); |
| 42 | clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} |
| 43 | } |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 44 | |
| 45 | |
Jordan Rose | 563ea23 | 2012-08-03 23:31:15 +0000 | [diff] [blame] | 46 | struct DelegatingConstructor { |
| 47 | int x; |
| 48 | DelegatingConstructor(int y) { x = y; } |
| 49 | DelegatingConstructor() : DelegatingConstructor(42) {} |
| 50 | }; |
| 51 | |
| 52 | void testDelegatingConstructor() { |
| 53 | DelegatingConstructor obj; |
| 54 | clang_analyzer_eval(obj.x == 42); // expected-warning{{TRUE}} |
| 55 | } |
| 56 | |
| 57 | |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 58 | struct RefWrapper { |
| 59 | RefWrapper(int *p) : x(*p) {} |
| 60 | RefWrapper(int &r) : x(r) {} |
| 61 | int &x; |
| 62 | }; |
| 63 | |
| 64 | void testReferenceMember() { |
| 65 | int *p = 0; |
Jordan Rose | de5277f | 2012-08-31 17:06:49 +0000 | [diff] [blame] | 66 | RefWrapper X(p); // expected-warning@-7 {{Dereference of null pointer}} |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void testReferenceMember2() { |
| 70 | int *p = 0; |
Anna Zaks | 018e9aa | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 71 | RefWrapper X(*p); // expected-warning {{Forming reference to null pointer}} |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 72 | } |
Jordan Rose | 3682f1e | 2012-08-25 01:06:23 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | extern "C" char *strdup(const char *); |
| 76 | |
| 77 | class StringWrapper { |
| 78 | char *str; |
| 79 | public: |
| 80 | StringWrapper(const char *input) : str(strdup(input)) {} // no-warning |
| 81 | }; |
Jordan Rose | 07c52d2 | 2013-01-26 03:16:31 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | // PR15070 - Constructing a type containing a non-POD array mistakenly |
| 85 | // tried to perform a bind instead of relying on the CXXConstructExpr, |
| 86 | // which caused a cast<> failure in RegionStore. |
| 87 | namespace DefaultConstructorWithCleanups { |
| 88 | class Element { |
| 89 | public: |
| 90 | int value; |
| 91 | |
| 92 | class Helper { |
| 93 | public: |
| 94 | ~Helper(); |
| 95 | }; |
| 96 | Element(Helper h = Helper()); |
| 97 | }; |
| 98 | class Wrapper { |
| 99 | public: |
| 100 | Element arr[2]; |
| 101 | |
| 102 | Wrapper(); |
| 103 | }; |
| 104 | |
| 105 | Wrapper::Wrapper() /* initializers synthesized */ {} |
| 106 | |
| 107 | int test() { |
| 108 | Wrapper w; |
| 109 | return w.arr[0].value; // no-warning |
| 110 | } |
| 111 | } |
Jordan Rose | bccda13 | 2013-07-17 17:16:42 +0000 | [diff] [blame] | 112 | |
| 113 | namespace DefaultMemberInitializers { |
| 114 | struct Wrapper { |
| 115 | int value = 42; |
| 116 | |
| 117 | Wrapper() {} |
| 118 | Wrapper(int x) : value(x) {} |
| 119 | Wrapper(bool) {} |
| 120 | }; |
| 121 | |
| 122 | void test() { |
| 123 | Wrapper w1; |
| 124 | clang_analyzer_eval(w1.value == 42); // expected-warning{{TRUE}} |
| 125 | |
| 126 | Wrapper w2(50); |
| 127 | clang_analyzer_eval(w2.value == 50); // expected-warning{{TRUE}} |
| 128 | |
| 129 | Wrapper w3(false); |
| 130 | clang_analyzer_eval(w3.value == 42); // expected-warning{{TRUE}} |
| 131 | } |
| 132 | |
| 133 | struct StringWrapper { |
| 134 | const char s[4] = "abc"; |
| 135 | const char *p = "xyz"; |
| 136 | |
| 137 | StringWrapper(bool) {} |
| 138 | }; |
| 139 | |
| 140 | void testString() { |
| 141 | StringWrapper w(true); |
| 142 | clang_analyzer_eval(w.s[1] == 'b'); // expected-warning{{TRUE}} |
| 143 | clang_analyzer_eval(w.p[1] == 'y'); // expected-warning{{TRUE}} |
| 144 | } |
| 145 | } |