blob: d1bc90fdbe166dd1e1b65ffaf89ee82d385ca096 [file] [log] [blame]
Argyrios Kyrtzidis7ee20492011-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"
11#include "clang/Lex/Lexer.h"
12#include "clang/Basic/SourceManager.h"
13#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000014using namespace clang;
15using namespace arcmt;
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000016
17// FIXME: This duplicates significant functionality from PlistDiagnostics.cpp,
18// it would be jolly good if there was a reusable PlistWriter or something.
19
20typedef llvm::DenseMap<FileID, unsigned> FIDMap;
21
Chris Lattner5f9e2722011-07-23 10:55:15 +000022static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000023 const SourceManager &SM, SourceLocation L) {
24
Chandler Carruth40278532011-07-25 16:49:02 +000025 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000026 FIDMap::iterator I = FIDs.find(FID);
27 if (I != FIDs.end()) return;
28 FIDs[FID] = V.size();
29 V.push_back(FID);
30}
31
32static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM,
33 SourceLocation L) {
Chandler Carruth40278532011-07-25 16:49:02 +000034 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000035 FIDMap::const_iterator I = FIDs.find(FID);
36 assert(I != FIDs.end());
37 return I->second;
38}
39
Chris Lattner5f9e2722011-07-23 10:55:15 +000040static raw_ostream& Indent(raw_ostream& o, const unsigned indent) {
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000041 for (unsigned i = 0; i < indent; ++i) o << ' ';
42 return o;
43}
44
Chris Lattner5f9e2722011-07-23 10:55:15 +000045static void EmitLocation(raw_ostream& o, const SourceManager &SM,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000046 const LangOptions &LangOpts,
47 SourceLocation L, const FIDMap &FM,
48 unsigned indent, bool extend = false) {
49
Chandler Carruth40278532011-07-25 16:49:02 +000050 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM));
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000051
52 // Add in the length of the token, so that we cover multi-char tokens.
53 unsigned offset =
54 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
55
56 Indent(o, indent) << "<dict>\n";
57 Indent(o, indent) << " <key>line</key><integer>"
Chandler Carruth64211622011-07-25 21:09:52 +000058 << Loc.getExpansionLineNumber() << "</integer>\n";
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000059 Indent(o, indent) << " <key>col</key><integer>"
Chandler Carrutha77c0312011-07-25 20:57:57 +000060 << Loc.getExpansionColumnNumber() + offset << "</integer>\n";
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000061 Indent(o, indent) << " <key>file</key><integer>"
62 << GetFID(FM, SM, Loc) << "</integer>\n";
63 Indent(o, indent) << "</dict>\n";
64}
65
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static void EmitRange(raw_ostream& o, const SourceManager &SM,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000067 const LangOptions &LangOpts,
68 CharSourceRange R, const FIDMap &FM,
69 unsigned indent) {
70 Indent(o, indent) << "<array>\n";
71 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1);
72 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, R.isTokenRange());
73 Indent(o, indent) << "</array>\n";
74}
75
Chris Lattner5f9e2722011-07-23 10:55:15 +000076static raw_ostream& EmitString(raw_ostream& o,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000077 StringRef s) {
78 o << "<string>";
79 for (StringRef::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) {
80 char c = *I;
81 switch (c) {
82 default: o << c; break;
83 case '&': o << "&amp;"; break;
84 case '<': o << "&lt;"; break;
85 case '>': o << "&gt;"; break;
86 case '\'': o << "&apos;"; break;
87 case '\"': o << "&quot;"; break;
88 }
89 }
90 o << "</string>";
91 return o;
92}
93
94void arcmt::writeARCDiagsToPlist(const std::string &outPath,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000095 ArrayRef<StoredDiagnostic> diags,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000096 SourceManager &SM,
97 const LangOptions &LangOpts) {
98 DiagnosticIDs DiagIDs;
99
100 // Build up a set of FIDs that we use by scanning the locations and
101 // ranges of the diagnostics.
102 FIDMap FM;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 SmallVector<FileID, 10> Fids;
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000104
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000105 for (ArrayRef<StoredDiagnostic>::iterator
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000106 I = diags.begin(), E = diags.end(); I != E; ++I) {
107 const StoredDiagnostic &D = *I;
108
109 AddFID(FM, Fids, SM, D.getLocation());
110
111 for (StoredDiagnostic::range_iterator
112 RI = D.range_begin(), RE = D.range_end(); RI != RE; ++RI) {
113 AddFID(FM, Fids, SM, RI->getBegin());
114 AddFID(FM, Fids, SM, RI->getEnd());
115 }
116 }
117
118 std::string errMsg;
119 llvm::raw_fd_ostream o(outPath.c_str(), errMsg);
120 if (!errMsg.empty()) {
121 llvm::errs() << "error: could not create file: " << outPath << '\n';
122 return;
123 }
124
125 // Write the plist header.
126 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
127 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
128 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
129 "<plist version=\"1.0\">\n";
130
131 // Write the root object: a <dict> containing...
132 // - "files", an <array> mapping from FIDs to file names
133 // - "diagnostics", an <array> containing the diagnostics
134 o << "<dict>\n"
135 " <key>files</key>\n"
136 " <array>\n";
137
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000139 I!=E; ++I) {
140 o << " ";
141 EmitString(o, SM.getFileEntryForID(*I)->getName()) << '\n';
142 }
143
144 o << " </array>\n"
145 " <key>diagnostics</key>\n"
146 " <array>\n";
147
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000148 for (ArrayRef<StoredDiagnostic>::iterator
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000149 DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) {
150
151 const StoredDiagnostic &D = *DI;
152
David Blaikied6471f72011-09-25 23:23:43 +0000153 if (D.getLevel() == DiagnosticsEngine::Ignored)
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000154 continue;
155
156 o << " <dict>\n";
157
158 // Output the diagnostic.
159 o << " <key>description</key>";
160 EmitString(o, D.getMessage()) << '\n';
161 o << " <key>category</key>";
162 EmitString(o, DiagIDs.getCategoryNameFromID(
163 DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n';
164 o << " <key>type</key>";
David Blaikied6471f72011-09-25 23:23:43 +0000165 if (D.getLevel() >= DiagnosticsEngine::Error)
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000166 EmitString(o, "error") << '\n';
David Blaikied6471f72011-09-25 23:23:43 +0000167 else if (D.getLevel() == DiagnosticsEngine::Warning)
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000168 EmitString(o, "warning") << '\n';
169 else
170 EmitString(o, "note") << '\n';
171
172 // Output the location of the bug.
173 o << " <key>location</key>\n";
174 EmitLocation(o, SM, LangOpts, D.getLocation(), FM, 2);
175
176 // Output the ranges (if any).
177 StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end();
178
179 if (RI != RE) {
180 o << " <key>ranges</key>\n";
181 o << " <array>\n";
182 for (; RI != RE; ++RI)
183 EmitRange(o, SM, LangOpts, *RI, FM, 4);
184 o << " </array>\n";
185 }
186
187 // Close up the entry.
188 o << " </dict>\n";
189 }
190
191 o << " </array>\n";
192
193 // Finish.
194 o << "</dict>\n</plist>";
195}