1 // RUN: %clang_analyze_cc1 -std=c++14 -triple amdgcn-unknown-unknown \
2 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
3 // RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s -check-prefix=X86-CHECK
5 // RUN: %clang_analyze_cc1 -std=c++14 -triple amdgcn-unknown-unknown \
6 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
7 // RUN: -analyzer-config core.NullDereference:SuppressAddressSpaces=false\
8 // RUN: -analyzer-output=text -verify -DX86 -DNOT_SUPPRESSED %s 2>&1 | FileCheck %s -check-prefix=X86-CHECK
10 // RUN: %clang_analyze_cc1 -std=c++14 -triple amdgcn-unknown-unknown \
11 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
12 // RUN: -analyzer-config core.NullDereference:SuppressAddressSpaces=true\
13 // RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s -check-prefix=X86-CHECK
15 // RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-unknown-unknown \
16 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
17 // RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s --check-prefix=X86-CHECK
19 // RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-unknown-unknown \
20 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
21 // RUN: -analyzer-config core.NullDereference:SuppressAddressSpaces=true\
22 // RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s --check-prefix=X86-CHECK-SUPPRESSED
24 // RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-unknown-unknown \
25 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
26 // RUN: -analyzer-config core.NullDereference:SuppressAddressSpaces=false\
27 // RUN: -analyzer-output=text -verify -DX86 -DNOT_SUPPRESSED %s 2>&1 | FileCheck %s --check-prefix=X86-CHECK
29 // RUN: %clang_analyze_cc1 -std=c++14 -triple mips-unknown-unknown \
30 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
31 // RUN: -analyzer-output=text -verify -DMIPS %s 2>&1
33 // RUN: %clang_analyze_cc1 -std=c++14 -triple mips-unknown-unknown \
34 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
35 // RUN: -analyzer-config core.NullDereference:SuppressAddressSpaces=false\
36 // RUN: -analyzer-output=text -verify -DMIPS %s 2>&1
38 // RUN: %clang_analyze_cc1 -std=c++14 -triple mips-unknown-unknown \
39 // RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
40 // RUN: -analyzer-config core.NullDereference:SuppressAddressSpaces=true\
41 // RUN: -analyzer-output=text -verify -DMIPS_SUPPRESSED %s
43 #include "Inputs/llvm.h"
45 // The amggcn triple case uses an intentionally different address space.
46 // The core.NullDereference checker intentionally ignores checks
47 // that use address spaces, so the case is differentiated here.
49 // From https://llvm.org/docs/AMDGPUUsage.html#address-spaces,
50 // select address space 3 (local), since the pointer size is
51 // different than Generic.
52 #define DEVICE __attribute__((address_space(3)))
57 const T
*castAs() const;
60 const T
*getAs() const;
62 class Triangle
: public Shape
{};
63 class Rectangle
: public Shape
{};
64 class Hexagon
: public Shape
{};
65 class Circle
: public Shape
{};
69 using namespace clang
;
71 void clang_analyzer_printState();
74 void evalReferences(const Shape
&S
) {
75 const auto &C
= dyn_cast
<Circle
>(S
);
76 // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}}
77 // expected-note@-2 {{Dereference of null pointer}}
78 // expected-warning@-3 {{Dereference of null pointer}}
79 clang_analyzer_printState();
80 // XX86-CHECK: "dynamic_types": [
81 // XX86-CHECK-NEXT: { "region": "SymRegion{reg_$0<const struct clang::Shape & S>}", "dyn_type": "const class clang::Circle &", "sub_classable": true }
84 #if defined(SUPPRESSED)
85 void evalReferences_addrspace(const Shape
&S
) {
86 const auto &C
= dyn_cast
<DEVICE Circle
>(S
);
87 clang_analyzer_printState();
88 // X86-CHECK-SUPPRESSED: "dynamic_types": [
89 // X86-CHECK-SUPPRESSED-NEXT: { "region": "SymRegion{reg_$0<const Shape & S>}", "dyn_type": "const __attribute__((address_space(3))) class clang::Circle &", "sub_classable": true }
93 #if defined(NOT_SUPPRESSED)
94 void evalReferences_addrspace(const Shape
&S
) {
95 const auto &C
= dyn_cast
<DEVICE Circle
>(S
);
96 // expected-note@-1 {{Assuming 'S' is not a 'const __attribute__((address_space(3))) class clang::Circle &'}}
97 // expected-note@-2 {{Dereference of null pointer}}
98 // expected-warning@-3 {{Dereference of null pointer}}
99 clang_analyzer_printState();
100 // X86-CHECK: "dynamic_types": [
101 // X86-CHECK-NEXT: { "region": "SymRegion{reg_$0<const Shape & S>}", "dyn_type": "const __attribute__((address_space(3))) class clang::Circle &", "sub_classable": true }
106 void evalReferences(const Shape
&S
) {
107 const auto &C
= dyn_cast
<Circle
>(S
);
108 // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}}
109 // expected-note@-2 {{Dereference of null pointer}}
110 // expected-warning@-3 {{Dereference of null pointer}}
112 #if defined(MIPS_SUPPRESSED)
113 void evalReferences_addrspace(const Shape
&S
) {
114 const auto &C
= dyn_cast
<DEVICE Circle
>(S
);
120 void evalNonNullParamNonNullReturnReference(const Shape
&S
) {
121 const auto *C
= dyn_cast_or_null
<Circle
>(S
);
122 // expected-note@-1 {{'C' initialized here}}
124 if (!dyn_cast_or_null
<Circle
>(C
)) {
125 // expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}}
126 // expected-note@-2 {{Taking false branch}}
130 if (dyn_cast_or_null
<Triangle
>(C
)) {
131 // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}}
132 // expected-note@-2 {{Taking false branch}}
136 if (dyn_cast_or_null
<Rectangle
>(C
)) {
137 // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}}
138 // expected-note@-2 {{Taking false branch}}
142 if (dyn_cast_or_null
<Hexagon
>(C
)) {
143 // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}}
144 // expected-note@-2 {{Taking false branch}}
148 if (isa
<Triangle
>(C
)) {
149 // expected-note@-1 {{'C' is not a 'Triangle'}}
150 // expected-note@-2 {{Taking false branch}}
154 if (isa
<Triangle
, Rectangle
>(C
)) {
155 // expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle'}}
156 // expected-note@-2 {{Taking false branch}}
160 if (isa
<Triangle
, Rectangle
, Hexagon
>(C
)) {
161 // expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle' nor a 'Hexagon'}}
162 // expected-note@-2 {{Taking false branch}}
166 if (isa
<Circle
, Rectangle
, Hexagon
>(C
)) {
167 // expected-note@-1 {{'C' is a 'Circle'}}
168 // expected-note@-2 {{Taking true branch}}
171 // expected-note@-1 {{'C' is non-null}}
172 // expected-note@-2 {{Division by zero}}
173 // expected-warning@-3 {{Division by zero}}
177 void evalNonNullParamNonNullReturn(const Shape
*S
) {
178 const auto *C
= cast
<Circle
>(S
);
179 // expected-note@-1 {{'S' is a 'const class clang::Circle *'}}
180 // expected-note@-2 {{'C' initialized here}}
182 if (!dyn_cast_or_null
<Circle
>(C
)) {
183 // expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}}
184 // expected-note@-2 {{Taking false branch}}
188 if (dyn_cast_or_null
<Triangle
>(C
)) {
189 // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}}
190 // expected-note@-2 {{Taking false branch}}
194 if (dyn_cast_or_null
<Rectangle
>(C
)) {
195 // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}}
196 // expected-note@-2 {{Taking false branch}}
200 if (dyn_cast_or_null
<Hexagon
>(C
)) {
201 // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}}
202 // expected-note@-2 {{Taking false branch}}
206 if (isa
<Triangle
>(C
)) {
207 // expected-note@-1 {{'C' is not a 'Triangle'}}
208 // expected-note@-2 {{Taking false branch}}
212 if (isa
<Triangle
, Rectangle
>(C
)) {
213 // expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle'}}
214 // expected-note@-2 {{Taking false branch}}
218 if (isa
<Triangle
, Rectangle
, Hexagon
>(C
)) {
219 // expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle' nor a 'Hexagon'}}
220 // expected-note@-2 {{Taking false branch}}
224 if (isa
<Circle
, Rectangle
, Hexagon
>(C
)) {
225 // expected-note@-1 {{'C' is a 'Circle'}}
226 // expected-note@-2 {{Taking true branch}}
229 // expected-note@-1 {{'C' is non-null}}
230 // expected-note@-2 {{Division by zero}}
231 // expected-warning@-3 {{Division by zero}}
235 void evalNonNullParamNullReturn(const Shape
*S
) {
236 const auto *C
= dyn_cast_or_null
<Circle
>(S
);
237 // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}}
239 if (const auto *T
= dyn_cast_or_null
<Triangle
>(S
)) {
240 // expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}}
241 // expected-note@-2 {{'T' initialized here}}
242 // expected-note@-3 {{'T' is non-null}}
243 // expected-note@-4 {{Taking true branch}}
246 // expected-note@-1 {{'T' is non-null}}
247 // expected-note@-2 {{Division by zero}}
248 // expected-warning@-3 {{Division by zero}}
252 void evalNullParamNullReturn(const Shape
*S
) {
253 const auto *C
= dyn_cast_or_null
<Circle
>(S
);
254 // expected-note@-1 {{Assuming null pointer is passed into cast}}
255 // expected-note@-2 {{'C' initialized to a null pointer value}}
258 // expected-note@-1 {{Division by zero}}
259 // expected-warning@-2 {{Division by zero}}
262 void evalZeroParamNonNullReturnPointer(const Shape
*S
) {
263 const auto *C
= S
->castAs
<Circle
>();
264 // expected-note@-1 {{'S' is a 'const class clang::Circle *'}}
265 // expected-note@-2 {{'C' initialized here}}
268 // expected-note@-1 {{'C' is non-null}}
269 // expected-note@-2 {{Division by zero}}
270 // expected-warning@-3 {{Division by zero}}
273 void evalZeroParamNonNullReturn(const Shape
&S
) {
274 const auto *C
= S
.castAs
<Circle
>();
275 // expected-note@-1 {{'C' initialized here}}
278 // expected-note@-1 {{'C' is non-null}}
279 // expected-note@-2 {{Division by zero}}
280 // expected-warning@-3 {{Division by zero}}
283 void evalZeroParamNullReturn(const Shape
*S
) {
284 const auto &C
= S
->getAs
<Circle
>();
285 // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}}
286 // expected-note@-2 {{Storing null pointer value}}
287 // expected-note@-3 {{'C' initialized here}}
289 if (!dyn_cast_or_null
<Triangle
>(S
)) {
290 // expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}}
291 // expected-note@-2 {{Taking false branch}}
295 if (!dyn_cast_or_null
<Triangle
>(S
)) {
296 // expected-note@-1 {{'S' is a 'Triangle'}}
297 // expected-note@-2 {{Taking false branch}}
302 // expected-note@-1 {{Division by zero}}
303 // expected-warning@-2 {{Division by zero}}
307 // CastValueChecker was using QualType()->getPointeeCXXRecordDecl(), in
308 // getNoteTag which evaluated to nullptr, then crashed when attempting to
309 // deref an invocation to getNameAsString(). The fix is to use
310 // QualType().getAsString().
313 // std::string CastToName =
314 // CastInfo ? CastInfo->to()->getAsCXXRecordDecl()->getNameAsString()
315 // : CastToTy->getPointeeCXXRecordDecl()->getNameAsString();
317 // std::string CastToName =
318 // CastInfo ? CastInfo->to()->getAsCXXRecordDecl()->getNameAsString()
319 // : CastToTy.getAsString();
321 template <typename
, typename a
> void isa(a
&);
322 template <typename
> class PointerUnion
{
324 template <typename T
> T
*getAs() {
325 (void)isa
<int>(*this);
330 PointerUnion
<LLVMContext
> c
;
331 void d() { c
.getAs
<int>(); }