Home | History | Annotate | Download | only in Core
      1 //===--- TextPathDiagnostics.cpp - Text Diagnostics for Paths ---*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines the TextPathDiagnostics object.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
     15 #include "clang/Lex/Preprocessor.h"
     16 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
     17 #include "llvm/Support/raw_ostream.h"
     18 using namespace clang;
     19 using namespace ento;
     20 using namespace llvm;
     21 
     22 namespace {
     23 
     24 /// \brief Simple path diagnostic client used for outputting as diagnostic notes
     25 /// the sequence of events.
     26 class TextPathDiagnostics : public PathDiagnosticConsumer {
     27   const std::string OutputFile;
     28   DiagnosticsEngine &Diag;
     29 
     30 public:
     31   TextPathDiagnostics(const std::string& output, DiagnosticsEngine &diag)
     32     : OutputFile(output), Diag(diag) {}
     33 
     34   void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
     35                             FilesMade *filesMade);
     36 
     37   virtual StringRef getName() const {
     38     return "TextPathDiagnostics";
     39   }
     40 
     41   PathGenerationScheme getGenerationScheme() const { return Minimal; }
     42   bool supportsLogicalOpControlFlow() const { return true; }
     43   bool supportsAllBlockEdges() const { return true; }
     44   virtual bool supportsCrossFileDiagnostics() const { return true; }
     45 };
     46 
     47 } // end anonymous namespace
     48 
     49 void ento::createTextPathDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
     50                                             PathDiagnosticConsumers &C,
     51                                             const std::string& out,
     52                                             const Preprocessor &PP) {
     53   C.push_back(new TextPathDiagnostics(out, PP.getDiagnostics()));
     54 }
     55 
     56 void TextPathDiagnostics::FlushDiagnosticsImpl(
     57                               std::vector<const PathDiagnostic *> &Diags,
     58                               FilesMade *) {
     59   for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
     60        et = Diags.end(); it != et; ++it) {
     61     const PathDiagnostic *D = *it;
     62 
     63     PathPieces FlatPath = D->path.flatten(/*ShouldFlattenMacros=*/true);
     64     for (PathPieces::const_iterator I = FlatPath.begin(), E = FlatPath.end();
     65          I != E; ++I) {
     66       unsigned diagID =
     67         Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note,
     68                                                  (*I)->getString());
     69       Diag.Report((*I)->getLocation().asLocation(), diagID);
     70     }
     71   }
     72 }
     73