]> granicus.if.org Git - clang/commitdiff
Add prototype implementation of unused ivar check.
authorTed Kremenek <kremenek@apple.com>
Wed, 23 Jul 2008 00:45:26 +0000 (00:45 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 23 Jul 2008 00:45:26 +0000 (00:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53942 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/Analyses.def
Driver/AnalysisConsumer.cpp
include/clang/Analysis/LocalCheckers.h
lib/Analysis/CheckObjCUnusedIVars.cpp [new file with mode: 0644]

index 623a2475d6b77c0c444dc67a60719b6d272c09ee..841f0c1da1ec0c79fc71f02a66a78d0453ab4dd7 100644 (file)
@@ -33,6 +33,9 @@ ANALYSIS(WarnObjCMethSigs, "warn-objc-methodsigs",
 ANALYSIS(WarnObjCDealloc, "warn-objc-missing-dealloc",
  "Warn about Objective-C classes that lack a correct implementation of -dealloc",
  ObjCImplementation)
+ANALYSIS(WarnObjCUnusedIvars, "warn-objc-unused-ivars",
+  "Warn about private ivars that are never used", ObjCImplementation)
 
 ANALYSIS(CheckerSimple, "checker-simple",
          "Perform simple path-sensitive checks.", Code)
index deb7a85006c851f333e2869717d88a2b9b0e8bf3..134f6d65334d3959db5c9627adab4de43871630f 100644 (file)
@@ -393,6 +393,11 @@ static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
                    mgr.getLangOptions(), BR);  
 }
 
+static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
+  BugReporter BR(mgr);
+  CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);  
+}
+
 static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
   BugReporter BR(mgr);
   
index 8da144ebce112f8fd55e26d7e892958abc1c8605..23610f9a2d97fc0829c4205f273a801b358e5f54 100644 (file)
@@ -45,6 +45,7 @@ void CheckObjCDealloc(ObjCImplementationDecl* D, const LangOptions& L,
                       BugReporter& BR);
   
 void CheckObjCInstMethSignature(ObjCImplementationDecl* ID, BugReporter& BR);
+void CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR);
   
 void RegisterAppleChecks(GRExprEngine& Eng);
   
diff --git a/lib/Analysis/CheckObjCUnusedIVars.cpp b/lib/Analysis/CheckObjCUnusedIVars.cpp
new file mode 100644 (file)
index 0000000..4eb2733
--- /dev/null
@@ -0,0 +1,87 @@
+//==- CheckObjCUnusedIVars.cpp - Check for unused ivars ----------*- 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 CheckObjCUnusedIvars, a checker that
+//  analyzes an Objective-C class's interface/implementation to determine if it
+//  has any ivars that are never accessed.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/LocalCheckers.h"
+#include "clang/Analysis/PathDiagnostic.h"
+#include "clang/Analysis/PathSensitive/BugReporter.h"
+#include "clang/AST/ExprObjC.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/DeclObjC.h"
+#include "clang/Basic/LangOptions.h"
+#include <sstream>
+
+using namespace clang;
+
+enum IVarState { Unused, Used };
+typedef llvm::DenseMap<ObjCIvarDecl*,IVarState> IvarUsageMap;
+
+static void Scan(IvarUsageMap& M, Stmt* S) {
+  if (!S)
+    return;
+  
+  if (ObjCIvarRefExpr* Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
+    ObjCIvarDecl* D = Ex->getDecl();
+    IvarUsageMap::iterator I = M.find(D);
+    if (I != M.end()) I->second = Used;
+  }
+  else
+    for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E;++I)
+      Scan(M, *I);
+}
+
+void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) {
+
+  ObjCInterfaceDecl* ID = D->getClassInterface();
+  IvarUsageMap M;
+
+
+  
+  // Iterate over the ivars.
+  for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
+       I!=E; ++I) {
+    
+    ObjCIvarDecl* ID = *I;
+    
+    // Ignore ivars that aren't private.
+    ObjCIvarDecl::AccessControl ac = ID->getAccessControl();
+    if (!(ac == ObjCIvarDecl::None || ac == ObjCIvarDecl::Private))
+      continue;
+    
+    if (ID->getAttr<IBOutletAttr>() == 0)
+      continue;
+    
+    M[ID] = Unused;
+  }
+
+  if (M.empty())
+    return;
+  
+  // Now scan the methods for accesses.
+  for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
+       E = D->instmeth_end(); I!=E; ++I)
+    Scan(M, (*I)->getBody());
+  
+  // Find ivars that are unused.
+  for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
+    if (I->second == Unused) {
+      
+      std::ostringstream os;
+      os << "Private ivar '" << I->first->getName() << "' is never used.";
+      
+      BR.EmitBasicReport("unused ivar",
+                         os.str().c_str(), I->first->getLocation());
+    }
+}
+