[cfe-commits] r56312 - in /cfe/trunk: Driver/Analyses.def Driver/AnalysisConsumer.cpp include/clang/Analysis/LocalCheckers.h include/clang/Analysis/PathSensitive/BugReporter.h include/clang/Analysis/PathSensitive/GRExprEngine.h lib/Analysis/BasicObjCFoundationChecks.cpp lib/Analysis/BasicObjCFoundationChecks.h lib/Analysis/CheckNSError.cpp

Ted Kremenek kremenek at apple.com
Thu Sep 18 14:25:17 PDT 2008


Author: kremenek
Date: Thu Sep 18 16:25:13 2008
New Revision: 56312

URL: http://llvm.org/viewvc/llvm-project?rev=56312&view=rev
Log:
Change implementation of NSError** coding-style check to be invoked at the end of the retain/release analysis.

Modified:
    cfe/trunk/Driver/Analyses.def
    cfe/trunk/Driver/AnalysisConsumer.cpp
    cfe/trunk/include/clang/Analysis/LocalCheckers.h
    cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h
    cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h
    cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.cpp
    cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.h
    cfe/trunk/lib/Analysis/CheckNSError.cpp

Modified: cfe/trunk/Driver/Analyses.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/Analyses.def?rev=56312&r1=56311&r2=56312&view=diff

==============================================================================
--- cfe/trunk/Driver/Analyses.def (original)
+++ cfe/trunk/Driver/Analyses.def Thu Sep 18 16:25:13 2008
@@ -34,10 +34,6 @@
  "Warn about Objective-C classes that lack a correct implementation of -dealloc",
  ObjCImplementation)
  
-ANALYSIS(WarnObjCNSError, "warn-objc-nserror-methods",
-  "Check coding rules for 'Creating and Returning NSError Objects'",
-  ObjCImplementation)
- 
 ANALYSIS(WarnObjCUnusedIvars, "warn-objc-unused-ivars",
   "Warn about private ivars that are never used", ObjCImplementation)
 

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

==============================================================================
--- cfe/trunk/Driver/AnalysisConsumer.cpp (original)
+++ cfe/trunk/Driver/AnalysisConsumer.cpp Thu Sep 18 16:25:13 2008
@@ -431,11 +431,6 @@
                              BR);
 }
 
-static void ActionWarnObjCNSError(AnalysisManager& mgr) {
-  BugReporter BR(mgr);
-  CheckNSError(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
-}
-
 //===----------------------------------------------------------------------===//
 // AnalysisConsumer creation.
 //===----------------------------------------------------------------------===//

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

==============================================================================
--- cfe/trunk/include/clang/Analysis/LocalCheckers.h (original)
+++ cfe/trunk/include/clang/Analysis/LocalCheckers.h Thu Sep 18 16:25:13 2008
@@ -46,7 +46,6 @@
   
 void CheckObjCInstMethSignature(ObjCImplementationDecl* ID, BugReporter& BR);
 void CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR);
-void CheckNSError(ObjCImplementationDecl* D, BugReporter& BR);
   
 void RegisterAppleChecks(GRExprEngine& Eng);
   

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h?rev=56312&r1=56311&r2=56312&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h Thu Sep 18 16:25:13 2008
@@ -223,12 +223,18 @@
   
   virtual ~GRBugReporter();
   
+  /// getEngine - Return the analysis engine used to analyze a given
+  ///  function or method.
   GRExprEngine& getEngine() {
     return Eng;
   }
 
+  /// getGraph - Get the exploded graph created by the analysis engine
+  ///  for the analyzed method or function.
   ExplodedGraph<GRState>& getGraph();
   
+  /// getStateManager - Return the state manager used by the analysis
+  ///  engine.
   GRStateManager& getStateManager();
   
   virtual void GeneratePathDiagnostic(PathDiagnostic& PD, BugReport& R);
@@ -240,7 +246,8 @@
   bool isNotable(SymbolID Sym) const {
     return (bool) NotableSymbols.count(Sym);
   }
-  
+    
+  /// classof - Used by isa<>, cast<>, and dyn_cast<>.
   static bool classof(const BugReporter* R) {
     return R->getKind() == GRBugReporterKind;
   }

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h?rev=56312&r1=56311&r2=56312&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h Thu Sep 18 16:25:13 2008
@@ -216,6 +216,9 @@
   const_bug_type_iterator bug_types_begin() const { return BugTypes.begin(); }
   const_bug_type_iterator bug_types_end() const { return BugTypes.end(); }
   
+  /// Register - Register a BugType with the analyzer engine.  A registered
+  ///  BugType object will have its 'EmitWarnings' method called when the
+  ///  the analyzer finishes analyzing a method or function.
   void Register(BugType* B) {
     BugTypes.push_back(B);
   }

Modified: cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.cpp?rev=56312&r1=56311&r2=56312&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.cpp (original)
+++ cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.cpp Thu Sep 18 16:25:13 2008
@@ -559,5 +559,8 @@
   Eng.AddCheck(CreateBasicObjCFoundationChecks(Ctx, VMgr),
                Stmt::ObjCMessageExprClass);
 
-  Eng.AddCheck(CreateAuditCFNumberCreate(Ctx, VMgr), Stmt::CallExprClass);
+  Eng.AddCheck(CreateAuditCFNumberCreate(Ctx, VMgr),
+               Stmt::CallExprClass);
+  
+  Eng.Register(CreateNSErrorCheck());
 }

Modified: cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.h?rev=56312&r1=56311&r2=56312&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.h (original)
+++ cfe/trunk/lib/Analysis/BasicObjCFoundationChecks.h Thu Sep 18 16:25:13 2008
@@ -29,14 +29,16 @@
 class GRSimpleAPICheck;
 class ASTContext;
 class GRStateManager;  
+class BugType;
   
 GRSimpleAPICheck* CreateBasicObjCFoundationChecks(ASTContext& Ctx,
                                                   GRStateManager* VMgr);
   
 GRSimpleAPICheck* CreateAuditCFNumberCreate(ASTContext& Ctx,
                                             GRStateManager* VMgr);
-
-
+  
+BugType* CreateNSErrorCheck();
+  
 } // end clang namespace
 
 #endif

Modified: cfe/trunk/lib/Analysis/CheckNSError.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CheckNSError.cpp?rev=56312&r1=56311&r2=56312&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CheckNSError.cpp (original)
+++ cfe/trunk/lib/Analysis/CheckNSError.cpp Thu Sep 18 16:25:13 2008
@@ -17,56 +17,90 @@
 
 #include "clang/Analysis/LocalCheckers.h"
 #include "clang/Analysis/PathSensitive/BugReporter.h"
+#include "clang/Analysis/PathSensitive/GRExprEngine.h"
+#include "BasicObjCFoundationChecks.h"
+#include "llvm/Support/Compiler.h"
 #include "clang/AST/DeclObjC.h"
-#include "clang/AST/Type.h"
-#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "llvm/ADT/SmallVector.h"
 
 using namespace clang;
 
-void clang::CheckNSError(ObjCImplementationDecl* ID, BugReporter& BR) {
-  // Look at the @interface for this class.
-  ObjCInterfaceDecl* D = ID->getClassInterface();
+namespace {
+class VISIBILITY_HIDDEN NSErrorCheck : public BugTypeCacheLocation {
+
+  void EmitGRWarnings(GRBugReporter& BR);
   
-  // Get the ASTContext.  Useful for querying type information.
-  ASTContext &Ctx = BR.getContext();
+  void CheckSignature(ObjCMethodDecl& MD, QualType& ResultTy,
+                      llvm::SmallVectorImpl<VarDecl*>& Params,
+                      IdentifierInfo* NSErrorII);
+
+  bool CheckArgument(QualType ArgTy, IdentifierInfo* NSErrorII);
   
-  // Get the IdentifierInfo* for "NSError".
-  IdentifierInfo* NSErrorII = &Ctx.Idents.get("NSError");
+public:
+  void EmitWarnings(BugReporter& BR) { EmitGRWarnings(cast<GRBugReporter>(BR));}
+  const char* getName() const { return "NSError** null dereference"; }
+};  
+  
+} // end anonymous namespace
 
-  // Scan the methods.  See if any of them have an argument of type NSError**.  
-  for (ObjCInterfaceDecl::instmeth_iterator I=D->instmeth_begin(),
-        E=D->instmeth_end(); I!=E; ++I) {
+BugType* clang::CreateNSErrorCheck() {
+  return new NSErrorCheck();
+}
 
-    // Get the method declaration.
-    ObjCMethodDecl* M = *I;
+void NSErrorCheck::EmitGRWarnings(GRBugReporter& BR) {
+  // Get the analysis engine and the exploded analysis graph.
+  GRExprEngine& Eng = BR.getEngine();
+  GRExprEngine::GraphTy& G = Eng.getGraph();
+  
+  // Get the declaration of the method/function that was analyzed.
+  Decl& CodeDecl = G.getCodeDecl();
+  
+  ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl);
+  if (!MD)
+    return;
+  
+  // Get the ASTContext, which is useful for querying type information.
+  ASTContext &Ctx = BR.getContext();
+
+  QualType ResultTy;
+  llvm::SmallVector<VarDecl*, 5> Params;  
+  CheckSignature(*MD, ResultTy, Params, &Ctx.Idents.get("NSError"));
+  
+  if (Params.empty())
+    return;
+  
+  if (ResultTy == Ctx.VoidTy) {
+    BR.EmitBasicReport("Bad return type when passing NSError**",
+              "Method accepting NSError** argument should have "
+              "non-void return value to indicate that an error occurred.",
+              CodeDecl.getLocation());
     
-    // Check for a non-void return type.
-    if (M->getResultType() != Ctx.VoidTy)
-      continue;
-
-    for (ObjCMethodDecl::param_iterator PI=M->param_begin(), 
-         PE=M->param_end(); PI!=PE; ++PI) {
-      
-      const PointerType* PPT = (*PI)->getType()->getAsPointerType();
-      if (!PPT) continue;
-      
-      const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
-      if (!PT) continue;
-      
-      const ObjCInterfaceType *IT =
-        PT->getPointeeType()->getAsObjCInterfaceType();
-      
-      if (!IT) continue;
-      
-      // Check if IT is "NSError".
-      if (IT->getDecl()->getIdentifier() == NSErrorII) {
-        // Documentation: "Creating and Returning NSError Objects"
-        BR.EmitBasicReport("Bad return type when passing NSError**",
-         "Method accepting NSError** argument should have "
-         "non-void return value to indicate that an error occurred.",
-         M->getLocStart());
-        break;
-      }
-    }
   }
 }
+
+void NSErrorCheck::CheckSignature(ObjCMethodDecl& M, QualType& ResultTy,
+                                  llvm::SmallVectorImpl<VarDecl*>& Params,
+                                  IdentifierInfo* NSErrorII) {
+
+  ResultTy = M.getResultType();
+  
+  for (ObjCMethodDecl::param_iterator I=M.param_begin(), 
+       E=M.param_end(); I!=E; ++I) 
+    if (CheckArgument((*I)->getType(), NSErrorII))
+      Params.push_back(*I);
+}
+
+bool NSErrorCheck::CheckArgument(QualType ArgTy, IdentifierInfo* NSErrorII) {
+  const PointerType* PPT = ArgTy->getAsPointerType();
+  if (!PPT) return false;
+  
+  const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
+  if (!PT) return false;
+  
+  const ObjCInterfaceType *IT =
+  PT->getPointeeType()->getAsObjCInterfaceType();
+  
+  if (!IT) return false;
+  return IT->getDecl()->getIdentifier() == NSErrorII;
+}





More information about the cfe-commits mailing list