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