@@ -41,6 +41,7 @@ A *return_null() {
41
41
42
42
void derefAfterValidCtr () {
43
43
std::unique_ptr<A> P (new A ());
44
+ clang_analyzer_numTimesReached (); // expected-warning {{1}}
44
45
P->foo (); // No warning.
45
46
}
46
47
@@ -50,17 +51,20 @@ void derefOfUnknown(std::unique_ptr<A> P) {
50
51
51
52
void derefAfterDefaultCtr () {
52
53
std::unique_ptr<A> P;
54
+ clang_analyzer_numTimesReached (); // expected-warning {{1}}
53
55
P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
54
56
}
55
57
56
58
void derefAfterCtrWithNull () {
57
59
std::unique_ptr<A> P (nullptr );
60
+ clang_analyzer_numTimesReached (); // expected-warning {{1}}
58
61
*P; // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
59
62
}
60
63
61
64
void derefAfterCtrWithNullVariable () {
62
65
A *InnerPtr = nullptr ;
63
66
std::unique_ptr<A> P (InnerPtr);
67
+ clang_analyzer_numTimesReached (); // expected-warning {{1}}
64
68
P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
65
69
}
66
70
@@ -87,6 +91,7 @@ void derefAfterResetWithNull() {
87
91
void derefAfterResetWithNonNull () {
88
92
std::unique_ptr<A> P;
89
93
P.reset (new A ());
94
+ clang_analyzer_numTimesReached (); // expected-warning {{1}}
90
95
P->foo (); // No warning.
91
96
}
92
97
@@ -116,37 +121,40 @@ void pass_smart_ptr_by_const_rvalue_ref(const std::unique_ptr<A> &&a);
116
121
void pass_smart_ptr_by_ptr (std::unique_ptr<A> *a);
117
122
void pass_smart_ptr_by_const_ptr (const std::unique_ptr<A> *a);
118
123
119
- void regioninvalidationTest () {
120
- {
121
- std::unique_ptr<A> P;
122
- pass_smart_ptr_by_ref (P);
123
- P->foo (); // no-warning
124
- }
125
- {
126
- std::unique_ptr<A> P;
127
- pass_smart_ptr_by_const_ref (P);
128
- P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
129
- }
130
- {
131
- std::unique_ptr<A> P;
132
- pass_smart_ptr_by_rvalue_ref (std::move (P));
133
- P->foo (); // no-warning
134
- }
135
- {
136
- std::unique_ptr<A> P;
137
- pass_smart_ptr_by_const_rvalue_ref (std::move (P));
138
- P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
139
- }
140
- {
141
- std::unique_ptr<A> P;
142
- pass_smart_ptr_by_ptr (&P);
143
- P->foo ();
144
- }
145
- {
146
- std::unique_ptr<A> P;
147
- pass_smart_ptr_by_const_ptr (&P);
148
- P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
149
- }
124
+ void regioninvalidationWithPassByRef () {
125
+ std::unique_ptr<A> P;
126
+ pass_smart_ptr_by_ref (P);
127
+ P->foo (); // no-warning
128
+ }
129
+
130
+ void regioninvalidationWithPassByCostRef () {
131
+ std::unique_ptr<A> P;
132
+ pass_smart_ptr_by_const_ref (P);
133
+ P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
134
+ }
135
+
136
+ void regioninvalidationWithPassByRValueRef () {
137
+ std::unique_ptr<A> P;
138
+ pass_smart_ptr_by_rvalue_ref (std::move (P));
139
+ P->foo (); // no-warning
140
+ }
141
+
142
+ void regioninvalidationWithPassByConstRValueRef () {
143
+ std::unique_ptr<A> P;
144
+ pass_smart_ptr_by_const_rvalue_ref (std::move (P));
145
+ P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
146
+ }
147
+
148
+ void regioninvalidationWithPassByPtr () {
149
+ std::unique_ptr<A> P;
150
+ pass_smart_ptr_by_ptr (&P);
151
+ P->foo ();
152
+ }
153
+
154
+ void regioninvalidationWithPassByConstPtr () {
155
+ std::unique_ptr<A> P;
156
+ pass_smart_ptr_by_const_ptr (&P);
157
+ P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
150
158
}
151
159
152
160
struct StructWithSmartPtr {
@@ -160,37 +168,40 @@ void pass_struct_with_smart_ptr_by_const_rvalue_ref(const StructWithSmartPtr &&a
160
168
void pass_struct_with_smart_ptr_by_ptr (StructWithSmartPtr *a);
161
169
void pass_struct_with_smart_ptr_by_const_ptr (const StructWithSmartPtr *a);
162
170
163
- void regioninvalidationTestWithinStruct () {
164
- {
165
- StructWithSmartPtr S;
166
- pass_struct_with_smart_ptr_by_ref (S);
167
- S.P ->foo (); // no-warning
168
- }
169
- {
170
- StructWithSmartPtr S;
171
- pass_struct_with_smart_ptr_by_const_ref (S);
172
- S.P ->foo (); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
173
- }
174
- {
175
- StructWithSmartPtr S;
176
- pass_struct_with_smart_ptr_by_rvalue_ref (std::move (S));
177
- S.P ->foo (); // no-warning
178
- }
179
- {
180
- StructWithSmartPtr S;
181
- pass_struct_with_smart_ptr_by_const_rvalue_ref (std::move (S));
182
- S.P ->foo (); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
183
- }
184
- {
185
- StructWithSmartPtr S;
186
- pass_struct_with_smart_ptr_by_ptr (&S);
187
- S.P ->foo ();
188
- }
189
- {
190
- StructWithSmartPtr S;
191
- pass_struct_with_smart_ptr_by_const_ptr (&S);
192
- S.P ->foo (); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
193
- }
171
+ void regioninvalidationWithinStructPassByRef () {
172
+ StructWithSmartPtr S;
173
+ pass_struct_with_smart_ptr_by_ref (S);
174
+ S.P ->foo (); // no-warning
175
+ }
176
+
177
+ void regioninvalidationWithinStructPassByConstRef () {
178
+ StructWithSmartPtr S;
179
+ pass_struct_with_smart_ptr_by_const_ref (S);
180
+ S.P ->foo (); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
181
+ }
182
+
183
+ void regioninvalidationWithinStructPassByRValueRef () {
184
+ StructWithSmartPtr S;
185
+ pass_struct_with_smart_ptr_by_rvalue_ref (std::move (S));
186
+ S.P ->foo (); // no-warning
187
+ }
188
+
189
+ void regioninvalidationWithinStructPassByConstRValueRef () {
190
+ StructWithSmartPtr S;
191
+ pass_struct_with_smart_ptr_by_const_rvalue_ref (std::move (S));
192
+ S.P ->foo (); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
193
+ }
194
+
195
+ void regioninvalidationWithinStructPassByPtr () {
196
+ StructWithSmartPtr S;
197
+ pass_struct_with_smart_ptr_by_ptr (&S);
198
+ S.P ->foo (); // no-warning
199
+ }
200
+
201
+ void regioninvalidationWithinStructPassByConstPtr () {
202
+ StructWithSmartPtr S;
203
+ pass_struct_with_smart_ptr_by_const_ptr (&S);
204
+ S.P ->foo (); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
194
205
}
195
206
196
207
void derefAfterAssignment () {
@@ -217,14 +228,20 @@ void derefOnSwappedNullPtr() {
217
228
(*P).foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
218
229
}
219
230
220
- void derefOnStdSwappedNullPtr () {
231
+ void derefOnFirstStdSwappedNullPtr () {
221
232
std::unique_ptr<A> P;
222
233
std::unique_ptr<A> PNull;
223
234
std::swap (P, PNull);
224
- PNull->foo (); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}}
225
235
P->foo (); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
226
236
}
227
237
238
+ void derefOnSecondStdSwappedNullPtr () {
239
+ std::unique_ptr<A> P;
240
+ std::unique_ptr<A> PNull;
241
+ std::swap (P, PNull);
242
+ PNull->foo (); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}}
243
+ }
244
+
228
245
void derefOnSwappedValidPtr () {
229
246
std::unique_ptr<A> P (new A ());
230
247
std::unique_ptr<A> PValid (new A ());
0 commit comments