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/AnalyzerOptions.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Basic/Version.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
20 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
28 typedef llvm::DenseMap<FileID, unsigned> FIDMap;
32 class PlistDiagnostics : public PathDiagnosticConsumer {
33 const std::string OutputFile;
34 const LangOptions &LangOpts;
35 const bool SupportsCrossFileDiagnostics;
37 PlistDiagnostics(AnalyzerOptions &AnalyzerOpts,
38 const std::string& prefix,
39 const LangOptions &LangOpts,
40 bool supportsMultipleFiles);
42 virtual ~PlistDiagnostics() {}
44 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
45 FilesMade *filesMade);
47 virtual StringRef getName() const {
48 return "PlistDiagnostics";
51 PathGenerationScheme getGenerationScheme() const { return Extensive; }
52 bool supportsLogicalOpControlFlow() const { return true; }
53 bool supportsAllBlockEdges() const { return true; }
54 virtual bool supportsCrossFileDiagnostics() const {
55 return SupportsCrossFileDiagnostics;
58 } // end anonymous namespace
60 PlistDiagnostics::PlistDiagnostics(AnalyzerOptions &AnalyzerOpts,
61 const std::string& output,
62 const LangOptions &LO,
63 bool supportsMultipleFiles)
66 SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
68 void ento::createPlistDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
69 PathDiagnosticConsumers &C,
71 const Preprocessor &PP) {
72 C.push_back(new PlistDiagnostics(AnalyzerOpts, s,
73 PP.getLangOpts(), false));
76 void ento::createPlistMultiFileDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
77 PathDiagnosticConsumers &C,
79 const Preprocessor &PP) {
80 C.push_back(new PlistDiagnostics(AnalyzerOpts, s,
81 PP.getLangOpts(), true));
84 static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
85 const SourceManager* SM, SourceLocation L) {
87 FileID FID = SM->getFileID(SM->getExpansionLoc(L));
88 FIDMap::iterator I = FIDs.find(FID);
89 if (I != FIDs.end()) return;
94 static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM,
96 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
97 FIDMap::const_iterator I = FIDs.find(FID);
98 assert(I != FIDs.end());
102 static raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
103 for (unsigned i = 0; i < indent; ++i) o << ' ';
107 static void EmitLocation(raw_ostream &o, const SourceManager &SM,
108 const LangOptions &LangOpts,
109 SourceLocation L, const FIDMap &FM,
110 unsigned indent, bool extend = false) {
112 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM));
114 // Add in the length of the token, so that we cover multi-char tokens.
116 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
118 Indent(o, indent) << "<dict>\n";
119 Indent(o, indent) << " <key>line</key><integer>"
120 << Loc.getExpansionLineNumber() << "</integer>\n";
121 Indent(o, indent) << " <key>col</key><integer>"
122 << Loc.getExpansionColumnNumber() + offset << "</integer>\n";
123 Indent(o, indent) << " <key>file</key><integer>"
124 << GetFID(FM, SM, Loc) << "</integer>\n";
125 Indent(o, indent) << "</dict>\n";
128 static void EmitLocation(raw_ostream &o, const SourceManager &SM,
129 const LangOptions &LangOpts,
130 const PathDiagnosticLocation &L, const FIDMap& FM,
131 unsigned indent, bool extend = false) {
132 EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend);
135 static void EmitRange(raw_ostream &o, const SourceManager &SM,
136 const LangOptions &LangOpts,
137 PathDiagnosticRange R, const FIDMap &FM,
139 Indent(o, indent) << "<array>\n";
140 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1);
141 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint);
142 Indent(o, indent) << "</array>\n";
145 static raw_ostream &EmitString(raw_ostream &o, StringRef s) {
147 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
150 default: o << c; break;
151 case '&': o << "&"; break;
152 case '<': o << "<"; break;
153 case '>': o << ">"; break;
154 case '\'': o << "'"; break;
155 case '\"': o << """; break;
162 static void ReportControlFlow(raw_ostream &o,
163 const PathDiagnosticControlFlowPiece& P,
165 const SourceManager &SM,
166 const LangOptions &LangOpts,
169 Indent(o, indent) << "<dict>\n";
172 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
175 Indent(o, indent) << "<key>edges</key>\n";
177 Indent(o, indent) << "<array>\n";
179 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
181 Indent(o, indent) << "<dict>\n";
184 // Make the ranges of the start and end point self-consistent with adjacent edges
185 // by forcing to use only the beginning of the range. This simplifies the layout
186 // logic for clients.
187 Indent(o, indent) << "<key>start</key>\n";
188 SourceLocation StartEdge = I->getStart().asRange().getBegin();
189 EmitRange(o, SM, LangOpts, SourceRange(StartEdge, StartEdge), FM, indent+1);
191 Indent(o, indent) << "<key>end</key>\n";
192 SourceLocation EndEdge = I->getEnd().asRange().getBegin();
193 EmitRange(o, SM, LangOpts, SourceRange(EndEdge, EndEdge), FM, indent+1);
196 Indent(o, indent) << "</dict>\n";
199 Indent(o, indent) << "</array>\n";
202 // Output any helper text.
203 const std::string& s = P.getString();
205 Indent(o, indent) << "<key>alternate</key>";
206 EmitString(o, s) << '\n';
210 Indent(o, indent) << "</dict>\n";
213 static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P,
215 const SourceManager &SM,
216 const LangOptions &LangOpts,
220 Indent(o, indent) << "<dict>\n";
223 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
225 // Output the location.
226 FullSourceLoc L = P.getLocation().asLocation();
228 Indent(o, indent) << "<key>location</key>\n";
229 EmitLocation(o, SM, LangOpts, L, FM, indent);
231 // Output the ranges (if any).
232 ArrayRef<SourceRange> Ranges = P.getRanges();
234 if (!Ranges.empty()) {
235 Indent(o, indent) << "<key>ranges</key>\n";
236 Indent(o, indent) << "<array>\n";
238 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
240 EmitRange(o, SM, LangOpts, *I, FM, indent+1);
243 Indent(o, indent) << "</array>\n";
246 // Output the call depth.
247 Indent(o, indent) << "<key>depth</key>"
248 << "<integer>" << depth << "</integer>\n";
251 assert(!P.getString().empty());
252 Indent(o, indent) << "<key>extended_message</key>\n";
254 EmitString(o, P.getString()) << '\n';
256 // Output the short text.
257 // FIXME: Really use a short string.
258 Indent(o, indent) << "<key>message</key>\n";
260 EmitString(o, P.getString()) << '\n';
264 Indent(o, indent); o << "</dict>\n";
267 static void ReportPiece(raw_ostream &o,
268 const PathDiagnosticPiece &P,
269 const FIDMap& FM, const SourceManager &SM,
270 const LangOptions &LangOpts,
273 bool includeControlFlow);
275 static void ReportCall(raw_ostream &o,
276 const PathDiagnosticCallPiece &P,
277 const FIDMap& FM, const SourceManager &SM,
278 const LangOptions &LangOpts,
282 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter =
283 P.getCallEnterEvent();
286 ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, depth, true);
288 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnterWithinCaller =
289 P.getCallEnterWithinCallerEvent();
293 if (callEnterWithinCaller)
294 ReportPiece(o, *callEnterWithinCaller, FM, SM, LangOpts,
295 indent, depth, true);
297 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
298 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, true);
302 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit =
303 P.getCallExitEvent();
306 ReportPiece(o, *callExit, FM, SM, LangOpts, indent, depth, true);
309 static void ReportMacro(raw_ostream &o,
310 const PathDiagnosticMacroPiece& P,
311 const FIDMap& FM, const SourceManager &SM,
312 const LangOptions &LangOpts,
316 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
318 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, false);
322 static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P,
323 const FIDMap& FM, const SourceManager &SM,
324 const LangOptions &LangOpts) {
325 ReportPiece(o, P, FM, SM, LangOpts, 4, 0, true);
328 static void ReportPiece(raw_ostream &o,
329 const PathDiagnosticPiece &P,
330 const FIDMap& FM, const SourceManager &SM,
331 const LangOptions &LangOpts,
334 bool includeControlFlow) {
335 switch (P.getKind()) {
336 case PathDiagnosticPiece::ControlFlow:
337 if (includeControlFlow)
338 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
341 case PathDiagnosticPiece::Call:
342 ReportCall(o, cast<PathDiagnosticCallPiece>(P), FM, SM, LangOpts,
345 case PathDiagnosticPiece::Event:
346 ReportEvent(o, cast<PathDiagnosticSpotPiece>(P), FM, SM, LangOpts,
349 case PathDiagnosticPiece::Macro:
350 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts,
356 void PlistDiagnostics::FlushDiagnosticsImpl(
357 std::vector<const PathDiagnostic *> &Diags,
358 FilesMade *filesMade) {
359 // Build up a set of FIDs that we use by scanning the locations and
360 // ranges of the diagnostics.
362 SmallVector<FileID, 10> Fids;
363 const SourceManager* SM = 0;
366 SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager();
369 for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(),
370 DE = Diags.end(); DI != DE; ++DI) {
372 const PathDiagnostic *D = *DI;
374 SmallVector<const PathPieces *, 5> WorkList;
375 WorkList.push_back(&D->path);
377 while (!WorkList.empty()) {
378 const PathPieces &path = *WorkList.back();
381 for (PathPieces::const_iterator I = path.begin(), E = path.end();
383 const PathDiagnosticPiece *piece = I->getPtr();
384 AddFID(FM, Fids, SM, piece->getLocation().asLocation());
385 ArrayRef<SourceRange> Ranges = piece->getRanges();
386 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
387 E = Ranges.end(); I != E; ++I) {
388 AddFID(FM, Fids, SM, I->getBegin());
389 AddFID(FM, Fids, SM, I->getEnd());
392 if (const PathDiagnosticCallPiece *call =
393 dyn_cast<PathDiagnosticCallPiece>(piece)) {
394 IntrusiveRefCntPtr<PathDiagnosticEventPiece>
395 callEnterWithin = call->getCallEnterWithinCallerEvent();
397 AddFID(FM, Fids, SM, callEnterWithin->getLocation().asLocation());
399 WorkList.push_back(&call->path);
401 else if (const PathDiagnosticMacroPiece *macro =
402 dyn_cast<PathDiagnosticMacroPiece>(piece)) {
403 WorkList.push_back(¯o->subPieces);
411 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg);
412 if (!ErrMsg.empty()) {
413 llvm::errs() << "warning: could not create file: " << OutputFile << '\n';
417 // Write the plist header.
418 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
419 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
420 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
421 "<plist version=\"1.0\">\n";
423 // Write the root object: a <dict> containing...
424 // - "clang_version", the string representation of clang version
425 // - "files", an <array> mapping from FIDs to file names
426 // - "diagnostics", an <array> containing the path diagnostics
428 " <key>clang_version</key>\n";
429 EmitString(o, getClangFullVersion()) << '\n';
430 o << " <key>files</key>\n"
433 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
436 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n';
440 " <key>diagnostics</key>\n"
443 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
444 DE = Diags.end(); DI!=DE; ++DI) {
447 " <key>path</key>\n";
449 const PathDiagnostic *D = *DI;
453 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end();
455 ReportDiag(o, **I, FM, *SM, LangOpts);
459 // Output the bug type and bug category.
460 o << " <key>description</key>";
461 EmitString(o, D->getShortDescription()) << '\n';
462 o << " <key>category</key>";
463 EmitString(o, D->getCategory()) << '\n';
464 o << " <key>type</key>";
465 EmitString(o, D->getBugType()) << '\n';
467 // Output information about the semantic context where
468 // the issue occurred.
469 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
470 // FIXME: handle blocks, which have no name.
471 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
473 switch (ND->getKind()) {
474 case Decl::CXXRecord:
475 declKind = "C++ class";
477 case Decl::CXXMethod:
478 declKind = "C++ method";
480 case Decl::ObjCMethod:
481 declKind = "Objective-C method";
484 declKind = "function";
489 if (!declKind.empty()) {
490 const std::string &declName = ND->getDeclName().getAsString();
491 o << " <key>issue_context_kind</key>";
492 EmitString(o, declKind) << '\n';
493 o << " <key>issue_context</key>";
494 EmitString(o, declName) << '\n';
497 // Output the bug hash for issue unique-ing. Currently, it's just an
498 // offset from the beginning of the function.
499 if (const Stmt *Body = DeclWithIssue->getBody()) {
501 // If the bug uniqueing location exists, use it for the hash.
502 // For example, this ensures that two leaks reported on the same line
503 // will have different issue_hashes and that the hash will identify
504 // the leak location even after code is added between the allocation
505 // site and the end of scope (leak report location).
506 PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
507 if (UPDLoc.isValid()) {
508 FullSourceLoc UL(SM->getExpansionLoc(UPDLoc.asLocation()),
510 FullSourceLoc UFunL(SM->getExpansionLoc(
511 D->getUniqueingDecl()->getBody()->getLocStart()), *SM);
512 o << " <key>issue_hash</key><string>"
513 << UL.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
516 // Otherwise, use the location on which the bug is reported.
518 FullSourceLoc L(SM->getExpansionLoc(D->getLocation().asLocation()),
520 FullSourceLoc FunL(SM->getExpansionLoc(Body->getLocStart()), *SM);
521 o << " <key>issue_hash</key><string>"
522 << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
530 // Output the location of the bug.
531 o << " <key>location</key>\n";
532 EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2);
534 // Output the diagnostic to the sub-diagnostic client, if any.
535 if (!filesMade->empty()) {
537 PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
539 for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
540 CE = files->end(); CI != CE; ++CI) {
541 StringRef newName = CI->first;
542 if (newName != lastName) {
543 if (!lastName.empty()) {
547 o << " <key>" << lastName << "_files</key>\n";
550 o << " <string>" << CI->second << "</string>\n";
556 // Close up the entry.
563 o << "</dict>\n</plist>";