blob: cd55625554f71bbbda2adc375d164fa8699c9158 [file] [log] [blame]
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00001//===--- 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"
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000011#include "clang/Basic/FileManager.h"
Alp Tokerbdbcfbf2014-01-25 11:14:41 +000012#include "clang/Basic/PlistSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Basic/SourceManager.h"
14#include "clang/Lex/Lexer.h"
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000015using namespace clang;
16using namespace arcmt;
Alp Toker48047f52014-01-25 14:38:41 +000017using namespace markup;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000018
Alp Toker06973622014-07-06 04:27:03 +000019static 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
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000035void arcmt::writeARCDiagsToPlist(const std::string &outPath,
Chris Lattner54b16772011-07-23 17:14:25 +000036 ArrayRef<StoredDiagnostic> diags,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000037 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;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000044 SmallVector<FileID, 10> Fids;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000045
Chris Lattner54b16772011-07-23 17:14:25 +000046 for (ArrayRef<StoredDiagnostic>::iterator
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000047 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::string errMsg;
Rafael Espindola4fbd3732014-02-24 18:20:21 +000060 llvm::raw_fd_ostream o(outPath.c_str(), errMsg, llvm::sys::fs::F_Text);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000061 if (!errMsg.empty()) {
62 llvm::errs() << "error: could not create file: " << outPath << '\n';
63 return;
64 }
65
66 // Write the plist header.
Alp Tokerbdbcfbf2014-01-25 11:14:41 +000067 o << PlistHeader;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000068
69 // Write the root object: a <dict> containing...
70 // - "files", an <array> mapping from FIDs to file names
71 // - "diagnostics", an <array> containing the diagnostics
72 o << "<dict>\n"
73 " <key>files</key>\n"
74 " <array>\n";
75
Chris Lattner0e62c1c2011-07-23 10:55:15 +000076 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000077 I!=E; ++I) {
78 o << " ";
79 EmitString(o, SM.getFileEntryForID(*I)->getName()) << '\n';
80 }
81
82 o << " </array>\n"
83 " <key>diagnostics</key>\n"
84 " <array>\n";
85
Chris Lattner54b16772011-07-23 17:14:25 +000086 for (ArrayRef<StoredDiagnostic>::iterator
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000087 DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) {
88
89 const StoredDiagnostic &D = *DI;
90
David Blaikie9c902b52011-09-25 23:23:43 +000091 if (D.getLevel() == DiagnosticsEngine::Ignored)
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000092 continue;
93
94 o << " <dict>\n";
95
96 // Output the diagnostic.
97 o << " <key>description</key>";
98 EmitString(o, D.getMessage()) << '\n';
99 o << " <key>category</key>";
100 EmitString(o, DiagIDs.getCategoryNameFromID(
101 DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n';
102 o << " <key>type</key>";
Alp Toker06973622014-07-06 04:27:03 +0000103 EmitString(o, getLevelName(D.getLevel())) << '\n';
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000104
105 // Output the location of the bug.
106 o << " <key>location</key>\n";
107 EmitLocation(o, SM, LangOpts, D.getLocation(), FM, 2);
108
109 // Output the ranges (if any).
110 StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end();
111
112 if (RI != RE) {
113 o << " <key>ranges</key>\n";
114 o << " <array>\n";
115 for (; RI != RE; ++RI)
116 EmitRange(o, SM, LangOpts, *RI, FM, 4);
117 o << " </array>\n";
118 }
119
120 // Close up the entry.
121 o << " </dict>\n";
122 }
123
124 o << " </array>\n";
125
126 // Finish.
127 o << "</dict>\n</plist>";
128}