]> granicus.if.org Git - clang/commitdiff
[Static Analyzer] Fixed a false positive case in DynamicTypeChecker when dealing...
authorGabor Horvath <xazax.hun@gmail.com>
Fri, 18 Sep 2015 23:38:57 +0000 (23:38 +0000)
committerGabor Horvath <xazax.hun@gmail.com>
Fri, 18 Sep 2015 23:38:57 +0000 (23:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248065 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/DynamicTypeChecker.cpp
test/Analysis/dynamic_type_check.m

index af4187005c32be1aeeb35889cb6acb03c73a75c3..7e0cb8e933951751eba03f1b6c5cc575610b19d8 100644 (file)
@@ -151,6 +151,14 @@ PathDiagnosticPiece *DynamicTypeChecker::DynamicTypeBugVisitor::VisitNode(
   return new PathDiagnosticEventPiece(Pos, OS.str(), true, nullptr);
 }
 
+static bool hasDefinition(const ObjCObjectPointerType *ObjPtr) {
+  const ObjCInterfaceDecl *Decl = ObjPtr->getInterfaceDecl();
+  if (!Decl)
+    return false;
+
+  return Decl->getDefinition();
+}
+
 // TODO: consider checking explicit casts?
 void DynamicTypeChecker::checkPostStmt(const ImplicitCastExpr *CE,
                                        CheckerContext &C) const {
@@ -177,6 +185,9 @@ void DynamicTypeChecker::checkPostStmt(const ImplicitCastExpr *CE,
   if (!DynObjCType || !StaticObjCType)
     return;
 
+  if (!hasDefinition(DynObjCType) || !hasDefinition(StaticObjCType))
+    return;
+
   ASTContext &ASTCtxt = C.getASTContext();
 
   // Strip kindeofness to correctly detect subtyping relationships.
index 029bbed38ff5aec1720f0490d65cbcd9289574c0..f9b181e308a9c7791d72c02caa0496b28429cc69 100644 (file)
@@ -26,8 +26,18 @@ __attribute__((objc_root_class))
 @interface NSNumber : NSObject <NSCopying>
 @end
 
+@class MyType;
+
 void testTypeCheck(NSString* str) {
   id obj = str;
   NSNumber *num = obj; // expected-warning {{}}
   (void)num;
 }
+
+void testForwardDeclarations(NSString* str) {
+  id obj = str;
+  // Do not warn, since no information is available wether MyType is a sub or
+  // super class of any other type.
+  MyType *num = obj; // no warning
+  (void)num;
+}