[cfe-commits] r154541 - /cfe/trunk/test/Analysis/dynamic-cast.cpp

Anna Zaks ganna at apple.com
Wed Apr 11 15:20:05 PDT 2012


Author: zaks
Date: Wed Apr 11 17:20:05 2012
New Revision: 154541

URL: http://llvm.org/viewvc/llvm-project?rev=154541&view=rev
Log:
[analyzer] Better test cases for explaining where tracking types of
symbolic regions would help.

Thanks to Richard Smith.

Modified:
    cfe/trunk/test/Analysis/dynamic-cast.cpp

Modified: cfe/trunk/test/Analysis/dynamic-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dynamic-cast.cpp?rev=154541&r1=154540&r2=154541&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/dynamic-cast.cpp (original)
+++ cfe/trunk/test/Analysis/dynamic-cast.cpp Wed Apr 11 17:20:05 2012
@@ -181,13 +181,43 @@
   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}}
+}
+
+class M : public B, public C {};
+void callTestDynCastMostLikelyWillFail() {
+  M m;
+  testDynCastMostLikelyWillFail(&m);
+}
+
+// False positives/negatives.
 
-// Symbolic regions are not typed, so we cannot deduce that the cast will
-// always fail in this case.
-int testDynCastFail1(class C *c) {
+// 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}}
 }
 





More information about the cfe-commits mailing list