blob: d87a8f189ff809e1303fda616750f33c52787f46 [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"
Ted Kremenekf0763392009-04-05 02:08:28 +000018#include "clang/Lex/Lexer.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000019#include "llvm/Support/Compiler.h"
20#include "llvm/Support/raw_ostream.h"
Ted Kremenek082cb8d2009-03-12 18:41:53 +000021#include "llvm/Support/Casting.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000022#include "llvm/System/Path.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallVector.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000025using namespace clang;
Ted Kremenek082cb8d2009-03-12 18:41:53 +000026using llvm::cast;
Chris Lattner2b2453a2009-01-17 06:22:33 +000027
28typedef llvm::DenseMap<FileID, unsigned> FIDMap;
Ted Kremenek5d866252008-11-03 22:33:57 +000029
Ted Kremenek4fc82c82008-11-03 23:18:07 +000030namespace clang {
31 class Preprocessor;
32 class PreprocessorFactory;
33}
34
Ted Kremenek5d866252008-11-03 22:33:57 +000035namespace {
36 class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient {
Ted Kremenekddf32da2009-01-21 00:42:24 +000037 std::vector<const PathDiagnostic*> BatchedDiags;
38 const std::string OutputFile;
Ted Kremenek5d866252008-11-03 22:33:57 +000039 public:
40 PlistDiagnostics(const std::string& prefix);
Ted Kremenekddf32da2009-01-21 00:42:24 +000041 ~PlistDiagnostics();
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000042 void HandlePathDiagnostic(const PathDiagnostic* D);
43
Ted Kremenek67f49642009-04-02 00:44:18 +000044 PathGenerationScheme getGenerationScheme() const { return Extensive; }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000045 bool supportsLogicalOpControlFlow() const { return true; }
Ted Kremenek7dc86642009-03-31 20:22:36 +000046 bool supportsAllBlockEdges() const { return true; }
Ted Kremenek5d866252008-11-03 22:33:57 +000047 };
48} // end anonymous namespace
49
Ted Kremenekddf32da2009-01-21 00:42:24 +000050PlistDiagnostics::PlistDiagnostics(const std::string& output)
51 : OutputFile(output) {}
Ted Kremenek5d866252008-11-03 22:33:57 +000052
Ted Kremenek4fc82c82008-11-03 23:18:07 +000053PathDiagnosticClient*
54clang::CreatePlistDiagnosticClient(const std::string& s,
55 Preprocessor*, PreprocessorFactory*) {
Ted Kremenek5d866252008-11-03 22:33:57 +000056 return new PlistDiagnostics(s);
57}
58
Chris Lattnera11d6172009-01-19 07:46:45 +000059static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000060 const SourceManager* SM, SourceLocation L) {
Ted Kremenek5d866252008-11-03 22:33:57 +000061
Ted Kremenekc472d792009-01-23 20:06:20 +000062 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000063 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000064 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000065 FIDs[FID] = V.size();
66 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000067}
68
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000069static unsigned GetFID(const FIDMap& FIDs, const SourceManager* SM,
70 SourceLocation L) {
Ted Kremenekc472d792009-01-23 20:06:20 +000071 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000072 FIDMap::const_iterator I = FIDs.find(FID);
73 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000074 return I->second;
75}
76
77static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +000078 for (unsigned i = 0; i < indent; ++i) o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000079 return o;
80}
81
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +000082static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM,
Ted Kremenek5d866252008-11-03 22:33:57 +000083 SourceLocation L, const FIDMap& FM,
Ted Kremenekf0763392009-04-05 02:08:28 +000084 const unsigned indent, bool extend = false) {
Ted Kremenek5d866252008-11-03 22:33:57 +000085
Ted Kremenekf0763392009-04-05 02:08:28 +000086 FullSourceLoc Loc(SM->getInstantiationLoc(L), const_cast<SourceManager&>(*SM));
87
88 // Add in the length of the token, so that we cover multi-char tokens.
89 unsigned offset = extend ? Lexer::MeasureTokenLength(Loc, *SM) - 1 : 0;
90
Ted Kremenek5d866252008-11-03 22:33:57 +000091 Indent(o, indent) << "<dict>\n";
92 Indent(o, indent) << " <key>line</key><integer>"
Ted Kremenekf0763392009-04-05 02:08:28 +000093 << Loc.getInstantiationLineNumber() << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000094 Indent(o, indent) << " <key>col</key><integer>"
Ted Kremenekf0763392009-04-05 02:08:28 +000095 << Loc.getInstantiationColumnNumber() + offset << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000096 Indent(o, indent) << " <key>file</key><integer>"
Ted Kremenekf0763392009-04-05 02:08:28 +000097 << GetFID(FM, SM, Loc) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000098 Indent(o, indent) << "</dict>\n";
99}
100
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000101static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM,
102 const PathDiagnosticLocation &L, const FIDMap& FM,
Ted Kremenekf0763392009-04-05 02:08:28 +0000103 const unsigned indent, bool extend = false) {
104 EmitLocation(o, SM, L.asLocation(), FM, indent, extend);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000105}
106
107static void EmitRange(llvm::raw_ostream& o, const SourceManager* SM,
108 SourceRange R, const FIDMap& FM,
109 const unsigned indent) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000110
111 Indent(o, indent) << "<array>\n";
Ted Kremenekf0763392009-04-05 02:08:28 +0000112 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
113 EmitLocation(o, SM, R.getEnd(), FM, indent+1, true);
Ted Kremenek5d866252008-11-03 22:33:57 +0000114 Indent(o, indent) << "</array>\n";
115}
116
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000117static llvm::raw_ostream& EmitString(llvm::raw_ostream& o,
118 const std::string& s) {
119 o << "<string>";
120 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) {
121 char c = *I;
122 switch (c) {
123 default: o << c; break;
124 case '&': o << "&amp;"; break;
125 case '<': o << "&lt;"; break;
126 case '>': o << "&gt;"; break;
127 case '\'': o << "&apos;"; break;
128 case '\"': o << "&quot;"; break;
129 }
130 }
131 o << "</string>";
132 return o;
133}
134
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000135static void ReportControlFlow(llvm::raw_ostream& o,
136 const PathDiagnosticControlFlowPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000137 const FIDMap& FM, const SourceManager *SM,
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000138 unsigned indent) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000139
Ted Kremenek5d866252008-11-03 22:33:57 +0000140 Indent(o, indent) << "<dict>\n";
141 ++indent;
142
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000143 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
144
Ted Kremenekf48fbc62009-03-27 15:53:20 +0000145 // FIXME: Eventually remove (DEPRECATED)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000146 // Output the start and end locations.
147 Indent(o, indent) << "<key>start</key>\n";
148 EmitLocation(o, SM, P.getStartLocation(), FM, indent);
149 Indent(o, indent) << "<key>end</key>\n";
150 EmitLocation(o, SM, P.getEndLocation(), FM, indent);
151
Ted Kremenekf48fbc62009-03-27 15:53:20 +0000152 // Emit edges.
153 Indent(o, indent) << "<key>edges</key>\n";
154 ++indent;
155 Indent(o, indent) << "<array>\n";
156 ++indent;
157 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
158 I!=E; ++I) {
159 Indent(o, indent) << "<dict>\n";
160 ++indent;
161 Indent(o, indent) << "<key>start</key>\n";
162 EmitRange(o, SM, I->getStart().asRange(), FM, indent+1);
163 Indent(o, indent) << "<key>end</key>\n";
164 EmitRange(o, SM, I->getEnd().asRange(), FM, indent+1);
165 --indent;
166 Indent(o, indent) << "</dict>\n";
167 }
168 --indent;
169 Indent(o, indent) << "</array>\n";
170 --indent;
171
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000172 // Output any helper text.
173 const std::string& s = P.getString();
174 if (!s.empty()) {
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000175 Indent(o, indent) << "<key>alternate</key>";
176 EmitString(o, s) << '\n';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000177 }
178
179 --indent;
180 Indent(o, indent) << "</dict>\n";
181}
182
183static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000184 const FIDMap& FM, const SourceManager* SM,
185 unsigned indent) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000186
187 Indent(o, indent) << "<dict>\n";
188 ++indent;
189
190 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
191
Ted Kremenek5d866252008-11-03 22:33:57 +0000192 // Output the location.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000193 FullSourceLoc L = P.getLocation().asLocation();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000194
Ted Kremenek5d866252008-11-03 22:33:57 +0000195 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000196 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000197
Ted Kremenek5d866252008-11-03 22:33:57 +0000198 // Output the ranges (if any).
199 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000200 RE = P.ranges_end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000201
202 if (RI != RE) {
203 Indent(o, indent) << "<key>ranges</key>\n";
204 Indent(o, indent) << "<array>\n";
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000205 ++indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000206 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000207 --indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000208 Indent(o, indent) << "</array>\n";
209 }
210
211 // Output the text.
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000212 assert(!P.getString().empty());
Ted Kremenek61dc71a2009-03-19 00:42:56 +0000213 Indent(o, indent) << "<key>extended_message</key>\n";
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000214 Indent(o, indent);
215 EmitString(o, P.getString()) << '\n';
Ted Kremenek61dc71a2009-03-19 00:42:56 +0000216
217 // Output the short text.
218 // FIXME: Really use a short string.
Ted Kremenek5d866252008-11-03 22:33:57 +0000219 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000220 EmitString(o, P.getString()) << '\n';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000221
Ted Kremenek5d866252008-11-03 22:33:57 +0000222 // Finish up.
223 --indent;
224 Indent(o, indent); o << "</dict>\n";
225}
226
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000227static void ReportMacro(llvm::raw_ostream& o,
228 const PathDiagnosticMacroPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000229 const FIDMap& FM, const SourceManager *SM,
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000230 unsigned indent) {
231
232 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
233 I!=E; ++I) {
234
235 switch ((*I)->getKind()) {
236 default:
237 break;
238 case PathDiagnosticPiece::Event:
239 ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, indent);
240 break;
241 case PathDiagnosticPiece::Macro:
242 ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, indent);
243 break;
244 }
245 }
246}
247
248static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000249 const FIDMap& FM, const SourceManager* SM) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000250
251 unsigned indent = 4;
252
253 switch (P.getKind()) {
254 case PathDiagnosticPiece::ControlFlow:
255 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
256 indent);
257 break;
258 case PathDiagnosticPiece::Event:
259 ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, indent);
260 break;
261 case PathDiagnosticPiece::Macro:
262 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, indent);
263 break;
264 }
265}
266
Ted Kremenek5d866252008-11-03 22:33:57 +0000267void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000268 if (!D)
269 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000270
Ted Kremenekddf32da2009-01-21 00:42:24 +0000271 if (D->empty()) {
272 delete D;
273 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000274 }
275
Ted Kremenek96a69262009-04-02 05:13:24 +0000276 // We need to flatten the locations (convert Stmt* to locations) because
277 // the referenced statements may be freed by the time the diagnostics
278 // are emitted.
279 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenekddf32da2009-01-21 00:42:24 +0000280 BatchedDiags.push_back(D);
281}
Ted Kremenek5d866252008-11-03 22:33:57 +0000282
Ted Kremenekddf32da2009-01-21 00:42:24 +0000283PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000284
285 // Build up a set of FIDs that we use by scanning the locations and
286 // ranges of the diagnostics.
287 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000288 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000289 const SourceManager* SM = 0;
Ted Kremenekc472d792009-01-23 20:06:20 +0000290
291 if (!BatchedDiags.empty())
292 SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000293
Ted Kremenekddf32da2009-01-21 00:42:24 +0000294 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
295 DE = BatchedDiags.end(); DI != DE; ++DI) {
296
297 const PathDiagnostic *D = *DI;
298
299 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000300 AddFID(FM, Fids, SM, I->getLocation().asLocation());
Ted Kremenekddf32da2009-01-21 00:42:24 +0000301
302 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
303 RE=I->ranges_end(); RI!=RE; ++RI) {
304 AddFID(FM, Fids, SM, RI->getBegin());
305 AddFID(FM, Fids, SM, RI->getEnd());
306 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000307 }
308 }
309
Ted Kremenekddf32da2009-01-21 00:42:24 +0000310 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000311 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000312 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000313 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000314 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000315 return;
316 }
317
318 // Write the plist header.
319 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000320 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
321 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
322 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000323
324 // Write the root object: a <dict> containing...
325 // - "files", an <array> mapping from FIDs to file names
326 // - "diagnostics", an <array> containing the path diagnostics
327 o << "<dict>\n"
328 " <key>files</key>\n"
329 " <array>\n";
330
Chris Lattner2b2453a2009-01-17 06:22:33 +0000331 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000332 I!=E; ++I) {
333 o << " ";
334 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n';
335 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000336
337 o << " </array>\n"
338 " <key>diagnostics</key>\n"
339 " <array>\n";
340
Ted Kremenekddf32da2009-01-21 00:42:24 +0000341 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
342 DE = BatchedDiags.end(); DI!=DE; ++DI) {
343
344 o << " <dict>\n"
345 " <key>path</key>\n";
346
347 const PathDiagnostic *D = *DI;
348 // Create an owning smart pointer for 'D' just so that we auto-free it
349 // when we exit this method.
350 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
351
352 o << " <array>\n";
353
354 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
355 ReportDiag(o, *I, FM, SM);
356
357 o << " </array>\n";
358
359 // Output the bug type and bug category.
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000360 o << " <key>description</key>";
361 EmitString(o, D->getDescription()) << '\n';
362 o << " <key>category</key>";
363 EmitString(o, D->getCategory()) << '\n';
364 o << " <key>type</key>";
365 EmitString(o, D->getBugType()) << '\n';
Ted Kremenekca1bada2009-03-27 15:31:11 +0000366
367 // Output the location of the bug.
368 o << " <key>location</key>\n";
369 EmitLocation(o, SM, D->getLocation(), FM, 2);
370
Ted Kremenekb0b6f722009-03-28 06:40:54 +0000371 // Close up the entry.
Ted Kremenekca1bada2009-03-27 15:31:11 +0000372 o << " </dict>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000373 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000374
375 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000376
Ted Kremenek5d866252008-11-03 22:33:57 +0000377 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000378 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000379}