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/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
16 #include "clang/Lex/Preprocessor.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:
TextPathDiagnostics(const std::string & output,DiagnosticsEngine & diag)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
getName() const37 virtual StringRef getName() const {
38 return "TextPathDiagnostics";
39 }
40
getGenerationScheme() const41 PathGenerationScheme getGenerationScheme() const { return Minimal; }
supportsLogicalOpControlFlow() const42 bool supportsLogicalOpControlFlow() const { return true; }
supportsAllBlockEdges() const43 bool supportsAllBlockEdges() const { return true; }
supportsCrossFileDiagnostics() const44 virtual bool supportsCrossFileDiagnostics() const { return true; }
45 };
46
47 } // end anonymous namespace
48
createTextPathDiagnosticConsumer(PathDiagnosticConsumers & C,const std::string & out,const Preprocessor & PP)49 void ento::createTextPathDiagnosticConsumer(PathDiagnosticConsumers &C,
50 const std::string& out,
51 const Preprocessor &PP) {
52 C.push_back(new TextPathDiagnostics(out, PP.getDiagnostics()));
53 }
54
FlushDiagnosticsImpl(std::vector<const PathDiagnostic * > & Diags,FilesMade *)55 void TextPathDiagnostics::FlushDiagnosticsImpl(
56 std::vector<const PathDiagnostic *> &Diags,
57 FilesMade *) {
58 for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
59 et = Diags.end(); it != et; ++it) {
60 const PathDiagnostic *D = *it;
61
62 PathPieces FlatPath = D->path.flatten(/*ShouldFlattenMacros=*/true);
63 for (PathPieces::const_iterator I = FlatPath.begin(), E = FlatPath.end();
64 I != E; ++I) {
65 unsigned diagID =
66 Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note,
67 (*I)->getString());
68 Diag.Report((*I)->getLocation().asLocation(), diagID);
69 }
70 }
71 }
72