1 //===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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;
19 static StringRef getLevelName(DiagnosticsEngine::Level Level) {
21 case DiagnosticsEngine::Ignored:
22 llvm_unreachable("ignored");
23 case DiagnosticsEngine::Note:
25 case DiagnosticsEngine::Remark:
26 case DiagnosticsEngine::Warning:
28 case DiagnosticsEngine::Fatal:
29 case DiagnosticsEngine::Error:
32 llvm_unreachable("Invalid DiagnosticsEngine level!");
35 void arcmt::writeARCDiagsToPlist(const std::string &outPath,
36 ArrayRef<StoredDiagnostic> diags,
38 const LangOptions &LangOpts) {
39 DiagnosticIDs DiagIDs;
41 // Build up a set of FIDs that we use by scanning the locations and
42 // ranges of the diagnostics.
44 SmallVector<FileID, 10> Fids;
46 for (ArrayRef<StoredDiagnostic>::iterator
47 I = diags.begin(), E = diags.end(); I != E; ++I) {
48 const StoredDiagnostic &D = *I;
50 AddFID(FM, Fids, SM, D.getLocation());
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());
60 llvm::raw_fd_ostream o(outPath, EC, llvm::sys::fs::F_Text);
62 llvm::errs() << "error: could not create file: " << outPath << '\n';
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
75 for (FileID FID : Fids)
76 EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
79 " <key>diagnostics</key>\n"
82 for (ArrayRef<StoredDiagnostic>::iterator
83 DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) {
85 const StoredDiagnostic &D = *DI;
87 if (D.getLevel() == DiagnosticsEngine::Ignored)
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';
101 // Output the location of the bug.
102 o << " <key>location</key>\n";
103 EmitLocation(o, SM, D.getLocation(), FM, 2);
105 // Output the ranges (if any).
106 if (!D.getRanges().empty()) {
107 o << " <key>ranges</key>\n";
109 for (auto &R : D.getRanges()) {
110 CharSourceRange ExpansionRange = SM.getExpansionRange(R);
111 EmitRange(o, SM, Lexer::getAsCharRange(ExpansionRange, SM, LangOpts),
117 // Close up the entry.
124 o << "</dict>\n</plist>";