[PATCH] Allow specifying a custom PathDiagnosticConsumer for use with the static analyzer.

Alexander Kornienko alexfh at google.com
Fri Jan 24 09:14:58 PST 2014


  > I'm not sure why "replace" is the right interface. Wouldn't it be better to
  > just have an AnalysisDiagClients entry for "no default
  > PathDiagnosticConsumers", and then be able to add to them?

  Yes, I considered this option too, but started with the other one for some
  reason. I've changed the implementation. Please take another look.

  > I'm also still not convinced that you really need a new
  > PathDiagnosticConsumer, rather than, say, improving the text diagnostics.

  I've posted a patch for clang-tidy, which adds code using this functionality, as
  an example: http://llvm-reviews.chandlerc.com/D2620

  The problem is, that if we don't add a custom PathDiagnosticConsumer, we'd need
  to pass the checker name through clang's diagnostic system, which has no
  dedicated facility for this. We could add it, but it doesn't seem generally
  useful for other parts of clang.

Hi jordan_rose, krememek,

http://llvm-reviews.chandlerc.com/D2556

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2556?vs=6471&id=6645#toc

Files:
  include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
  include/clang/StaticAnalyzer/Frontend/AnalysisConsumer.h
  lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Index: include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
===================================================================
--- include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
+++ include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
@@ -54,6 +54,7 @@
 enum AnalysisDiagClients {
 #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
 #include "clang/StaticAnalyzer/Core/Analyses.def"
+PD_NONE,
 NUM_ANALYSIS_DIAG_CLIENTS
 };
 
Index: include/clang/StaticAnalyzer/Frontend/AnalysisConsumer.h
===================================================================
--- include/clang/StaticAnalyzer/Frontend/AnalysisConsumer.h
+++ include/clang/StaticAnalyzer/Frontend/AnalysisConsumer.h
@@ -15,26 +15,32 @@
 #ifndef LLVM_CLANG_GR_ANALYSISCONSUMER_H
 #define LLVM_CLANG_GR_ANALYSISCONSUMER_H
 
+#include "clang/AST/ASTConsumer.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
 #include <string>
 
 namespace clang {
 
-class ASTConsumer;
 class Preprocessor;
 class DiagnosticsEngine;
 
 namespace ento {
 class CheckerManager;
 
+class AnalysisASTConsumer : public ASTConsumer {
+public:
+  virtual void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) = 0;
+};
+
 /// CreateAnalysisConsumer - Creates an ASTConsumer to run various code
 /// analysis passes.  (The set of analyses run is controlled by command-line
 /// options.)
-ASTConsumer* CreateAnalysisConsumer(const Preprocessor &pp,
-                                    const std::string &output,
-                                    AnalyzerOptionsRef opts,
-                                    ArrayRef<std::string> plugins);
+AnalysisASTConsumer *CreateAnalysisConsumer(const Preprocessor &pp,
+                                            const std::string &output,
+                                            AnalyzerOptionsRef opts,
+                                            ArrayRef<std::string> plugins);
 
 } // end GR namespace
 
Index: lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
===================================================================
--- lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
+++ lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
@@ -145,7 +145,7 @@
 
 namespace {
 
-class AnalysisConsumer : public ASTConsumer,
+class AnalysisConsumer : public AnalysisASTConsumer,
                          public DataRecursiveASTVisitor<AnalysisConsumer> {
   enum {
     AM_None = 0,
@@ -208,21 +208,24 @@
   }
 
   void DigestAnalyzerOptions() {
-    // Create the PathDiagnosticConsumer.
-    ClangDiagPathDiagConsumer *clangDiags =
-      new ClangDiagPathDiagConsumer(PP.getDiagnostics());
-    PathConsumers.push_back(clangDiags);
-
-    if (Opts->AnalysisDiagOpt == PD_TEXT) {
-      clangDiags->enablePaths();
-
-    } else if (!OutDir.empty()) {
-      switch (Opts->AnalysisDiagOpt) {
-      default:
-#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
-        case PD_##NAME: CREATEFN(*Opts.getPtr(), PathConsumers, OutDir, PP);\
-        break;
+    if (Opts->AnalysisDiagOpt != PD_NONE) {
+      // Create the PathDiagnosticConsumer.
+      ClangDiagPathDiagConsumer *clangDiags =
+          new ClangDiagPathDiagConsumer(PP.getDiagnostics());
+      PathConsumers.push_back(clangDiags);
+
+      if (Opts->AnalysisDiagOpt == PD_TEXT) {
+        clangDiags->enablePaths();
+
+      } else if (!OutDir.empty()) {
+        switch (Opts->AnalysisDiagOpt) {
+        default:
+#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN)                    \
+  case PD_##NAME:                                                              \
+    CREATEFN(*Opts.getPtr(), PathConsumers, OutDir, PP);                       \
+    break;
 #include "clang/StaticAnalyzer/Core/Analyses.def"
+        }
       }
     }
 
@@ -377,6 +380,11 @@
     return true;
   }
 
+  virtual void
+  AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) LLVM_OVERRIDE {
+    PathConsumers.push_back(Consumer);
+  }
+
 private:
   void storeTopLevelDecls(DeclGroupRef DG);
 
@@ -687,10 +695,10 @@
 // AnalysisConsumer creation.
 //===----------------------------------------------------------------------===//
 
-ASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp,
-                                          const std::string& outDir,
-                                          AnalyzerOptionsRef opts,
-                                          ArrayRef<std::string> plugins) {
+AnalysisASTConsumer *
+ento::CreateAnalysisConsumer(const Preprocessor &pp, const std::string &outDir,
+                             AnalyzerOptionsRef opts,
+                             ArrayRef<std::string> plugins) {
   // Disable the effects of '-Werror' when using the AnalysisConsumer.
   pp.getDiagnostics().setWarningsAsErrors(false);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2556.2.patch
Type: text/x-patch
Size: 4861 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140124/5966cb35/attachment.bin>


More information about the cfe-commits mailing list