1 //===--- 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
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;
24 using namespace ento;
25
26 typedef llvm::DenseMap<FileID, unsigned> FIDMap;
27
28
29 namespace {
30 class PlistDiagnostics : public PathDiagnosticConsumer {
31 const std::string OutputFile;
32 const LangOptions &LangOpts;
33 const bool SupportsCrossFileDiagnostics;
34 public:
35 PlistDiagnostics(const std::string& prefix, const LangOptions &LangOpts,
36 bool supportsMultipleFiles);
37
~PlistDiagnostics()38 virtual ~PlistDiagnostics() {}
39
40 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
41 FilesMade *filesMade);
42
getName() const43 virtual StringRef getName() const {
44 return "PlistDiagnostics";
45 }
46
getGenerationScheme() const47 PathGenerationScheme getGenerationScheme() const { return Extensive; }
supportsLogicalOpControlFlow() const48 bool supportsLogicalOpControlFlow() const { return true; }
supportsAllBlockEdges() const49 bool supportsAllBlockEdges() const { return true; }
supportsCrossFileDiagnostics() const50 virtual bool supportsCrossFileDiagnostics() const {
51 return SupportsCrossFileDiagnostics;
52 }
53 };
54 } // end anonymous namespace
55
PlistDiagnostics(const std::string & output,const LangOptions & LO,bool supportsMultipleFiles)56 PlistDiagnostics::PlistDiagnostics(const std::string& output,
57 const LangOptions &LO,
58 bool supportsMultipleFiles)
59 : OutputFile(output), LangOpts(LO),
60 SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
61
createPlistDiagnosticConsumer(PathDiagnosticConsumers & C,const std::string & s,const Preprocessor & PP)62 void ento::createPlistDiagnosticConsumer(PathDiagnosticConsumers &C,
63 const std::string& s,
64 const Preprocessor &PP) {
65 C.push_back(new PlistDiagnostics(s, PP.getLangOpts(), false));
66 }
67
createPlistMultiFileDiagnosticConsumer(PathDiagnosticConsumers & C,const std::string & s,const Preprocessor & PP)68 void ento::createPlistMultiFileDiagnosticConsumer(PathDiagnosticConsumers &C,
69 const std::string &s,
70 const Preprocessor &PP) {
71 C.push_back(new PlistDiagnostics(s, PP.getLangOpts(), true));
72 }
73
AddFID(FIDMap & FIDs,SmallVectorImpl<FileID> & V,const SourceManager * SM,SourceLocation L)74 static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
75 const SourceManager* SM, SourceLocation L) {
76
77 FileID FID = SM->getFileID(SM->getExpansionLoc(L));
78 FIDMap::iterator I = FIDs.find(FID);
79 if (I != FIDs.end()) return;
80 FIDs[FID] = V.size();
81 V.push_back(FID);
82 }
83
GetFID(const FIDMap & FIDs,const SourceManager & SM,SourceLocation L)84 static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM,
85 SourceLocation L) {
86 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
87 FIDMap::const_iterator I = FIDs.find(FID);
88 assert(I != FIDs.end());
89 return I->second;
90 }
91
Indent(raw_ostream & o,const unsigned indent)92 static raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
93 for (unsigned i = 0; i < indent; ++i) o << ' ';
94 return o;
95 }
96
EmitLocation(raw_ostream & o,const SourceManager & SM,const LangOptions & LangOpts,SourceLocation L,const FIDMap & FM,unsigned indent,bool extend=false)97 static void EmitLocation(raw_ostream &o, const SourceManager &SM,
98 const LangOptions &LangOpts,
99 SourceLocation L, const FIDMap &FM,
100 unsigned indent, bool extend = false) {
101
102 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM));
103
104 // Add in the length of the token, so that we cover multi-char tokens.
105 unsigned offset =
106 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
107
108 Indent(o, indent) << "<dict>\n";
109 Indent(o, indent) << " <key>line</key><integer>"
110 << Loc.getExpansionLineNumber() << "</integer>\n";
111 Indent(o, indent) << " <key>col</key><integer>"
112 << Loc.getExpansionColumnNumber() + offset << "</integer>\n";
113 Indent(o, indent) << " <key>file</key><integer>"
114 << GetFID(FM, SM, Loc) << "</integer>\n";
115 Indent(o, indent) << "</dict>\n";
116 }
117
EmitLocation(raw_ostream & o,const SourceManager & SM,const LangOptions & LangOpts,const PathDiagnosticLocation & L,const FIDMap & FM,unsigned indent,bool extend=false)118 static void EmitLocation(raw_ostream &o, const SourceManager &SM,
119 const LangOptions &LangOpts,
120 const PathDiagnosticLocation &L, const FIDMap& FM,
121 unsigned indent, bool extend = false) {
122 EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend);
123 }
124
EmitRange(raw_ostream & o,const SourceManager & SM,const LangOptions & LangOpts,PathDiagnosticRange R,const FIDMap & FM,unsigned indent)125 static void EmitRange(raw_ostream &o, const SourceManager &SM,
126 const LangOptions &LangOpts,
127 PathDiagnosticRange R, const FIDMap &FM,
128 unsigned indent) {
129 Indent(o, indent) << "<array>\n";
130 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1);
131 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint);
132 Indent(o, indent) << "</array>\n";
133 }
134
EmitString(raw_ostream & o,StringRef s)135 static raw_ostream &EmitString(raw_ostream &o, StringRef s) {
136 o << "<string>";
137 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
138 char c = *I;
139 switch (c) {
140 default: o << c; break;
141 case '&': o << "&"; break;
142 case '<': o << "<"; break;
143 case '>': o << ">"; break;
144 case '\'': o << "'"; break;
145 case '\"': o << """; break;
146 }
147 }
148 o << "</string>";
149 return o;
150 }
151
ReportControlFlow(raw_ostream & o,const PathDiagnosticControlFlowPiece & P,const FIDMap & FM,const SourceManager & SM,const LangOptions & LangOpts,unsigned indent)152 static void ReportControlFlow(raw_ostream &o,
153 const PathDiagnosticControlFlowPiece& P,
154 const FIDMap& FM,
155 const SourceManager &SM,
156 const LangOptions &LangOpts,
157 unsigned indent) {
158
159 Indent(o, indent) << "<dict>\n";
160 ++indent;
161
162 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
163
164 // Emit edges.
165 Indent(o, indent) << "<key>edges</key>\n";
166 ++indent;
167 Indent(o, indent) << "<array>\n";
168 ++indent;
169 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
170 I!=E; ++I) {
171 Indent(o, indent) << "<dict>\n";
172 ++indent;
173
174 // Make the ranges of the start and end point self-consistent with adjacent edges
175 // by forcing to use only the beginning of the range. This simplifies the layout
176 // logic for clients.
177 Indent(o, indent) << "<key>start</key>\n";
178 SourceLocation StartEdge = I->getStart().asRange().getBegin();
179 EmitRange(o, SM, LangOpts, SourceRange(StartEdge, StartEdge), FM, indent+1);
180
181 Indent(o, indent) << "<key>end</key>\n";
182 SourceLocation EndEdge = I->getEnd().asRange().getBegin();
183 EmitRange(o, SM, LangOpts, SourceRange(EndEdge, EndEdge), FM, indent+1);
184
185 --indent;
186 Indent(o, indent) << "</dict>\n";
187 }
188 --indent;
189 Indent(o, indent) << "</array>\n";
190 --indent;
191
192 // Output any helper text.
193 const std::string& s = P.getString();
194 if (!s.empty()) {
195 Indent(o, indent) << "<key>alternate</key>";
196 EmitString(o, s) << '\n';
197 }
198
199 --indent;
200 Indent(o, indent) << "</dict>\n";
201 }
202
ReportEvent(raw_ostream & o,const PathDiagnosticPiece & P,const FIDMap & FM,const SourceManager & SM,const LangOptions & LangOpts,unsigned indent,unsigned depth)203 static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P,
204 const FIDMap& FM,
205 const SourceManager &SM,
206 const LangOptions &LangOpts,
207 unsigned indent,
208 unsigned depth) {
209
210 Indent(o, indent) << "<dict>\n";
211 ++indent;
212
213 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
214
215 // Output the location.
216 FullSourceLoc L = P.getLocation().asLocation();
217
218 Indent(o, indent) << "<key>location</key>\n";
219 EmitLocation(o, SM, LangOpts, L, FM, indent);
220
221 // Output the ranges (if any).
222 ArrayRef<SourceRange> Ranges = P.getRanges();
223
224 if (!Ranges.empty()) {
225 Indent(o, indent) << "<key>ranges</key>\n";
226 Indent(o, indent) << "<array>\n";
227 ++indent;
228 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
229 I != E; ++I) {
230 EmitRange(o, SM, LangOpts, *I, FM, indent+1);
231 }
232 --indent;
233 Indent(o, indent) << "</array>\n";
234 }
235
236 // Output the call depth.
237 Indent(o, indent) << "<key>depth</key>"
238 << "<integer>" << depth << "</integer>\n";
239
240 // Output the text.
241 assert(!P.getString().empty());
242 Indent(o, indent) << "<key>extended_message</key>\n";
243 Indent(o, indent);
244 EmitString(o, P.getString()) << '\n';
245
246 // Output the short text.
247 // FIXME: Really use a short string.
248 Indent(o, indent) << "<key>message</key>\n";
249 Indent(o, indent);
250 EmitString(o, P.getString()) << '\n';
251
252 // Finish up.
253 --indent;
254 Indent(o, indent); o << "</dict>\n";
255 }
256
257 static void ReportPiece(raw_ostream &o,
258 const PathDiagnosticPiece &P,
259 const FIDMap& FM, const SourceManager &SM,
260 const LangOptions &LangOpts,
261 unsigned indent,
262 unsigned depth,
263 bool includeControlFlow);
264
ReportCall(raw_ostream & o,const PathDiagnosticCallPiece & P,const FIDMap & FM,const SourceManager & SM,const LangOptions & LangOpts,unsigned indent,unsigned depth)265 static void ReportCall(raw_ostream &o,
266 const PathDiagnosticCallPiece &P,
267 const FIDMap& FM, const SourceManager &SM,
268 const LangOptions &LangOpts,
269 unsigned indent,
270 unsigned depth) {
271
272 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter =
273 P.getCallEnterEvent();
274
275 if (callEnter)
276 ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, depth, true);
277
278 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnterWithinCaller =
279 P.getCallEnterWithinCallerEvent();
280
281 ++depth;
282
283 if (callEnterWithinCaller)
284 ReportPiece(o, *callEnterWithinCaller, FM, SM, LangOpts,
285 indent, depth, true);
286
287 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
288 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, true);
289
290 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit =
291 P.getCallExitEvent();
292
293 if (callExit)
294 ReportPiece(o, *callExit, FM, SM, LangOpts, indent, depth, true);
295 }
296
ReportMacro(raw_ostream & o,const PathDiagnosticMacroPiece & P,const FIDMap & FM,const SourceManager & SM,const LangOptions & LangOpts,unsigned indent,unsigned depth)297 static void ReportMacro(raw_ostream &o,
298 const PathDiagnosticMacroPiece& P,
299 const FIDMap& FM, const SourceManager &SM,
300 const LangOptions &LangOpts,
301 unsigned indent,
302 unsigned depth) {
303
304 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
305 I!=E; ++I) {
306 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, false);
307 }
308 }
309
ReportDiag(raw_ostream & o,const PathDiagnosticPiece & P,const FIDMap & FM,const SourceManager & SM,const LangOptions & LangOpts)310 static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P,
311 const FIDMap& FM, const SourceManager &SM,
312 const LangOptions &LangOpts) {
313 ReportPiece(o, P, FM, SM, LangOpts, 4, 0, true);
314 }
315
ReportPiece(raw_ostream & o,const PathDiagnosticPiece & P,const FIDMap & FM,const SourceManager & SM,const LangOptions & LangOpts,unsigned indent,unsigned depth,bool includeControlFlow)316 static void ReportPiece(raw_ostream &o,
317 const PathDiagnosticPiece &P,
318 const FIDMap& FM, const SourceManager &SM,
319 const LangOptions &LangOpts,
320 unsigned indent,
321 unsigned depth,
322 bool includeControlFlow) {
323 switch (P.getKind()) {
324 case PathDiagnosticPiece::ControlFlow:
325 if (includeControlFlow)
326 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
327 LangOpts, indent);
328 break;
329 case PathDiagnosticPiece::Call:
330 ReportCall(o, cast<PathDiagnosticCallPiece>(P), FM, SM, LangOpts,
331 indent, depth);
332 break;
333 case PathDiagnosticPiece::Event:
334 ReportEvent(o, cast<PathDiagnosticSpotPiece>(P), FM, SM, LangOpts,
335 indent, depth);
336 break;
337 case PathDiagnosticPiece::Macro:
338 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts,
339 indent, depth);
340 break;
341 }
342 }
343
FlushDiagnosticsImpl(std::vector<const PathDiagnostic * > & Diags,FilesMade * filesMade)344 void PlistDiagnostics::FlushDiagnosticsImpl(
345 std::vector<const PathDiagnostic *> &Diags,
346 FilesMade *filesMade) {
347 // Build up a set of FIDs that we use by scanning the locations and
348 // ranges of the diagnostics.
349 FIDMap FM;
350 SmallVector<FileID, 10> Fids;
351 const SourceManager* SM = 0;
352
353 if (!Diags.empty())
354 SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager();
355
356
357 for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(),
358 DE = Diags.end(); DI != DE; ++DI) {
359
360 const PathDiagnostic *D = *DI;
361
362 llvm::SmallVector<const PathPieces *, 5> WorkList;
363 WorkList.push_back(&D->path);
364
365 while (!WorkList.empty()) {
366 const PathPieces &path = *WorkList.back();
367 WorkList.pop_back();
368
369 for (PathPieces::const_iterator I = path.begin(), E = path.end();
370 I!=E; ++I) {
371 const PathDiagnosticPiece *piece = I->getPtr();
372 AddFID(FM, Fids, SM, piece->getLocation().asLocation());
373 ArrayRef<SourceRange> Ranges = piece->getRanges();
374 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
375 E = Ranges.end(); I != E; ++I) {
376 AddFID(FM, Fids, SM, I->getBegin());
377 AddFID(FM, Fids, SM, I->getEnd());
378 }
379
380 if (const PathDiagnosticCallPiece *call =
381 dyn_cast<PathDiagnosticCallPiece>(piece)) {
382 IntrusiveRefCntPtr<PathDiagnosticEventPiece>
383 callEnterWithin = call->getCallEnterWithinCallerEvent();
384 if (callEnterWithin)
385 AddFID(FM, Fids, SM, callEnterWithin->getLocation().asLocation());
386
387 WorkList.push_back(&call->path);
388 }
389 else if (const PathDiagnosticMacroPiece *macro =
390 dyn_cast<PathDiagnosticMacroPiece>(piece)) {
391 WorkList.push_back(¯o->subPieces);
392 }
393 }
394 }
395 }
396
397 // Open the file.
398 std::string ErrMsg;
399 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg);
400 if (!ErrMsg.empty()) {
401 llvm::errs() << "warning: could not create file: " << OutputFile << '\n';
402 return;
403 }
404
405 // Write the plist header.
406 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
407 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
408 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
409 "<plist version=\"1.0\">\n";
410
411 // Write the root object: a <dict> containing...
412 // - "files", an <array> mapping from FIDs to file names
413 // - "diagnostics", an <array> containing the path diagnostics
414 o << "<dict>\n"
415 " <key>files</key>\n"
416 " <array>\n";
417
418 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
419 I!=E; ++I) {
420 o << " ";
421 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n';
422 }
423
424 o << " </array>\n"
425 " <key>diagnostics</key>\n"
426 " <array>\n";
427
428 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
429 DE = Diags.end(); DI!=DE; ++DI) {
430
431 o << " <dict>\n"
432 " <key>path</key>\n";
433
434 const PathDiagnostic *D = *DI;
435
436 o << " <array>\n";
437
438 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end();
439 I != E; ++I)
440 ReportDiag(o, **I, FM, *SM, LangOpts);
441
442 o << " </array>\n";
443
444 // Output the bug type and bug category.
445 o << " <key>description</key>";
446 EmitString(o, D->getShortDescription()) << '\n';
447 o << " <key>category</key>";
448 EmitString(o, D->getCategory()) << '\n';
449 o << " <key>type</key>";
450 EmitString(o, D->getBugType()) << '\n';
451
452 // Output information about the semantic context where
453 // the issue occurred.
454 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
455 // FIXME: handle blocks, which have no name.
456 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
457 StringRef declKind;
458 switch (ND->getKind()) {
459 case Decl::CXXRecord:
460 declKind = "C++ class";
461 break;
462 case Decl::CXXMethod:
463 declKind = "C++ method";
464 break;
465 case Decl::ObjCMethod:
466 declKind = "Objective-C method";
467 break;
468 case Decl::Function:
469 declKind = "function";
470 break;
471 default:
472 break;
473 }
474 if (!declKind.empty()) {
475 const std::string &declName = ND->getDeclName().getAsString();
476 o << " <key>issue_context_kind</key>";
477 EmitString(o, declKind) << '\n';
478 o << " <key>issue_context</key>";
479 EmitString(o, declName) << '\n';
480 }
481
482 // Output the bug hash for issue unique-ing. Currently, it's just an
483 // offset from the beginning of the function.
484 if (const Stmt *Body = DeclWithIssue->getBody()) {
485 FullSourceLoc Loc(SM->getExpansionLoc(D->getLocation().asLocation()),
486 *SM);
487 FullSourceLoc FunLoc(SM->getExpansionLoc(Body->getLocStart()), *SM);
488 o << " <key>issue_hash</key><integer>"
489 << Loc.getExpansionLineNumber() - FunLoc.getExpansionLineNumber()
490 << "</integer>\n";
491 }
492 }
493 }
494
495 // Output the location of the bug.
496 o << " <key>location</key>\n";
497 EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2);
498
499 // Output the diagnostic to the sub-diagnostic client, if any.
500 if (!filesMade->empty()) {
501 StringRef lastName;
502 PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
503 if (files) {
504 for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
505 CE = files->end(); CI != CE; ++CI) {
506 StringRef newName = CI->first;
507 if (newName != lastName) {
508 if (!lastName.empty()) {
509 o << " </array>\n";
510 }
511 lastName = newName;
512 o << " <key>" << lastName << "_files</key>\n";
513 o << " <array>\n";
514 }
515 o << " <string>" << CI->second << "</string>\n";
516 }
517 o << " </array>\n";
518 }
519 }
520
521 // Close up the entry.
522 o << " </dict>\n";
523 }
524
525 o << " </array>\n";
526
527 // Finish.
528 o << "</dict>\n</plist>";
529 }
530