[cfe-commits] r53482 - in /cfe/trunk: Driver/AnalysisConsumer.cpp Driver/AnalysisConsumer.h Driver/clang.cpp include/clang/Analysis/LocalCheckers.h lib/Analysis/CheckObjCInstMethSignature.cpp test/Analysis/ObjCRetSigs.m

Ted Kremenek kremenek at apple.com
Fri Jul 11 15:40:47 PDT 2008


Author: kremenek
Date: Fri Jul 11 17:40:47 2008
New Revision: 53482

URL: http://llvm.org/viewvc/llvm-project?rev=53482&view=rev
Log:
Add new check: -check-objc-methodsigs. This check scans methods in
ObjCImplementationDecls and sees if a ancestor class defines a method with the
same selector but with a different type signature. Right now it just compares
return types, and mainly looks at differences in primitive values. The checking
will be expanded in the future.

Added:
    cfe/trunk/lib/Analysis/CheckObjCInstMethSignature.cpp
    cfe/trunk/test/Analysis/ObjCRetSigs.m
Modified:
    cfe/trunk/Driver/AnalysisConsumer.cpp
    cfe/trunk/Driver/AnalysisConsumer.h
    cfe/trunk/Driver/clang.cpp
    cfe/trunk/include/clang/Analysis/LocalCheckers.h

Modified: cfe/trunk/Driver/AnalysisConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/AnalysisConsumer.cpp?rev=53482&r1=53481&r2=53482&view=diff

==============================================================================
--- cfe/trunk/Driver/AnalysisConsumer.cpp (original)
+++ cfe/trunk/Driver/AnalysisConsumer.cpp Fri Jul 11 17:40:47 2008
@@ -390,6 +390,13 @@
                    mgr.getLangOptions(), BR);  
 }
 
+static void ActionCheckObjCInstMethSignature(AnalysisManager& mgr) {
+  BugReporter BR(mgr);
+  
+  CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
+                             BR);
+}
+
 //===----------------------------------------------------------------------===//
 // AnalysisConsumer creation.
 //===----------------------------------------------------------------------===//
@@ -416,6 +423,10 @@
       case WarnUninitVals:
         C->addCodeAction(&ActionUninitVals);
         break;
+        
+      case CheckObjCMethSigs:
+        C->addObjCImplementationAction(&ActionCheckObjCInstMethSignature);
+        break;
       
       case DisplayLiveVariables:
         C->addCodeAction(&ActionLiveness);
@@ -442,8 +453,8 @@
   
   // Checks we always perform:
   if (lopts.getGCMode() != LangOptions::GCOnly)
-    C->addObjCImplementationAction(&ActionCheckObjCDealloc);  
-  
+    C->addObjCImplementationAction(&ActionCheckObjCDealloc);
+    
   return C.take();
 }
 

Modified: cfe/trunk/Driver/AnalysisConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/AnalysisConsumer.h?rev=53482&r1=53481&r2=53482&view=diff

==============================================================================
--- cfe/trunk/Driver/AnalysisConsumer.h (original)
+++ cfe/trunk/Driver/AnalysisConsumer.h Fri Jul 11 17:40:47 2008
@@ -23,7 +23,8 @@
   WarnUninitVals,
   DisplayLiveVariables,
   CheckerCFRef,
-  CheckerSimple
+  CheckerSimple,
+  CheckObjCMethSigs
 };
   
 ASTConsumer* CreateAnalysisConsumer(Analyses* Beg, Analyses* End,

Modified: cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.cpp?rev=53482&r1=53481&r2=53482&view=diff

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Fri Jul 11 17:40:47 2008
@@ -170,6 +170,8 @@
            "Flag warnings of stores to dead variables"),
 clEnumValN(WarnUninitVals, "warn-uninit-values",
            "Flag warnings of uses of unitialized variables"),
+clEnumValN(CheckObjCMethSigs, "check-objc-methodsigs",
+      "Check the Objective-C method signatures for type incompatibilities."),
 clEnumValN(CheckerSimple, "checker-simple",
            "Perform simple path-sensitive checks."),
 clEnumValN(CheckerCFRef, "checker-cfref",

Modified: cfe/trunk/include/clang/Analysis/LocalCheckers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/LocalCheckers.h?rev=53482&r1=53481&r2=53482&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/LocalCheckers.h (original)
+++ cfe/trunk/include/clang/Analysis/LocalCheckers.h Fri Jul 11 17:40:47 2008
@@ -43,7 +43,8 @@
   
 void CheckObjCDealloc(ObjCImplementationDecl* D, const LangOptions& L,
                       BugReporter& BR);
-
+  
+void CheckObjCInstMethSignature(ObjCImplementationDecl* ID, BugReporter& BR);
   
 } // end namespace clang
 

Added: cfe/trunk/lib/Analysis/CheckObjCInstMethSignature.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CheckObjCInstMethSignature.cpp?rev=53482&view=auto

==============================================================================
--- cfe/trunk/lib/Analysis/CheckObjCInstMethSignature.cpp (added)
+++ cfe/trunk/lib/Analysis/CheckObjCInstMethSignature.cpp Fri Jul 11 17:40:47 2008
@@ -0,0 +1,125 @@
+//=- CheckObjCInstMethodRetTy.cpp - Check ObjC method signatures -*- C++ -*-==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines a CheckObjCInstMethSignature, a flow-insenstive check
+//  that determines if an Objective-C class interface incorrectly redefines
+//  the method signature in a subclass.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/LocalCheckers.h"
+#include "clang/Analysis/PathDiagnostic.h"
+#include "clang/Analysis/PathSensitive/BugReporter.h"
+#include "clang/AST/DeclObjC.h"
+#include "clang/AST/Type.h"
+#include "clang/AST/ASTContext.h"
+
+#include "llvm/ADT/DenseMap.h"
+#include <sstream>
+
+using namespace clang;
+
+static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
+                               ASTContext& C) {
+
+  // Right now don't compare the compatibility of pointers.  That involves
+  // looking at subtyping relationships.  FIXME: Future patch.
+  if ((Derived->isPointerType() || Derived->isObjCQualifiedIdType())  && 
+      (Ancestor->isPointerType() || Ancestor->isObjCQualifiedIdType()))
+    return true;
+
+  return C.typesAreCompatible(Derived, Ancestor);
+}
+
+static void CompareReturnTypes(ObjCMethodDecl* MethDerived,
+                               ObjCMethodDecl* MethAncestor,
+                               BugReporter& BR, ASTContext& Ctx,
+                               ObjCImplementationDecl* ID) {
+    
+  QualType ResDerived  = MethDerived->getResultType();
+  QualType ResAncestor = MethAncestor->getResultType(); 
+  
+  if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
+    std::ostringstream os;
+    
+    os << "The Objective-C class '"
+       << MethDerived->getClassInterface()->getName()
+       << "', which is derived from class '"
+       << MethAncestor->getClassInterface()->getName()
+       << "', defines the instance method '"
+       << MethDerived->getSelector().getName()
+       << "' whose return type is '"
+       << ResDerived.getAsString()
+       << "'.  The same method (same selector) is defined in class 'B' and has "
+          "a return type of '"
+       << ResAncestor.getAsString()
+       << "'.  These two types are incompatible, and may result in undefined "
+          "behavior for clients of these classes.";
+    
+    // Refactor.
+    SimpleBugType BT("incompatible instance method return type");
+    DiagCollector C(BT);
+    Diagnostic& Diag = BR.getDiagnostic();    
+    Diag.Report(&C, Ctx.getFullLoc(MethDerived->getLocStart()),
+                Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
+                NULL, 0, NULL, 0);
+    
+    for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
+      BR.EmitWarning(*I);
+  }
+}
+
+void clang::CheckObjCInstMethSignature(ObjCImplementationDecl* ID,
+                                       BugReporter& BR) {
+  
+  ObjCInterfaceDecl* D = ID->getClassInterface();
+  ObjCInterfaceDecl* C = D->getSuperClass();
+
+  if (!C)
+    return;
+  
+  // Build a DenseMap of the methods for quick querying.
+  typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
+  MapTy IMeths;
+  unsigned NumMethods = 0;
+  
+  for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
+       E=ID->instmeth_end(); I!=E; ++I) {    
+    
+    ObjCMethodDecl* M = *I;
+    IMeths[M->getSelector()] = M;
+    ++NumMethods;
+  }
+
+  // Now recurse the class hierarchy chain looking for methods with the
+  // same signatures.
+  ASTContext& Ctx = BR.getContext();
+  
+  while (C && NumMethods) {
+    for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
+         E=C->instmeth_end(); I!=E; ++I) {
+
+      ObjCMethodDecl* M = *I;
+      Selector S = M->getSelector();
+      
+      MapTy::iterator MI = IMeths.find(S);
+
+      if (MI == IMeths.end() || MI->second == 0)
+        continue;
+      
+      --NumMethods;
+      ObjCMethodDecl* MethDerived = MI->second;
+      MI->second = 0;
+      
+      CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
+    }
+    
+    C = C->getSuperClass();
+  }
+}

Added: cfe/trunk/test/Analysis/ObjCRetSigs.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/ObjCRetSigs.m?rev=53482&view=auto

==============================================================================
--- cfe/trunk/test/Analysis/ObjCRetSigs.m (added)
+++ cfe/trunk/test/Analysis/ObjCRetSigs.m Fri Jul 11 17:40:47 2008
@@ -0,0 +1,25 @@
+// RUN: clang -check-objc-methodsigs -verify %s
+
+#include <stdio.h>
+
+ at interface MyBase
+-(long long)length;
+ at end
+
+ at interface MySub : MyBase{}
+-(double)length;
+ at end
+
+ at implementation MyBase
+-(long long)length{
+   printf("Called MyBase -length;\n");
+   return 3;
+}
+ at end
+
+ at implementation MySub
+-(double)length{  // expected-warning{{types are incompatible}}
+   printf("Called MySub -length;\n");
+   return 3.3;
+}
+ at end





More information about the cfe-commits mailing list