clang 8.0.0

PlistReporter.cpp
Go to the documentation of this file.
1 //===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- 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 #include "Internals.h"
11 #include "clang/Basic/FileManager.h"
12 #include "clang/Basic/PlistSupport.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Lex/Lexer.h"
15 using namespace clang;
16 using namespace arcmt;
17 using namespace markup;
18
19 static StringRef getLevelName(DiagnosticsEngine::Level Level) {
20 switch (Level) {
21 case DiagnosticsEngine::Ignored:
22 llvm_unreachable("ignored");
23 case DiagnosticsEngine::Note:
24 return "note";
25 case DiagnosticsEngine::Remark:
26 case DiagnosticsEngine::Warning:
27 return "warning";
28 case DiagnosticsEngine::Fatal:
29 case DiagnosticsEngine::Error:
30 return "error";
31 }
32 llvm_unreachable("Invalid DiagnosticsEngine level!");
33 }
34
35 void arcmt::writeARCDiagsToPlist(const std::string &outPath,
36 ArrayRef<StoredDiagnostic> diags,
37 SourceManager &SM,
38 const LangOptions &LangOpts) {
39 DiagnosticIDs DiagIDs;
40
41 // Build up a set of FIDs that we use by scanning the locations and
42 // ranges of the diagnostics.
43 FIDMap FM;
44 SmallVector<FileID, 10> Fids;
45
46 for (ArrayRef<StoredDiagnostic>::iterator
47 I = diags.begin(), E = diags.end(); I != E; ++I) {
48 const StoredDiagnostic &D = *I;
49
50 AddFID(FM, Fids, SM, D.getLocation());
51
52 for (StoredDiagnostic::range_iterator
53 RI = D.range_begin(), RE = D.range_end(); RI != RE; ++RI) {
54 AddFID(FM, Fids, SM, RI->getBegin());
55 AddFID(FM, Fids, SM, RI->getEnd());
56 }
57 }
58
59 std::error_code EC;
60 llvm::raw_fd_ostream o(outPath, EC, llvm::sys::fs::F_Text);
61 if (EC) {
62 llvm::errs() << "error: could not create file: " << outPath << '\n';
63 return;
64 }
65
66 EmitPlistHeader(o);
67
68 // Write the root object: a <dict> containing...
69 // - "files", an <array> mapping from FIDs to file names
70 // - "diagnostics", an <array> containing the diagnostics
71 o << "<dict>\n"
72 " <key>files</key>\n"
73 " <array>\n";
74
75 for (FileID FID : Fids)
76 EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
77
78 o << " </array>\n"
79 " <key>diagnostics</key>\n"
80 " <array>\n";
81
82 for (ArrayRef<StoredDiagnostic>::iterator
83 DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) {
84
85 const StoredDiagnostic &D = *DI;
86
87 if (D.getLevel() == DiagnosticsEngine::Ignored)
88 continue;
89
90 o << " <dict>\n";
91
92 // Output the diagnostic.
93 o << " <key>description</key>";
94 EmitString(o, D.getMessage()) << '\n';
95 o << " <key>category</key>";
96 EmitString(o, DiagIDs.getCategoryNameFromID(
97 DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n';
98 o << " <key>type</key>";
99 EmitString(o, getLevelName(D.getLevel())) << '\n';
100
101 // Output the location of the bug.
102 o << " <key>location</key>\n";
103 EmitLocation(o, SM, D.getLocation(), FM, 2);
104
105 // Output the ranges (if any).
106 if (!D.getRanges().empty()) {
107 o << " <key>ranges</key>\n";
108 o << " <array>\n";
109 for (auto &R : D.getRanges()) {
110 CharSourceRange ExpansionRange = SM.getExpansionRange(R);
111 EmitRange(o, SM, Lexer::getAsCharRange(ExpansionRange, SM, LangOpts),
112 FM, 4);
113 }
114 o << " </array>\n";
115 }
116
117 // Close up the entry.
118 o << " </dict>\n";
119 }
120
121 o << " </array>\n";
122
123 // Finish.
124 o << "</dict>\n</plist>";
125 }
clang::DiagnosticIDs::getCategoryNumberForDiag
static unsigned getCategoryNumberForDiag(unsigned DiagID)
Return the category number that a specified DiagID belongs to, or 0 if no category.
Definition: DiagnosticIDs.cpp:192
clang::StoredDiagnostic::getID
unsigned getID() const
Definition: Diagnostic.h:1458
clang::markup::EmitLocation
void EmitLocation(raw_ostream &o, const SourceManager &SM, SourceLocation L, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:108
FileManager.h
Defines the clang::FileManager interface and associated types.
clang::StoredDiagnostic::range_begin
range_iterator range_begin() const
Definition: Diagnostic.h:1467
SourceManager.h
Defines the SourceManager interface.
clang::StoredDiagnostic
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1437
Internals.h
clang::StoredDiagnostic::range_end
range_iterator range_end() const
Definition: Diagnostic.h:1468
clang::DiagnosticIDs::getCategoryNameFromID
static StringRef getCategoryNameFromID(unsigned CategoryID)
Given a category ID, return the name of the category.
Definition: DiagnosticIDs.cpp:241
markup
clang::markup::AddFID
unsigned AddFID(FIDMap &FIDs, SmallVectorImpl< FileID > &V, FileID FID)
Definition: PlistSupport.h:28
clang::DiagnosticsEngine::Note
Definition: Diagnostic.h:154
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
clang::StoredDiagnostic::getRanges
ArrayRef< CharSourceRange > getRanges() const
Definition: Diagnostic.h:1471
clang::StoredDiagnostic::getMessage
StringRef getMessage() const
Definition: Diagnostic.h:1461
llvm::ArrayRef
Definition: LLVM.h:32
clang::DiagnosticsEngine::Ignored
Definition: Diagnostic.h:153
getLevelName
static StringRef getLevelName(DiagnosticsEngine::Level Level)
Definition: PlistReporter.cpp:19
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:236
clang::StoredDiagnostic::getLevel
DiagnosticsEngine::Level getLevel() const
Definition: Diagnostic.h:1459
clang::SourceManager::getFileEntryForID
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Definition: SourceManager.h:986
Lexer.h
clang::arcmt::writeARCDiagsToPlist
void writeARCDiagsToPlist(const std::string &outPath, ArrayRef< StoredDiagnostic > diags, SourceManager &SM, const LangOptions &LangOpts)
Definition: PlistReporter.cpp:35
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::markup::FIDMap
llvm::DenseMap< FileID, unsigned > FIDMap
Definition: PlistSupport.h:26
PlistSupport.h
clang::FileEntry::getName
StringRef getName() const
Definition: FileManager.h:85
clang::DiagnosticsEngine::Remark
Definition: Diagnostic.h:155
llvm::SmallVector
Definition: LLVM.h:36
clang::markup::EmitPlistHeader
raw_ostream & EmitPlistHeader(raw_ostream &o)
Definition: PlistSupport.h:63
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::StoredDiagnostic::getLocation
const FullSourceLoc & getLocation() const
Definition: Diagnostic.h:1460
clang::DiagnosticIDs
Used for handling and querying diagnostic IDs.
Definition: DiagnosticIDs.h:164
clang::markup::EmitRange
void EmitRange(raw_ostream &o, const SourceManager &SM, CharSourceRange R, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:124
clang::DiagnosticsEngine::Warning
Definition: Diagnostic.h:156
clang::Lexer::getAsCharRange
static CharSourceRange getAsCharRange(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Given a token range, produce a corresponding CharSourceRange that is not a token range.
Definition: Lexer.h:380
clang::DiagnosticsEngine::Error
Definition: Diagnostic.h:157
clang::SourceManager::getExpansionRange
CharSourceRange getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
Definition: SourceManager.cpp:978
clang::markup::EmitString
raw_ostream & EmitString(raw_ostream &o, StringRef s)
Definition: PlistSupport.h:79
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
clang::prec::Level
Level
Definition: OperatorPrecedence.h:27
clang::StoredDiagnostic::range_iterator
std::vector< CharSourceRange >::const_iterator range_iterator
Definition: Diagnostic.h:1465
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::DiagnosticsEngine::Fatal
Definition: Diagnostic.h:158