1 //===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- 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 // This file defines the PlistDiagnostics object.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallVector.h"
23 using namespace clang;
26 typedef llvm::DenseMap<FileID, unsigned> FIDMap;
30 class PlistDiagnostics : public PathDiagnosticConsumer {
31 const std::string OutputFile;
32 const LangOptions &LangOpts;
33 OwningPtr<PathDiagnosticConsumer> SubPD;
35 const bool SupportsCrossFileDiagnostics;
37 PlistDiagnostics(const std::string& prefix, const LangOptions &LangOpts,
38 bool supportsMultipleFiles,
39 PathDiagnosticConsumer *subPD);
41 virtual ~PlistDiagnostics() {}
43 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
44 SmallVectorImpl<std::string> *FilesMade);
46 virtual StringRef getName() const {
47 return "PlistDiagnostics";
50 PathGenerationScheme getGenerationScheme() const;
51 bool supportsLogicalOpControlFlow() const { return true; }
52 bool supportsAllBlockEdges() const { return true; }
53 virtual bool useVerboseDescription() const { return false; }
54 virtual bool supportsCrossFileDiagnostics() const {
55 return SupportsCrossFileDiagnostics;
58 } // end anonymous namespace
60 PlistDiagnostics::PlistDiagnostics(const std::string& output,
61 const LangOptions &LO,
62 bool supportsMultipleFiles,
63 PathDiagnosticConsumer *subPD)
64 : OutputFile(output), LangOpts(LO), SubPD(subPD), flushed(false),
65 SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
67 PathDiagnosticConsumer*
68 ento::createPlistDiagnosticConsumer(const std::string& s, const Preprocessor &PP,
69 PathDiagnosticConsumer *subPD) {
70 return new PlistDiagnostics(s, PP.getLangOpts(), false, subPD);
73 PathDiagnosticConsumer*
74 ento::createPlistMultiFileDiagnosticConsumer(const std::string &s,
75 const Preprocessor &PP) {
76 return new PlistDiagnostics(s, PP.getLangOpts(), true, 0);
79 PathDiagnosticConsumer::PathGenerationScheme
80 PlistDiagnostics::getGenerationScheme() const {
81 if (const PathDiagnosticConsumer *PD = SubPD.get())
82 return PD->getGenerationScheme();
87 static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
88 const SourceManager* SM, SourceLocation L) {
90 FileID FID = SM->getFileID(SM->getExpansionLoc(L));
91 FIDMap::iterator I = FIDs.find(FID);
92 if (I != FIDs.end()) return;
97 static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM,
99 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
100 FIDMap::const_iterator I = FIDs.find(FID);
101 assert(I != FIDs.end());
105 static raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
106 for (unsigned i = 0; i < indent; ++i) o << ' ';
110 static void EmitLocation(raw_ostream &o, const SourceManager &SM,
111 const LangOptions &LangOpts,
112 SourceLocation L, const FIDMap &FM,
113 unsigned indent, bool extend = false) {
115 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM));
117 // Add in the length of the token, so that we cover multi-char tokens.
119 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
121 Indent(o, indent) << "<dict>\n";
122 Indent(o, indent) << " <key>line</key><integer>"
123 << Loc.getExpansionLineNumber() << "</integer>\n";
124 Indent(o, indent) << " <key>col</key><integer>"
125 << Loc.getExpansionColumnNumber() + offset << "</integer>\n";
126 Indent(o, indent) << " <key>file</key><integer>"
127 << GetFID(FM, SM, Loc) << "</integer>\n";
128 Indent(o, indent) << "</dict>\n";
131 static void EmitLocation(raw_ostream &o, const SourceManager &SM,
132 const LangOptions &LangOpts,
133 const PathDiagnosticLocation &L, const FIDMap& FM,
134 unsigned indent, bool extend = false) {
135 EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend);
138 static void EmitRange(raw_ostream &o, const SourceManager &SM,
139 const LangOptions &LangOpts,
140 PathDiagnosticRange R, const FIDMap &FM,
142 Indent(o, indent) << "<array>\n";
143 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1);
144 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint);
145 Indent(o, indent) << "</array>\n";
148 static raw_ostream &EmitString(raw_ostream &o, StringRef s) {
150 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
153 default: o << c; break;
154 case '&': o << "&"; break;
155 case '<': o << "<"; break;
156 case '>': o << ">"; break;
157 case '\'': o << "'"; break;
158 case '\"': o << """; break;
165 static void ReportControlFlow(raw_ostream &o,
166 const PathDiagnosticControlFlowPiece& P,
168 const SourceManager &SM,
169 const LangOptions &LangOpts,
172 Indent(o, indent) << "<dict>\n";
175 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
178 Indent(o, indent) << "<key>edges</key>\n";
180 Indent(o, indent) << "<array>\n";
182 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
184 Indent(o, indent) << "<dict>\n";
186 Indent(o, indent) << "<key>start</key>\n";
187 EmitRange(o, SM, LangOpts, I->getStart().asRange(), FM, indent+1);
188 Indent(o, indent) << "<key>end</key>\n";
189 EmitRange(o, SM, LangOpts, I->getEnd().asRange(), FM, indent+1);
191 Indent(o, indent) << "</dict>\n";
194 Indent(o, indent) << "</array>\n";
197 // Output any helper text.
198 const std::string& s = P.getString();
200 Indent(o, indent) << "<key>alternate</key>";
201 EmitString(o, s) << '\n';
205 Indent(o, indent) << "</dict>\n";
208 static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P,
210 const SourceManager &SM,
211 const LangOptions &LangOpts,
215 Indent(o, indent) << "<dict>\n";
218 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
220 // Output the location.
221 FullSourceLoc L = P.getLocation().asLocation();
223 Indent(o, indent) << "<key>location</key>\n";
224 EmitLocation(o, SM, LangOpts, L, FM, indent);
226 // Output the ranges (if any).
227 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
231 Indent(o, indent) << "<key>ranges</key>\n";
232 Indent(o, indent) << "<array>\n";
234 for (; RI != RE; ++RI)
235 EmitRange(o, SM, LangOpts, *RI, FM, indent+1);
237 Indent(o, indent) << "</array>\n";
240 // Output the call depth.
241 Indent(o, indent) << "<key>depth</key>"
242 << "<integer>" << depth << "</integer>\n";
245 assert(!P.getString().empty());
246 Indent(o, indent) << "<key>extended_message</key>\n";
248 EmitString(o, P.getString()) << '\n';
250 // Output the short text.
251 // FIXME: Really use a short string.
252 Indent(o, indent) << "<key>message</key>\n";
253 EmitString(o, P.getString()) << '\n';
257 Indent(o, indent); o << "</dict>\n";
260 static void ReportPiece(raw_ostream &o,
261 const PathDiagnosticPiece &P,
262 const FIDMap& FM, const SourceManager &SM,
263 const LangOptions &LangOpts,
266 bool includeControlFlow);
268 static void ReportCall(raw_ostream &o,
269 const PathDiagnosticCallPiece &P,
270 const FIDMap& FM, const SourceManager &SM,
271 const LangOptions &LangOpts,
275 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter =
276 P.getCallEnterEvent();
279 ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, depth, true);
281 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnterWithinCaller =
282 P.getCallEnterWithinCallerEvent();
286 if (callEnterWithinCaller)
287 ReportPiece(o, *callEnterWithinCaller, FM, SM, LangOpts,
288 indent, depth, true);
290 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
291 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, true);
293 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit =
294 P.getCallExitEvent();
297 ReportPiece(o, *callExit, FM, SM, LangOpts, indent, depth, true);
300 static void ReportMacro(raw_ostream &o,
301 const PathDiagnosticMacroPiece& P,
302 const FIDMap& FM, const SourceManager &SM,
303 const LangOptions &LangOpts,
307 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
309 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, false);
313 static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P,
314 const FIDMap& FM, const SourceManager &SM,
315 const LangOptions &LangOpts) {
316 ReportPiece(o, P, FM, SM, LangOpts, 4, 0, true);
319 static void ReportPiece(raw_ostream &o,
320 const PathDiagnosticPiece &P,
321 const FIDMap& FM, const SourceManager &SM,
322 const LangOptions &LangOpts,
325 bool includeControlFlow) {
326 switch (P.getKind()) {
327 case PathDiagnosticPiece::ControlFlow:
328 if (includeControlFlow)
329 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
332 case PathDiagnosticPiece::Call:
333 ReportCall(o, cast<PathDiagnosticCallPiece>(P), FM, SM, LangOpts,
336 case PathDiagnosticPiece::Event:
337 ReportEvent(o, cast<PathDiagnosticSpotPiece>(P), FM, SM, LangOpts,
340 case PathDiagnosticPiece::Macro:
341 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts,
347 void PlistDiagnostics::FlushDiagnosticsImpl(
348 std::vector<const PathDiagnostic *> &Diags,
349 SmallVectorImpl<std::string> *FilesMade) {
350 // Build up a set of FIDs that we use by scanning the locations and
351 // ranges of the diagnostics.
353 SmallVector<FileID, 10> Fids;
354 const SourceManager* SM = 0;
357 SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager();
360 for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(),
361 DE = Diags.end(); DI != DE; ++DI) {
363 const PathDiagnostic *D = *DI;
365 llvm::SmallVector<const PathPieces *, 5> WorkList;
366 WorkList.push_back(&D->path);
368 while (!WorkList.empty()) {
369 const PathPieces &path = *WorkList.back();
372 for (PathPieces::const_iterator I = path.begin(), E = path.end();
374 const PathDiagnosticPiece *piece = I->getPtr();
375 AddFID(FM, Fids, SM, piece->getLocation().asLocation());
377 for (PathDiagnosticPiece::range_iterator RI = piece->ranges_begin(),
378 RE= piece->ranges_end(); RI != RE; ++RI) {
379 AddFID(FM, Fids, SM, RI->getBegin());
380 AddFID(FM, Fids, SM, RI->getEnd());
383 if (const PathDiagnosticCallPiece *call =
384 dyn_cast<PathDiagnosticCallPiece>(piece)) {
385 WorkList.push_back(&call->path);
387 else if (const PathDiagnosticMacroPiece *macro =
388 dyn_cast<PathDiagnosticMacroPiece>(piece)) {
389 WorkList.push_back(¯o->subPieces);
397 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg);
398 if (!ErrMsg.empty()) {
399 llvm::errs() << "warning: could not create file: " << OutputFile << '\n';
403 // Write the plist header.
404 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
405 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
406 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
407 "<plist version=\"1.0\">\n";
409 // Write the root object: a <dict> containing...
410 // - "files", an <array> mapping from FIDs to file names
411 // - "diagnostics", an <array> containing the path diagnostics
413 " <key>files</key>\n"
416 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
419 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n';
423 " <key>diagnostics</key>\n"
426 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
427 DE = Diags.end(); DI!=DE; ++DI) {
430 " <key>path</key>\n";
432 const PathDiagnostic *D = *DI;
436 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end();
438 ReportDiag(o, **I, FM, *SM, LangOpts);
442 // Output the bug type and bug category.
443 o << " <key>description</key>";
444 EmitString(o, D->getDescription()) << '\n';
445 o << " <key>category</key>";
446 EmitString(o, D->getCategory()) << '\n';
447 o << " <key>type</key>";
448 EmitString(o, D->getBugType()) << '\n';
450 // Output information about the semantic context where
451 // the issue occurred.
452 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
453 // FIXME: handle blocks, which have no name.
454 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
456 switch (ND->getKind()) {
457 case Decl::CXXRecord:
458 declKind = "C++ class";
460 case Decl::CXXMethod:
461 declKind = "C++ method";
463 case Decl::ObjCMethod:
464 declKind = "Objective-C method";
467 declKind = "function";
472 if (!declKind.empty()) {
473 const std::string &declName = ND->getDeclName().getAsString();
474 o << " <key>issue_context_kind</key>";
475 EmitString(o, declKind) << '\n';
476 o << " <key>issue_context</key>";
477 EmitString(o, declName) << '\n';
482 // Output the location of the bug.
483 o << " <key>location</key>\n";
484 EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2);
486 // Output the diagnostic to the sub-diagnostic client, if any.
488 std::vector<const PathDiagnostic *> SubDiags;
489 SubDiags.push_back(D);
490 SmallVector<std::string, 1> SubFilesMade;
491 SubPD->FlushDiagnosticsImpl(SubDiags, &SubFilesMade);
493 if (!SubFilesMade.empty()) {
494 o << " <key>" << SubPD->getName() << "_files</key>\n";
496 for (size_t i = 0, n = SubFilesMade.size(); i < n ; ++i)
497 o << " <string>" << SubFilesMade[i] << "</string>\n";
502 // Close up the entry.
509 o << "</dict>\n</plist>";
512 FilesMade->push_back(OutputFile);