2022-04-08 06:17:14 -05:00
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple amdgcn-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
|
|
|
// RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s -check-prefix=X86-CHECK
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple amdgcn-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
2025-03-03 10:44:05 +01:00
|
|
|
// RUN: -analyzer-config suppress-dereferences-from-any-address-space=false\
|
2022-04-08 06:17:14 -05:00
|
|
|
// RUN: -analyzer-output=text -verify -DX86 -DNOT_SUPPRESSED %s 2>&1 | FileCheck %s -check-prefix=X86-CHECK
|
2022-02-08 18:22:32 -06:00
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple amdgcn-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
2025-03-03 10:44:05 +01:00
|
|
|
// RUN: -analyzer-config suppress-dereferences-from-any-address-space=true\
|
2022-04-08 06:17:14 -05:00
|
|
|
// RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s -check-prefix=X86-CHECK
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
|
|
|
// RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s --check-prefix=X86-CHECK
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
2025-03-03 10:44:05 +01:00
|
|
|
// RUN: -analyzer-config suppress-dereferences-from-any-address-space=true\
|
2022-04-08 06:17:14 -05:00
|
|
|
// RUN: -analyzer-output=text -verify -DX86 -DSUPPRESSED %s 2>&1 | FileCheck %s --check-prefix=X86-CHECK-SUPPRESSED
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
2025-03-03 10:44:05 +01:00
|
|
|
// RUN: -analyzer-config suppress-dereferences-from-any-address-space=false\
|
2022-04-08 06:17:14 -05:00
|
|
|
// RUN: -analyzer-output=text -verify -DX86 -DNOT_SUPPRESSED %s 2>&1 | FileCheck %s --check-prefix=X86-CHECK
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple mips-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
|
|
|
// RUN: -analyzer-output=text -verify -DMIPS %s 2>&1
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple mips-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
2025-03-03 10:44:05 +01:00
|
|
|
// RUN: -analyzer-config suppress-dereferences-from-any-address-space=false\
|
2022-04-08 06:17:14 -05:00
|
|
|
// RUN: -analyzer-output=text -verify -DMIPS %s 2>&1
|
|
|
|
//
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++14 -triple mips-unknown-unknown \
|
|
|
|
// RUN: -analyzer-checker=core,apiModeling.llvm.CastValue,debug.ExprInspection\
|
2025-03-03 10:44:05 +01:00
|
|
|
// RUN: -analyzer-config suppress-dereferences-from-any-address-space=true\
|
2022-04-08 06:17:14 -05:00
|
|
|
// RUN: -analyzer-output=text -verify -DMIPS_SUPPRESSED %s
|
2019-08-22 00:20:36 +00:00
|
|
|
|
|
|
|
#include "Inputs/llvm.h"
|
|
|
|
|
2022-02-08 18:22:32 -06:00
|
|
|
// The amggcn triple case uses an intentionally different address space.
|
|
|
|
// The core.NullDereference checker intentionally ignores checks
|
|
|
|
// that use address spaces, so the case is differentiated here.
|
|
|
|
//
|
|
|
|
// From https://llvm.org/docs/AMDGPUUsage.html#address-spaces,
|
|
|
|
// select address space 3 (local), since the pointer size is
|
|
|
|
// different than Generic.
|
|
|
|
#define DEVICE __attribute__((address_space(3)))
|
|
|
|
|
2019-08-22 00:20:36 +00:00
|
|
|
namespace clang {
|
|
|
|
struct Shape {
|
|
|
|
template <typename T>
|
|
|
|
const T *castAs() const;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const T *getAs() const;
|
|
|
|
};
|
|
|
|
class Triangle : public Shape {};
|
2020-08-27 08:06:10 -07:00
|
|
|
class Rectangle : public Shape {};
|
|
|
|
class Hexagon : public Shape {};
|
2019-08-22 00:20:36 +00:00
|
|
|
class Circle : public Shape {};
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2022-02-08 18:22:32 -06:00
|
|
|
void clang_analyzer_printState();
|
|
|
|
|
2022-04-08 06:17:14 -05:00
|
|
|
#if defined(X86)
|
2019-08-22 00:20:36 +00:00
|
|
|
void evalReferences(const Shape &S) {
|
|
|
|
const auto &C = dyn_cast<Circle>(S);
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{Dereference of null pointer}}
|
|
|
|
// expected-warning@-3 {{Dereference of null pointer}}
|
2022-02-08 18:22:32 -06:00
|
|
|
clang_analyzer_printState();
|
2022-04-08 06:17:14 -05:00
|
|
|
// XX86-CHECK: "dynamic_types": [
|
|
|
|
// XX86-CHECK-NEXT: { "region": "SymRegion{reg_$0<const struct clang::Shape & S>}", "dyn_type": "const class clang::Circle &", "sub_classable": true }
|
2022-02-08 18:22:32 -06:00
|
|
|
(void)C;
|
|
|
|
}
|
2022-04-08 06:17:14 -05:00
|
|
|
#if defined(SUPPRESSED)
|
|
|
|
void evalReferences_addrspace(const Shape &S) {
|
2022-02-08 18:22:32 -06:00
|
|
|
const auto &C = dyn_cast<DEVICE Circle>(S);
|
|
|
|
clang_analyzer_printState();
|
2022-04-08 06:17:14 -05:00
|
|
|
// X86-CHECK-SUPPRESSED: "dynamic_types": [
|
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written
without a keyword and nested name qualifier, which goes against the intent that
we should produce an AST which retains enough details to recover how things are
written.
The lack of this sugar is incompatible with the intent of the type printer
default policy, which is to print types as written, but to fall back and print
them fully qualified when they are desugared.
An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still
requires pointer alignment due to pre-existing bug in the TypeLoc buffer
handling.
---
Troubleshooting list to deal with any breakage seen with this patch:
1) The most likely effect one would see by this patch is a change in how
a type is printed. The type printer will, by design and default,
print types as written. There are customization options there, but
not that many, and they mainly apply to how to print a type that we
somehow failed to track how it was written. This patch fixes a
problem where we failed to distinguish between a type
that was written without any elaborated-type qualifiers,
such as a 'struct'/'class' tags and name spacifiers such as 'std::',
and one that has been stripped of any 'metadata' that identifies such,
the so called canonical types.
Example:
```
namespace foo {
struct A {};
A a;
};
```
If one were to print the type of `foo::a`, prior to this patch, this
would result in `foo::A`. This is how the type printer would have,
by default, printed the canonical type of A as well.
As soon as you add any name qualifiers to A, the type printer would
suddenly start accurately printing the type as written. This patch
will make it print it accurately even when written without
qualifiers, so we will just print `A` for the initial example, as
the user did not really write that `foo::` namespace qualifier.
2) This patch could expose a bug in some AST matcher. Matching types
is harder to get right when there is sugar involved. For example,
if you want to match a type against being a pointer to some type A,
then you have to account for getting a type that is sugar for a
pointer to A, or being a pointer to sugar to A, or both! Usually
you would get the second part wrong, and this would work for a
very simple test where you don't use any name qualifiers, but
you would discover is broken when you do. The usual fix is to
either use the matcher which strips sugar, which is annoying
to use as for example if you match an N level pointer, you have
to put N+1 such matchers in there, beginning to end and between
all those levels. But in a lot of cases, if the property you want
to match is present in the canonical type, it's easier and faster
to just match on that... This goes with what is said in 1), if
you want to match against the name of a type, and you want
the name string to be something stable, perhaps matching on
the name of the canonical type is the better choice.
3) This patch could expose a bug in how you get the source range of some
TypeLoc. For some reason, a lot of code is using getLocalSourceRange(),
which only looks at the given TypeLoc node. This patch introduces a new,
and more common TypeLoc node which contains no source locations on itself.
This is not an inovation here, and some other, more rare TypeLoc nodes could
also have this property, but if you use getLocalSourceRange on them, it's not
going to return any valid locations, because it doesn't have any. The right fix
here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive
into the inner TypeLoc to get the source range if it doesn't find it on the
top level one. You can use getLocalSourceRange if you are really into
micro-optimizations and you have some outside knowledge that the TypeLocs you are
dealing with will always include some source location.
4) Exposed a bug somewhere in the use of the normal clang type class API, where you
have some type, you want to see if that type is some particular kind, you try a
`dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an
ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match.
Again, like 2), this would usually have been tested poorly with some simple tests with
no qualifications, and would have been broken had there been any other kind of type sugar,
be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType.
The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper
into the type. Or use `getAsAdjusted` when dealing with TypeLocs.
For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast.
5) It could be a bug in this patch perhaps.
Let me know if you need any help!
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Differential Revision: https://reviews.llvm.org/D112374
2021-10-11 18:15:36 +02:00
|
|
|
// X86-CHECK-SUPPRESSED-NEXT: { "region": "SymRegion{reg_$0<const Shape & S>}", "dyn_type": "const __attribute__((address_space(3))) class clang::Circle &", "sub_classable": true }
|
2022-02-08 18:22:32 -06:00
|
|
|
(void)C;
|
2019-08-22 00:20:36 +00:00
|
|
|
}
|
2022-04-08 06:17:14 -05:00
|
|
|
#endif
|
|
|
|
#if defined(NOT_SUPPRESSED)
|
|
|
|
void evalReferences_addrspace(const Shape &S) {
|
|
|
|
const auto &C = dyn_cast<DEVICE Circle>(S);
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is not a 'const __attribute__((address_space(3))) class clang::Circle &'}}
|
2022-04-08 06:17:14 -05:00
|
|
|
// expected-note@-2 {{Dereference of null pointer}}
|
|
|
|
// expected-warning@-3 {{Dereference of null pointer}}
|
|
|
|
clang_analyzer_printState();
|
|
|
|
// X86-CHECK: "dynamic_types": [
|
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written
without a keyword and nested name qualifier, which goes against the intent that
we should produce an AST which retains enough details to recover how things are
written.
The lack of this sugar is incompatible with the intent of the type printer
default policy, which is to print types as written, but to fall back and print
them fully qualified when they are desugared.
An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still
requires pointer alignment due to pre-existing bug in the TypeLoc buffer
handling.
---
Troubleshooting list to deal with any breakage seen with this patch:
1) The most likely effect one would see by this patch is a change in how
a type is printed. The type printer will, by design and default,
print types as written. There are customization options there, but
not that many, and they mainly apply to how to print a type that we
somehow failed to track how it was written. This patch fixes a
problem where we failed to distinguish between a type
that was written without any elaborated-type qualifiers,
such as a 'struct'/'class' tags and name spacifiers such as 'std::',
and one that has been stripped of any 'metadata' that identifies such,
the so called canonical types.
Example:
```
namespace foo {
struct A {};
A a;
};
```
If one were to print the type of `foo::a`, prior to this patch, this
would result in `foo::A`. This is how the type printer would have,
by default, printed the canonical type of A as well.
As soon as you add any name qualifiers to A, the type printer would
suddenly start accurately printing the type as written. This patch
will make it print it accurately even when written without
qualifiers, so we will just print `A` for the initial example, as
the user did not really write that `foo::` namespace qualifier.
2) This patch could expose a bug in some AST matcher. Matching types
is harder to get right when there is sugar involved. For example,
if you want to match a type against being a pointer to some type A,
then you have to account for getting a type that is sugar for a
pointer to A, or being a pointer to sugar to A, or both! Usually
you would get the second part wrong, and this would work for a
very simple test where you don't use any name qualifiers, but
you would discover is broken when you do. The usual fix is to
either use the matcher which strips sugar, which is annoying
to use as for example if you match an N level pointer, you have
to put N+1 such matchers in there, beginning to end and between
all those levels. But in a lot of cases, if the property you want
to match is present in the canonical type, it's easier and faster
to just match on that... This goes with what is said in 1), if
you want to match against the name of a type, and you want
the name string to be something stable, perhaps matching on
the name of the canonical type is the better choice.
3) This patch could expose a bug in how you get the source range of some
TypeLoc. For some reason, a lot of code is using getLocalSourceRange(),
which only looks at the given TypeLoc node. This patch introduces a new,
and more common TypeLoc node which contains no source locations on itself.
This is not an inovation here, and some other, more rare TypeLoc nodes could
also have this property, but if you use getLocalSourceRange on them, it's not
going to return any valid locations, because it doesn't have any. The right fix
here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive
into the inner TypeLoc to get the source range if it doesn't find it on the
top level one. You can use getLocalSourceRange if you are really into
micro-optimizations and you have some outside knowledge that the TypeLocs you are
dealing with will always include some source location.
4) Exposed a bug somewhere in the use of the normal clang type class API, where you
have some type, you want to see if that type is some particular kind, you try a
`dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an
ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match.
Again, like 2), this would usually have been tested poorly with some simple tests with
no qualifications, and would have been broken had there been any other kind of type sugar,
be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType.
The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper
into the type. Or use `getAsAdjusted` when dealing with TypeLocs.
For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast.
5) It could be a bug in this patch perhaps.
Let me know if you need any help!
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Differential Revision: https://reviews.llvm.org/D112374
2021-10-11 18:15:36 +02:00
|
|
|
// X86-CHECK-NEXT: { "region": "SymRegion{reg_$0<const Shape & S>}", "dyn_type": "const __attribute__((address_space(3))) class clang::Circle &", "sub_classable": true }
|
2022-04-08 06:17:14 -05:00
|
|
|
(void)C;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#elif defined(MIPS)
|
|
|
|
void evalReferences(const Shape &S) {
|
|
|
|
const auto &C = dyn_cast<Circle>(S);
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}}
|
2022-04-08 06:17:14 -05:00
|
|
|
// expected-note@-2 {{Dereference of null pointer}}
|
|
|
|
// expected-warning@-3 {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
#if defined(MIPS_SUPPRESSED)
|
|
|
|
void evalReferences_addrspace(const Shape &S) {
|
|
|
|
const auto &C = dyn_cast<DEVICE Circle>(S);
|
|
|
|
(void)C;
|
|
|
|
}
|
|
|
|
#endif
|
2022-02-08 18:22:32 -06:00
|
|
|
#endif
|
2019-08-22 00:20:36 +00:00
|
|
|
|
|
|
|
void evalNonNullParamNonNullReturnReference(const Shape &S) {
|
|
|
|
const auto *C = dyn_cast_or_null<Circle>(S);
|
2019-08-23 03:23:58 +00:00
|
|
|
// expected-note@-1 {{'C' initialized here}}
|
2019-08-22 00:20:36 +00:00
|
|
|
|
|
|
|
if (!dyn_cast_or_null<Circle>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dyn_cast_or_null<Triangle>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 08:06:10 -07:00
|
|
|
if (dyn_cast_or_null<Rectangle>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}}
|
2020-08-27 08:06:10 -07:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dyn_cast_or_null<Hexagon>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}}
|
2020-08-27 08:06:10 -07:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-22 02:57:59 +00:00
|
|
|
if (isa<Triangle>(C)) {
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-1 {{'C' is not a 'Triangle'}}
|
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 08:06:10 -07:00
|
|
|
if (isa<Triangle, Rectangle>(C)) {
|
|
|
|
// expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle'}}
|
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<Triangle, Rectangle, Hexagon>(C)) {
|
|
|
|
// expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle' nor a 'Hexagon'}}
|
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<Circle, Rectangle, Hexagon>(C)) {
|
2019-08-22 02:57:59 +00:00
|
|
|
// expected-note@-1 {{'C' is a 'Circle'}}
|
|
|
|
// expected-note@-2 {{Taking true branch}}
|
|
|
|
|
|
|
|
(void)(1 / !C);
|
|
|
|
// expected-note@-1 {{'C' is non-null}}
|
|
|
|
// expected-note@-2 {{Division by zero}}
|
|
|
|
// expected-warning@-3 {{Division by zero}}
|
|
|
|
}
|
2019-08-22 00:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void evalNonNullParamNonNullReturn(const Shape *S) {
|
|
|
|
const auto *C = cast<Circle>(S);
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{'S' is a 'const class clang::Circle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{'C' initialized here}}
|
|
|
|
|
2020-08-27 08:06:10 -07:00
|
|
|
if (!dyn_cast_or_null<Circle>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}}
|
2020-08-27 08:06:10 -07:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dyn_cast_or_null<Triangle>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}}
|
2020-08-27 08:06:10 -07:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dyn_cast_or_null<Rectangle>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}}
|
2020-08-27 08:06:10 -07:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dyn_cast_or_null<Hexagon>(C)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}}
|
2020-08-27 08:06:10 -07:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<Triangle>(C)) {
|
|
|
|
// expected-note@-1 {{'C' is not a 'Triangle'}}
|
2019-08-22 02:57:59 +00:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 08:06:10 -07:00
|
|
|
if (isa<Triangle, Rectangle>(C)) {
|
|
|
|
// expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 08:06:10 -07:00
|
|
|
if (isa<Triangle, Rectangle, Hexagon>(C)) {
|
|
|
|
// expected-note@-1 {{'C' is neither a 'Triangle' nor a 'Rectangle' nor a 'Hexagon'}}
|
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<Circle, Rectangle, Hexagon>(C)) {
|
|
|
|
// expected-note@-1 {{'C' is a 'Circle'}}
|
|
|
|
// expected-note@-2 {{Taking true branch}}
|
|
|
|
|
|
|
|
(void)(1 / !C);
|
|
|
|
// expected-note@-1 {{'C' is non-null}}
|
|
|
|
// expected-note@-2 {{Division by zero}}
|
|
|
|
// expected-warning@-3 {{Division by zero}}
|
|
|
|
}
|
2019-08-22 00:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void evalNonNullParamNullReturn(const Shape *S) {
|
|
|
|
const auto *C = dyn_cast_or_null<Circle>(S);
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
|
|
|
|
if (const auto *T = dyn_cast_or_null<Triangle>(S)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{'T' initialized here}}
|
|
|
|
// expected-note@-3 {{'T' is non-null}}
|
|
|
|
// expected-note@-4 {{Taking true branch}}
|
|
|
|
|
|
|
|
(void)(1 / !T);
|
|
|
|
// expected-note@-1 {{'T' is non-null}}
|
|
|
|
// expected-note@-2 {{Division by zero}}
|
|
|
|
// expected-warning@-3 {{Division by zero}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void evalNullParamNullReturn(const Shape *S) {
|
|
|
|
const auto *C = dyn_cast_or_null<Circle>(S);
|
|
|
|
// expected-note@-1 {{Assuming null pointer is passed into cast}}
|
|
|
|
// expected-note@-2 {{'C' initialized to a null pointer value}}
|
|
|
|
|
|
|
|
(void)(1 / (bool)C);
|
|
|
|
// expected-note@-1 {{Division by zero}}
|
|
|
|
// expected-warning@-2 {{Division by zero}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void evalZeroParamNonNullReturnPointer(const Shape *S) {
|
|
|
|
const auto *C = S->castAs<Circle>();
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{'S' is a 'const class clang::Circle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{'C' initialized here}}
|
|
|
|
|
|
|
|
(void)(1 / !C);
|
|
|
|
// expected-note@-1 {{'C' is non-null}}
|
|
|
|
// expected-note@-2 {{Division by zero}}
|
|
|
|
// expected-warning@-3 {{Division by zero}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void evalZeroParamNonNullReturn(const Shape &S) {
|
|
|
|
const auto *C = S.castAs<Circle>();
|
2019-08-23 03:24:04 +00:00
|
|
|
// expected-note@-1 {{'C' initialized here}}
|
2019-08-22 00:20:36 +00:00
|
|
|
|
|
|
|
(void)(1 / !C);
|
|
|
|
// expected-note@-1 {{'C' is non-null}}
|
|
|
|
// expected-note@-2 {{Division by zero}}
|
|
|
|
// expected-warning@-3 {{Division by zero}}
|
|
|
|
}
|
|
|
|
|
2019-08-23 03:23:58 +00:00
|
|
|
void evalZeroParamNullReturn(const Shape *S) {
|
|
|
|
const auto &C = S->getAs<Circle>();
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}}
|
2019-08-23 03:23:58 +00:00
|
|
|
// expected-note@-2 {{Storing null pointer value}}
|
|
|
|
// expected-note@-3 {{'C' initialized here}}
|
2019-08-22 00:20:36 +00:00
|
|
|
|
|
|
|
if (!dyn_cast_or_null<Triangle>(S)) {
|
2022-06-06 08:23:08 -04:00
|
|
|
// expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}}
|
2019-08-22 00:20:36 +00:00
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dyn_cast_or_null<Triangle>(S)) {
|
|
|
|
// expected-note@-1 {{'S' is a 'Triangle'}}
|
|
|
|
// expected-note@-2 {{Taking false branch}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)(1 / (bool)C);
|
|
|
|
// expected-note@-1 {{Division by zero}}
|
|
|
|
// expected-warning@-2 {{Division by zero}}
|
|
|
|
}
|
2022-06-06 08:23:08 -04:00
|
|
|
|
|
|
|
// don't crash
|
|
|
|
// CastValueChecker was using QualType()->getPointeeCXXRecordDecl(), in
|
|
|
|
// getNoteTag which evaluated to nullptr, then crashed when attempting to
|
|
|
|
// deref an invocation to getNameAsString(). The fix is to use
|
|
|
|
// QualType().getAsString().
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// std::string CastToName =
|
|
|
|
// CastInfo ? CastInfo->to()->getAsCXXRecordDecl()->getNameAsString()
|
|
|
|
// : CastToTy->getPointeeCXXRecordDecl()->getNameAsString();
|
|
|
|
// Changed to:
|
|
|
|
// std::string CastToName =
|
|
|
|
// CastInfo ? CastInfo->to()->getAsCXXRecordDecl()->getNameAsString()
|
|
|
|
// : CastToTy.getAsString();
|
|
|
|
namespace llvm {
|
|
|
|
template <typename, typename a> void isa(a &);
|
|
|
|
template <typename> class PointerUnion {
|
|
|
|
public:
|
|
|
|
template <typename T> T *getAs() {
|
|
|
|
(void)isa<int>(*this);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class LLVMContext {
|
|
|
|
PointerUnion<LLVMContext> c;
|
|
|
|
void d() { c.getAs<int>(); }
|
|
|
|
};
|
|
|
|
} // namespace llvm
|