blob: 9649cf618817bec7856ffbd760bc4f969d6a7ad6 [file] [log] [blame]
Ted Kremenek5d866252008-11-03 22:33:57 +00001//===--- PlistDiagnostics.cpp - Plist 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 PlistDiagnostics object.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/PathDiagnosticClients.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000015#include "clang/Analysis/PathDiagnostic.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Basic/FileManager.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/raw_ostream.h"
Ted Kremenek082cb8d2009-03-12 18:41:53 +000020#include "llvm/Support/Casting.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000021#include "llvm/System/Path.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/SmallVector.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000024using namespace clang;
Ted Kremenek082cb8d2009-03-12 18:41:53 +000025using llvm::cast;
Chris Lattner2b2453a2009-01-17 06:22:33 +000026
27typedef llvm::DenseMap<FileID, unsigned> FIDMap;
Ted Kremenek5d866252008-11-03 22:33:57 +000028
Ted Kremenek4fc82c82008-11-03 23:18:07 +000029namespace clang {
30 class Preprocessor;
31 class PreprocessorFactory;
32}
33
Ted Kremenek5d866252008-11-03 22:33:57 +000034namespace {
35 class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient {
Ted Kremenekddf32da2009-01-21 00:42:24 +000036 std::vector<const PathDiagnostic*> BatchedDiags;
37 const std::string OutputFile;
Ted Kremenek5d866252008-11-03 22:33:57 +000038 public:
39 PlistDiagnostics(const std::string& prefix);
Ted Kremenekddf32da2009-01-21 00:42:24 +000040 ~PlistDiagnostics();
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000041 void HandlePathDiagnostic(const PathDiagnostic* D);
42
Ted Kremenek67f49642009-04-02 00:44:18 +000043 PathGenerationScheme getGenerationScheme() const { return Extensive; }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000044 bool supportsLogicalOpControlFlow() const { return true; }
Ted Kremenek7dc86642009-03-31 20:22:36 +000045 bool supportsAllBlockEdges() const { return true; }
Ted Kremenek5d866252008-11-03 22:33:57 +000046 };
47} // end anonymous namespace
48
Ted Kremenekddf32da2009-01-21 00:42:24 +000049PlistDiagnostics::PlistDiagnostics(const std::string& output)
50 : OutputFile(output) {}
Ted Kremenek5d866252008-11-03 22:33:57 +000051
Ted Kremenek4fc82c82008-11-03 23:18:07 +000052PathDiagnosticClient*
53clang::CreatePlistDiagnosticClient(const std::string& s,
54 Preprocessor*, PreprocessorFactory*) {
Ted Kremenek5d866252008-11-03 22:33:57 +000055 return new PlistDiagnostics(s);
56}
57
Chris Lattnera11d6172009-01-19 07:46:45 +000058static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000059 const SourceManager* SM, SourceLocation L) {
Ted Kremenek5d866252008-11-03 22:33:57 +000060
Ted Kremenekc472d792009-01-23 20:06:20 +000061 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000062 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000063 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000064 FIDs[FID] = V.size();
65 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000066}
67
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000068static unsigned GetFID(const FIDMap& FIDs, const SourceManager* SM,
69 SourceLocation L) {
Ted Kremenekc472d792009-01-23 20:06:20 +000070 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000071 FIDMap::const_iterator I = FIDs.find(FID);
72 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000073 return I->second;
74}
75
76static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +000077 for (unsigned i = 0; i < indent; ++i) o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000078 return o;
79}
80
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000081static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM,
Ted Kremenek5d866252008-11-03 22:33:57 +000082 SourceLocation L, const FIDMap& FM,
83 const unsigned indent) {
84
85 Indent(o, indent) << "<dict>\n";
86 Indent(o, indent) << " <key>line</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000087 << SM->getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000088 Indent(o, indent) << " <key>col</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000089 << SM->getInstantiationColumnNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000090 Indent(o, indent) << " <key>file</key><integer>"
91 << GetFID(FM, SM, L) << "</integer>\n";
92 Indent(o, indent) << "</dict>\n";
93}
94
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000095static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM,
96 const PathDiagnosticLocation &L, const FIDMap& FM,
97 const unsigned indent) {
98 EmitLocation(o, SM, L.asLocation(), FM, indent);
99}
100
101static void EmitRange(llvm::raw_ostream& o, const SourceManager* SM,
102 SourceRange R, const FIDMap& FM,
103 const unsigned indent) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000104
105 Indent(o, indent) << "<array>\n";
106 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
107 EmitLocation(o, SM, R.getEnd(), FM, indent+1);
108 Indent(o, indent) << "</array>\n";
109}
110
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000111static llvm::raw_ostream& EmitString(llvm::raw_ostream& o,
112 const std::string& s) {
113 o << "<string>";
114 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) {
115 char c = *I;
116 switch (c) {
117 default: o << c; break;
118 case '&': o << "&amp;"; break;
119 case '<': o << "&lt;"; break;
120 case '>': o << "&gt;"; break;
121 case '\'': o << "&apos;"; break;
122 case '\"': o << "&quot;"; break;
123 }
124 }
125 o << "</string>";
126 return o;
127}
128
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000129static void ReportControlFlow(llvm::raw_ostream& o,
130 const PathDiagnosticControlFlowPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000131 const FIDMap& FM, const SourceManager *SM,
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000132 unsigned indent) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000133
Ted Kremenek5d866252008-11-03 22:33:57 +0000134 Indent(o, indent) << "<dict>\n";
135 ++indent;
136
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000137 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
138
Ted Kremenekf48fbc62009-03-27 15:53:20 +0000139 // FIXME: Eventually remove (DEPRECATED)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000140 // Output the start and end locations.
141 Indent(o, indent) << "<key>start</key>\n";
142 EmitLocation(o, SM, P.getStartLocation(), FM, indent);
143 Indent(o, indent) << "<key>end</key>\n";
144 EmitLocation(o, SM, P.getEndLocation(), FM, indent);
145
Ted Kremenekf48fbc62009-03-27 15:53:20 +0000146 // Emit edges.
147 Indent(o, indent) << "<key>edges</key>\n";
148 ++indent;
149 Indent(o, indent) << "<array>\n";
150 ++indent;
151 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
152 I!=E; ++I) {
153 Indent(o, indent) << "<dict>\n";
154 ++indent;
155 Indent(o, indent) << "<key>start</key>\n";
156 EmitRange(o, SM, I->getStart().asRange(), FM, indent+1);
157 Indent(o, indent) << "<key>end</key>\n";
158 EmitRange(o, SM, I->getEnd().asRange(), FM, indent+1);
159 --indent;
160 Indent(o, indent) << "</dict>\n";
161 }
162 --indent;
163 Indent(o, indent) << "</array>\n";
164 --indent;
165
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000166 // Output any helper text.
167 const std::string& s = P.getString();
168 if (!s.empty()) {
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000169 Indent(o, indent) << "<key>alternate</key>";
170 EmitString(o, s) << '\n';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000171 }
172
173 --indent;
174 Indent(o, indent) << "</dict>\n";
175}
176
177static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000178 const FIDMap& FM, const SourceManager* SM,
179 unsigned indent) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000180
181 Indent(o, indent) << "<dict>\n";
182 ++indent;
183
184 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
185
Ted Kremenek5d866252008-11-03 22:33:57 +0000186 // Output the location.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000187 FullSourceLoc L = P.getLocation().asLocation();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000188
Ted Kremenek5d866252008-11-03 22:33:57 +0000189 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000190 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000191
Ted Kremenek5d866252008-11-03 22:33:57 +0000192 // Output the ranges (if any).
193 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000194 RE = P.ranges_end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000195
196 if (RI != RE) {
197 Indent(o, indent) << "<key>ranges</key>\n";
198 Indent(o, indent) << "<array>\n";
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000199 ++indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000200 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000201 --indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000202 Indent(o, indent) << "</array>\n";
203 }
204
205 // Output the text.
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000206 assert(!P.getString().empty());
Ted Kremenek61dc71a2009-03-19 00:42:56 +0000207 Indent(o, indent) << "<key>extended_message</key>\n";
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000208 Indent(o, indent);
209 EmitString(o, P.getString()) << '\n';
Ted Kremenek61dc71a2009-03-19 00:42:56 +0000210
211 // Output the short text.
212 // FIXME: Really use a short string.
Ted Kremenek5d866252008-11-03 22:33:57 +0000213 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000214 EmitString(o, P.getString()) << '\n';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000215
Ted Kremenek5d866252008-11-03 22:33:57 +0000216 // Finish up.
217 --indent;
218 Indent(o, indent); o << "</dict>\n";
219}
220
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000221static void ReportMacro(llvm::raw_ostream& o,
222 const PathDiagnosticMacroPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000223 const FIDMap& FM, const SourceManager *SM,
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000224 unsigned indent) {
225
226 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
227 I!=E; ++I) {
228
229 switch ((*I)->getKind()) {
230 default:
231 break;
232 case PathDiagnosticPiece::Event:
233 ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, indent);
234 break;
235 case PathDiagnosticPiece::Macro:
236 ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, indent);
237 break;
238 }
239 }
240}
241
242static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000243 const FIDMap& FM, const SourceManager* SM) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000244
245 unsigned indent = 4;
246
247 switch (P.getKind()) {
248 case PathDiagnosticPiece::ControlFlow:
249 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
250 indent);
251 break;
252 case PathDiagnosticPiece::Event:
253 ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, indent);
254 break;
255 case PathDiagnosticPiece::Macro:
256 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, indent);
257 break;
258 }
259}
260
Ted Kremenek5d866252008-11-03 22:33:57 +0000261void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000262 if (!D)
263 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000264
Ted Kremenekddf32da2009-01-21 00:42:24 +0000265 if (D->empty()) {
266 delete D;
267 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000268 }
269
Ted Kremenekddf32da2009-01-21 00:42:24 +0000270 BatchedDiags.push_back(D);
271}
Ted Kremenek5d866252008-11-03 22:33:57 +0000272
Ted Kremenekddf32da2009-01-21 00:42:24 +0000273PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000274
275 // Build up a set of FIDs that we use by scanning the locations and
276 // ranges of the diagnostics.
277 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000278 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000279 const SourceManager* SM = 0;
Ted Kremenekc472d792009-01-23 20:06:20 +0000280
281 if (!BatchedDiags.empty())
282 SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000283
Ted Kremenekddf32da2009-01-21 00:42:24 +0000284 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
285 DE = BatchedDiags.end(); DI != DE; ++DI) {
286
287 const PathDiagnostic *D = *DI;
288
289 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000290 AddFID(FM, Fids, SM, I->getLocation().asLocation());
Ted Kremenekddf32da2009-01-21 00:42:24 +0000291
292 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
293 RE=I->ranges_end(); RI!=RE; ++RI) {
294 AddFID(FM, Fids, SM, RI->getBegin());
295 AddFID(FM, Fids, SM, RI->getEnd());
296 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000297 }
298 }
299
Ted Kremenekddf32da2009-01-21 00:42:24 +0000300 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000301 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000302 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000303 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000304 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000305 return;
306 }
307
308 // Write the plist header.
309 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000310 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
311 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
312 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000313
314 // Write the root object: a <dict> containing...
315 // - "files", an <array> mapping from FIDs to file names
316 // - "diagnostics", an <array> containing the path diagnostics
317 o << "<dict>\n"
318 " <key>files</key>\n"
319 " <array>\n";
320
Chris Lattner2b2453a2009-01-17 06:22:33 +0000321 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000322 I!=E; ++I) {
323 o << " ";
324 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n';
325 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000326
327 o << " </array>\n"
328 " <key>diagnostics</key>\n"
329 " <array>\n";
330
Ted Kremenekddf32da2009-01-21 00:42:24 +0000331 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
332 DE = BatchedDiags.end(); DI!=DE; ++DI) {
333
334 o << " <dict>\n"
335 " <key>path</key>\n";
336
337 const PathDiagnostic *D = *DI;
338 // Create an owning smart pointer for 'D' just so that we auto-free it
339 // when we exit this method.
340 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
341
342 o << " <array>\n";
343
344 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
345 ReportDiag(o, *I, FM, SM);
346
347 o << " </array>\n";
348
349 // Output the bug type and bug category.
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000350 o << " <key>description</key>";
351 EmitString(o, D->getDescription()) << '\n';
352 o << " <key>category</key>";
353 EmitString(o, D->getCategory()) << '\n';
354 o << " <key>type</key>";
355 EmitString(o, D->getBugType()) << '\n';
Ted Kremenekca1bada2009-03-27 15:31:11 +0000356
357 // Output the location of the bug.
358 o << " <key>location</key>\n";
359 EmitLocation(o, SM, D->getLocation(), FM, 2);
360
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000361 // Close up the entry.
Ted Kremenekca1bada2009-03-27 15:31:11 +0000362 o << " </dict>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000363 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000364
365 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000366
Ted Kremenek5d866252008-11-03 22:33:57 +0000367 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000368 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000369}