diff options
author | Anna Zaks <ganna@apple.com> | 2012-04-11 22:20:05 +0000 |
---|---|---|
committer | Anna Zaks <ganna@apple.com> | 2012-04-11 22:20:05 +0000 |
commit | 06868aa7e7231a755f1a5078af6bd2703de665bb (patch) | |
tree | b0b3efe6e474c63fe299679e2e34d197d1bb74fd /test/Analysis/dynamic-cast.cpp | |
parent | acee1c9dbe180c6493d1bcda6a0a7ee4018ed7b1 (diff) |
[analyzer] Better test cases for explaining where tracking types of
symbolic regions would help.
Thanks to Richard Smith.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154541 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Analysis/dynamic-cast.cpp')
-rw-r--r-- | test/Analysis/dynamic-cast.cpp | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/test/Analysis/dynamic-cast.cpp b/test/Analysis/dynamic-cast.cpp index 8d1fde8cc5..62481e3635 100644 --- a/test/Analysis/dynamic-cast.cpp +++ b/test/Analysis/dynamic-cast.cpp @@ -181,13 +181,43 @@ int testReferenceFailedCast() { return *x; // no warning (An exception is thrown by the cast.) } -// False negatives. +// Here we allow any outcome of the cast and this is good because there is a +// situation where this will fail. So if the user has written the code in this +// way, we assume they expect the cast to succeed. +// Note, this might need special handling if we track types of symbolic casts +// and use them for dynamic_cast handling. +int testDynCastMostLikelyWillFail(C *c) { + B *b = 0; + b = dynamic_cast<B*>(c); + const int* res = 0; + static const int i = 5; + if (b) { + res = &i; + } else { + res = 0; + } + return *res; // expected-warning{{Dereference of null pointer}} +} -// Symbolic regions are not typed, so we cannot deduce that the cast will -// always fail in this case. -int testDynCastFail1(class C *c) { +class M : public B, public C {}; +void callTestDynCastMostLikelyWillFail() { + M m; + testDynCastMostLikelyWillFail(&m); +} + +// False positives/negatives. + +// Due to symbolic regions not being typed. +int testDynCastFalsePositive(BB *c) { B *b = 0; b = dynamic_cast<B*>(c); - return b->m; + const int* res = 0; + static const int i = 5; + if (b) { + res = &i; + } else { + res = 0; + } + return *res; // expected-warning{{Dereference of null pointer}} } |