diff options
Diffstat (limited to 'clang/test/Analysis/cast-value-notes.cpp')
-rw-r--r-- | clang/test/Analysis/cast-value-notes.cpp | 59 |
1 files changed, 44 insertions, 15 deletions
diff --git a/clang/test/Analysis/cast-value-notes.cpp b/clang/test/Analysis/cast-value-notes.cpp index a09586309fb4..2506fd9021ab 100644 --- a/clang/test/Analysis/cast-value-notes.cpp +++ b/clang/test/Analysis/cast-value-notes.cpp @@ -23,7 +23,7 @@ using namespace clang; void evalReferences(const Shape &S) { const auto &C = dyn_cast<Circle>(S); - // expected-note@-1 {{Assuming 'S' is not a 'Circle'}} + // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}} // expected-note@-2 {{Dereference of null pointer}} // expected-warning@-3 {{Dereference of null pointer}} } @@ -33,25 +33,25 @@ void evalNonNullParamNonNullReturnReference(const Shape &S) { // expected-note@-1 {{'C' initialized here}} if (!dyn_cast_or_null<Circle>(C)) { - // expected-note@-1 {{'C' is a 'Circle'}} + // expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null<Triangle>(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'Triangle'}} + // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null<Rectangle>(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'Rectangle'}} + // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null<Hexagon>(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'Hexagon'}} + // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}} // expected-note@-2 {{Taking false branch}} return; } @@ -87,29 +87,29 @@ void evalNonNullParamNonNullReturnReference(const Shape &S) { void evalNonNullParamNonNullReturn(const Shape *S) { const auto *C = cast<Circle>(S); - // expected-note@-1 {{'S' is a 'Circle'}} + // expected-note@-1 {{'S' is a 'const class clang::Circle *'}} // expected-note@-2 {{'C' initialized here}} if (!dyn_cast_or_null<Circle>(C)) { - // expected-note@-1 {{'C' is a 'Circle'}} + // expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null<Triangle>(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'Triangle'}} + // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null<Rectangle>(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'Rectangle'}} + // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null<Hexagon>(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'Hexagon'}} + // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}} // expected-note@-2 {{Taking false branch}} return; } @@ -145,10 +145,10 @@ void evalNonNullParamNonNullReturn(const Shape *S) { void evalNonNullParamNullReturn(const Shape *S) { const auto *C = dyn_cast_or_null<Circle>(S); - // expected-note@-1 {{Assuming 'S' is not a 'Circle'}} + // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}} if (const auto *T = dyn_cast_or_null<Triangle>(S)) { - // expected-note@-1 {{Assuming 'S' is a 'Triangle'}} + // expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}} // expected-note@-2 {{'T' initialized here}} // expected-note@-3 {{'T' is non-null}} // expected-note@-4 {{Taking true branch}} @@ -172,7 +172,7 @@ void evalNullParamNullReturn(const Shape *S) { void evalZeroParamNonNullReturnPointer(const Shape *S) { const auto *C = S->castAs<Circle>(); - // expected-note@-1 {{'S' is a 'Circle'}} + // expected-note@-1 {{'S' is a 'const class clang::Circle *'}} // expected-note@-2 {{'C' initialized here}} (void)(1 / !C); @@ -193,12 +193,12 @@ void evalZeroParamNonNullReturn(const Shape &S) { void evalZeroParamNullReturn(const Shape *S) { const auto &C = S->getAs<Circle>(); - // expected-note@-1 {{Assuming 'S' is not a 'Circle'}} + // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}} // expected-note@-2 {{Storing null pointer value}} // expected-note@-3 {{'C' initialized here}} if (!dyn_cast_or_null<Triangle>(S)) { - // expected-note@-1 {{Assuming 'S' is a 'Triangle'}} + // expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}} // expected-note@-2 {{Taking false branch}} return; } @@ -213,3 +213,32 @@ void evalZeroParamNullReturn(const Shape *S) { // expected-note@-1 {{Division by zero}} // expected-warning@-2 {{Division by zero}} } + +// 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 |