[cfe-commits] r123921 - in /cfe/trunk: lib/StaticAnalyzer/Checkers/AnalysisConsumer.cpp test/Analysis/dead-stores.cpp

Ted Kremenek kremenek at apple.com
Thu Jan 20 09:09:49 PST 2011


Author: kremenek
Date: Thu Jan 20 11:09:48 2011
New Revision: 123921

URL: http://llvm.org/viewvc/llvm-project?rev=123921&view=rev
Log:
Enhance AnalysisConsumer to also visit functions
and methods defined within 'namespace X { ... }'.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/AnalysisConsumer.cpp
    cfe/trunk/test/Analysis/dead-stores.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/AnalysisConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/AnalysisConsumer.cpp?rev=123921&r1=123920&r2=123921&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/AnalysisConsumer.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/AnalysisConsumer.cpp Thu Jan 20 11:09:48 2011
@@ -198,6 +198,8 @@
   }
 
   virtual void HandleTranslationUnit(ASTContext &C);
+  void HandleDeclContext(ASTContext &C, DeclContext *dc);
+
   void HandleCode(Decl *D, Actions& actions);
 };
 } // end anonymous namespace
@@ -206,55 +208,61 @@
 // AnalysisConsumer implementation.
 //===----------------------------------------------------------------------===//
 
-void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
-
-  TranslationUnitDecl *TU = C.getTranslationUnitDecl();
-
-  for (DeclContext::decl_iterator I = TU->decls_begin(), E = TU->decls_end();
+void AnalysisConsumer::HandleDeclContext(ASTContext &C, DeclContext *dc) {
+  for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end();
        I != E; ++I) {
     Decl *D = *I;
-
+    
     switch (D->getKind()) {
-    case Decl::CXXConstructor:
-    case Decl::CXXDestructor:
-    case Decl::CXXConversion:
-    case Decl::CXXMethod:
-    case Decl::Function: {
-      FunctionDecl* FD = cast<FunctionDecl>(D);
-      // We skip function template definitions, as their semantics is
-      // only determined when they are instantiated.
-      if (FD->isThisDeclarationADefinition() &&
-          !FD->isDependentContext()) {
-        if (!Opts.AnalyzeSpecificFunction.empty() &&
-            FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction)
-          break;
-        DisplayFunction(FD);
-        HandleCode(FD, FunctionActions);
+      case Decl::Namespace: {
+        HandleDeclContext(C, cast<NamespaceDecl>(D));
+        break;
       }
-      break;
-    }
-
-    case Decl::ObjCImplementation: {
-      ObjCImplementationDecl* ID = cast<ObjCImplementationDecl>(*I);
-      HandleCode(ID, ObjCImplementationActions);
-
-      for (ObjCImplementationDecl::method_iterator MI = ID->meth_begin(), 
-             ME = ID->meth_end(); MI != ME; ++MI) {
-        if ((*MI)->isThisDeclarationADefinition()) {
+      case Decl::CXXConstructor:
+      case Decl::CXXDestructor:
+      case Decl::CXXConversion:
+      case Decl::CXXMethod:
+      case Decl::Function: {
+        FunctionDecl* FD = cast<FunctionDecl>(D);
+        // We skip function template definitions, as their semantics is
+        // only determined when they are instantiated.
+        if (FD->isThisDeclarationADefinition() &&
+            !FD->isDependentContext()) {
           if (!Opts.AnalyzeSpecificFunction.empty() &&
-             Opts.AnalyzeSpecificFunction != (*MI)->getSelector().getAsString())
+              FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction)
             break;
-          DisplayFunction(*MI);
-          HandleCode(*MI, ObjCMethodActions);
+          DisplayFunction(FD);
+          HandleCode(FD, FunctionActions);
         }
+        break;
       }
-      break;
+        
+      case Decl::ObjCImplementation: {
+        ObjCImplementationDecl* ID = cast<ObjCImplementationDecl>(*I);
+        HandleCode(ID, ObjCImplementationActions);
+        
+        for (ObjCImplementationDecl::method_iterator MI = ID->meth_begin(), 
+             ME = ID->meth_end(); MI != ME; ++MI) {
+          if ((*MI)->isThisDeclarationADefinition()) {
+            if (!Opts.AnalyzeSpecificFunction.empty() &&
+                Opts.AnalyzeSpecificFunction != (*MI)->getSelector().getAsString())
+              break;
+            DisplayFunction(*MI);
+            HandleCode(*MI, ObjCMethodActions);
+          }
+        }
+        break;
+      }
+        
+      default:
+        break;
     }
+  }  
+}
 
-    default:
-      break;
-    }
-  }
+void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
+  TranslationUnitDecl *TU = C.getTranslationUnitDecl();
+  HandleDeclContext(C, TU);
 
   for (TUActions::iterator I = TranslationUnitActions.begin(),
                            E = TranslationUnitActions.end(); I != E; ++I) {

Modified: cfe/trunk/test/Analysis/dead-stores.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.cpp?rev=123921&r1=123920&r2=123921&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.cpp (original)
+++ cfe/trunk/test/Analysis/dead-stores.cpp Thu Jan 20 11:09:48 2011
@@ -100,3 +100,15 @@
   char **p = new char* [n]; // expected-warning{{never read}}
 }
 
+//===----------------------------------------------------------------------===//
+// Dead stores in namespaces.
+//===----------------------------------------------------------------------===//
+
+namespace foo {
+  int test_4(int x) {
+    x = 2; // expected-warning{{Value stored to 'x' is never read}}
+    x = 2;
+    return x;
+  }
+}
+





More information about the cfe-commits mailing list