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