blob: 03c6088027a2d593c023ca73ec1b46a8c5fb6e60 [file] [log] [blame]
Argyrios Kyrtzidisebd4f522010-12-03 00:58:14 +00001//===--- 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/Checker/PathDiagnosticClients.h"
15#include "clang/Checker/BugReporter/PathDiagnostic.h"
16#include "clang/Lex/Preprocessor.h"
17#include "llvm/Support/raw_ostream.h"
18using namespace clang;
19using namespace llvm;
20
21namespace {
22
23/// \brief Simple path diagnostic client used for outputting as text
24/// the sequence of events.
25class TextPathDiagnostics : public PathDiagnosticClient {
26 const std::string OutputFile;
27 Diagnostic &Diag;
28
29public:
30 TextPathDiagnostics(const std::string& output, Diagnostic &diag)
31 : OutputFile(output), Diag(diag) {}
32
33 void HandlePathDiagnostic(const PathDiagnostic* D);
34
35 void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade) { }
36
37 virtual llvm::StringRef getName() const {
38 return "TextPathDiagnostics";
39 }
40
41 PathGenerationScheme getGenerationScheme() const { return Extensive; }
42 bool supportsLogicalOpControlFlow() const { return true; }
43 bool supportsAllBlockEdges() const { return true; }
44 virtual bool useVerboseDescription() const { return true; }
45};
46
47} // end anonymous namespace
48
49PathDiagnosticClient*
50clang::createTextPathDiagnosticClient(const std::string& out,
51 const Preprocessor &PP) {
52 return new TextPathDiagnostics(out, PP.getDiagnostics());
53}
54
55void TextPathDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
56 if (!D)
57 return;
58
59 if (D->empty()) {
60 delete D;
61 return;
62 }
63
64 // Open the file.
65 std::string ErrMsg;
66 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg);
67 if (!ErrMsg.empty()) {
68 llvm::errs() << "warning: could not create file: " << OutputFile << '\n';
69 return;
70 }
71
72 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) {
73 if (isa<PathDiagnosticEventPiece>(*I)) {
74 PathDiagnosticEventPiece &event = cast<PathDiagnosticEventPiece>(*I);
75 unsigned diagID = Diag.getDiagnosticIDs()->getCustomDiagID(
76 DiagnosticIDs::Note, event.getString());
77 Diag.Report(event.getLocation().asLocation(), diagID);
78 }
79 }
80}