]> granicus.if.org Git - clang/blob - include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
Allow some BugReports to opt-out of PathDiagnostic callstack pruning until we have...
[clang] / include / clang / StaticAnalyzer / Core / BugReporter / BugReporter.h
1 //===---  BugReporter.h - Generate PathDiagnostics --------------*- 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 BugReporter, a utility class for generating
11 //  PathDiagnostics for analyses based on ProgramState.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_GR_BUGREPORTER
16 #define LLVM_CLANG_GR_BUGREPORTER
17
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/ilist.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/ADT/ImmutableSet.h"
26 #include "llvm/ADT/DenseSet.h"
27
28 namespace clang {
29
30 class ASTContext;
31 class DiagnosticsEngine;
32 class Stmt;
33 class ParentMap;
34
35 namespace ento {
36
37 class PathDiagnostic;
38 class ExplodedNode;
39 class ExplodedGraph;
40 class BugReport;
41 class BugReporter;
42 class BugReporterContext;
43 class ExprEngine;
44 class BugType;
45
46 //===----------------------------------------------------------------------===//
47 // Interface for individual bug reports.
48 //===----------------------------------------------------------------------===//
49
50 /// This class provides an interface through which checkers can create
51 /// individual bug reports.
52 class BugReport : public llvm::ilist_node<BugReport> {
53 public:  
54   class NodeResolver {
55     virtual void anchor();
56   public:
57     virtual ~NodeResolver() {}
58     virtual const ExplodedNode*
59             getOriginalNode(const ExplodedNode *N) = 0;
60   };
61
62   typedef const SourceRange *ranges_iterator;
63   typedef SmallVector<BugReporterVisitor *, 8> VisitorList;
64   typedef VisitorList::iterator visitor_iterator;
65   typedef SmallVector<StringRef, 2> ExtraTextList;
66
67 protected:
68   friend class BugReporter;
69   friend class BugReportEquivClass;
70
71   BugType& BT;
72   const Decl *DeclWithIssue;
73   std::string ShortDescription;
74   std::string Description;
75   PathDiagnosticLocation Location;
76   PathDiagnosticLocation UniqueingLocation;
77   const ExplodedNode *ErrorNode;
78   SmallVector<SourceRange, 4> Ranges;
79   ExtraTextList ExtraText;
80   
81   typedef llvm::DenseSet<SymbolRef> Symbols;
82   typedef llvm::DenseSet<const MemRegion *> Regions;
83
84   /// A set of symbols that are registered with this report as being
85   /// "interesting", and thus used to help decide which diagnostics
86   /// to include when constructing the final path diagnostic.
87   Symbols interestingSymbols;
88
89   /// A set of regions that are registered with this report as being
90   /// "interesting", and thus used to help decide which diagnostics
91   /// to include when constructing the final path diagnostic.
92   Regions interestingRegions;
93
94   /// A set of custom visitors which generate "event" diagnostics at
95   /// interesting points in the path.
96   VisitorList Callbacks;
97
98   /// Used for ensuring the visitors are only added once.
99   llvm::FoldingSet<BugReporterVisitor> CallbacksSet;
100
101   /// Used for clients to tell if the report's configuration has changed
102   /// since the last time they checked.
103   unsigned ConfigurationChangeToken;
104   
105   /// When set, this flag disables all callstack pruning from a diagnostic
106   /// path.  This is useful for some reports that want maximum fidelty
107   /// when reporting an issue.
108   bool DoNotPrunePath;
109
110 public:
111   BugReport(BugType& bt, StringRef desc, const ExplodedNode *errornode)
112     : BT(bt), DeclWithIssue(0), Description(desc), ErrorNode(errornode),
113       ConfigurationChangeToken(0), DoNotPrunePath(false) {}
114
115   BugReport(BugType& bt, StringRef shortDesc, StringRef desc,
116             const ExplodedNode *errornode)
117     : BT(bt), DeclWithIssue(0), ShortDescription(shortDesc), Description(desc),
118       ErrorNode(errornode), ConfigurationChangeToken(0),
119       DoNotPrunePath(false) {}
120
121   BugReport(BugType& bt, StringRef desc, PathDiagnosticLocation l)
122     : BT(bt), DeclWithIssue(0), Description(desc), Location(l), ErrorNode(0),
123       ConfigurationChangeToken(0),
124       DoNotPrunePath(false) {}
125
126   /// \brief Create a BugReport with a custom uniqueing location.
127   ///
128   /// The reports that have the same report location, description, bug type, and
129   /// ranges are uniqued - only one of the equivalent reports will be presented
130   /// to the user. This method allows to rest the location which should be used
131   /// for uniquing reports. For example, memory leaks checker, could set this to
132   /// the allocation site, rather then the location where the bug is reported.
133   BugReport(BugType& bt, StringRef desc, const ExplodedNode *errornode,
134             PathDiagnosticLocation LocationToUnique)
135     : BT(bt), DeclWithIssue(0), Description(desc),
136       UniqueingLocation(LocationToUnique),
137       ErrorNode(errornode), ConfigurationChangeToken(0) {}
138
139   virtual ~BugReport();
140
141   const BugType& getBugType() const { return BT; }
142   BugType& getBugType() { return BT; }
143
144   const ExplodedNode *getErrorNode() const { return ErrorNode; }
145
146   const StringRef getDescription() const { return Description; }
147
148   const StringRef getShortDescription() const {
149     return ShortDescription.empty() ? Description : ShortDescription;
150   }
151
152   /// Indicates whether or not any path pruning should take place
153   /// when generating a PathDiagnostic from this BugReport.
154   bool shouldPrunePath() const { return !DoNotPrunePath; }
155
156   /// Disable all path pruning when generating a PathDiagnostic.
157   void disablePathPruning() { DoNotPrunePath = true; }
158   
159   void markInteresting(SymbolRef sym);
160   void markInteresting(const MemRegion *R);
161   void markInteresting(SVal V);
162   
163   bool isInteresting(SymbolRef sym) const;
164   bool isInteresting(const MemRegion *R) const;
165   bool isInteresting(SVal V) const;
166
167   unsigned getConfigurationChangeToken() const {
168     return ConfigurationChangeToken;
169   }
170   
171   /// Return the canonical declaration, be it a method or class, where
172   /// this issue semantically occurred.
173   const Decl *getDeclWithIssue() const;
174   
175   /// Specifically set the Decl where an issue occurred.  This isn't necessary
176   /// for BugReports that cover a path as it will be automatically inferred.
177   void setDeclWithIssue(const Decl *declWithIssue) {
178     DeclWithIssue = declWithIssue;
179   }
180   
181   /// \brief This allows for addition of meta data to the diagnostic.
182   ///
183   /// Currently, only the HTMLDiagnosticClient knows how to display it. 
184   void addExtraText(StringRef S) {
185     ExtraText.push_back(S);
186   }
187
188   virtual const ExtraTextList &getExtraText() {
189     return ExtraText;
190   }
191
192   /// \brief Return the "definitive" location of the reported bug.
193   ///
194   ///  While a bug can span an entire path, usually there is a specific
195   ///  location that can be used to identify where the key issue occurred.
196   ///  This location is used by clients rendering diagnostics.
197   virtual PathDiagnosticLocation getLocation(const SourceManager &SM) const;
198
199   const Stmt *getStmt() const;
200
201   /// \brief Add a range to a bug report.
202   ///
203   /// Ranges are used to highlight regions of interest in the source code.
204   /// They should be at the same source code line as the BugReport location.
205   /// By default, the source range of the statement corresponding to the error
206   /// node will be used; add a single invalid range to specify absence of
207   /// ranges.
208   void addRange(SourceRange R) {
209     assert((R.isValid() || Ranges.empty()) && "Invalid range can only be used "
210                            "to specify that the report does not have a range.");
211     Ranges.push_back(R);
212   }
213
214   /// \brief Get the SourceRanges associated with the report.
215   virtual std::pair<ranges_iterator, ranges_iterator> getRanges();
216
217   /// \brief Add custom or predefined bug report visitors to this report.
218   ///
219   /// The visitors should be used when the default trace is not sufficient.
220   /// For example, they allow constructing a more elaborate trace.
221   /// \sa registerConditionVisitor(), registerTrackNullOrUndefValue(),
222   /// registerFindLastStore(), registerNilReceiverVisitor(), and
223   /// registerVarDeclsLastStore().
224   void addVisitor(BugReporterVisitor *visitor);
225
226         /// Iterators through the custom diagnostic visitors.
227   visitor_iterator visitor_begin() { return Callbacks.begin(); }
228   visitor_iterator visitor_end() { return Callbacks.end(); }
229
230   /// Profile to identify equivalent bug reports for error report coalescing.
231   /// Reports are uniqued to ensure that we do not emit multiple diagnostics
232   /// for each bug.
233   virtual void Profile(llvm::FoldingSetNodeID& hash) const;
234 };
235
236 } // end ento namespace
237 } // end clang namespace
238
239 namespace llvm {
240   template<> struct ilist_traits<clang::ento::BugReport>
241     : public ilist_default_traits<clang::ento::BugReport> {
242     clang::ento::BugReport *createSentinel() const {
243       return static_cast<clang::ento::BugReport *>(&Sentinel);
244     }
245     void destroySentinel(clang::ento::BugReport *) const {}
246
247     clang::ento::BugReport *provideInitialHead() const {
248       return createSentinel();
249     }
250     clang::ento::BugReport *ensureHead(clang::ento::BugReport *) const {
251       return createSentinel();
252     }
253   private:
254     mutable ilist_half_node<clang::ento::BugReport> Sentinel;
255   };
256 }
257
258 namespace clang {
259 namespace ento {
260
261 //===----------------------------------------------------------------------===//
262 // BugTypes (collections of related reports).
263 //===----------------------------------------------------------------------===//
264
265 class BugReportEquivClass : public llvm::FoldingSetNode {
266   /// List of *owned* BugReport objects.
267   llvm::ilist<BugReport> Reports;
268
269   friend class BugReporter;
270   void AddReport(BugReport* R) { Reports.push_back(R); }
271 public:
272   BugReportEquivClass(BugReport* R) { Reports.push_back(R); }
273   ~BugReportEquivClass();
274
275   void Profile(llvm::FoldingSetNodeID& ID) const {
276     assert(!Reports.empty());
277     Reports.front().Profile(ID);
278   }
279
280   typedef llvm::ilist<BugReport>::iterator iterator;
281   typedef llvm::ilist<BugReport>::const_iterator const_iterator;
282
283   iterator begin() { return Reports.begin(); }
284   iterator end() { return Reports.end(); }
285
286   const_iterator begin() const { return Reports.begin(); }
287   const_iterator end() const { return Reports.end(); }
288 };
289
290 //===----------------------------------------------------------------------===//
291 // BugReporter and friends.
292 //===----------------------------------------------------------------------===//
293
294 class BugReporterData {
295 public:
296   virtual ~BugReporterData();
297   virtual DiagnosticsEngine& getDiagnostic() = 0;
298   virtual PathDiagnosticConsumer* getPathDiagnosticConsumer() = 0;
299   virtual ASTContext &getASTContext() = 0;
300   virtual SourceManager& getSourceManager() = 0;
301 };
302
303 /// BugReporter is a utility class for generating PathDiagnostics for analysis.
304 /// It collects the BugReports and BugTypes and knows how to generate
305 /// and flush the corresponding diagnostics.
306 class BugReporter {
307 public:
308   enum Kind { BaseBRKind, GRBugReporterKind };
309
310 private:
311   typedef llvm::ImmutableSet<BugType*> BugTypesTy;
312   BugTypesTy::Factory F;
313   BugTypesTy BugTypes;
314
315   const Kind kind;
316   BugReporterData& D;
317
318   /// Generate and flush the diagnostics for the given bug report.
319   void FlushReport(BugReportEquivClass& EQ);
320
321   /// The set of bug reports tracked by the BugReporter.
322   llvm::FoldingSet<BugReportEquivClass> EQClasses;
323   /// A vector of BugReports for tracking the allocated pointers and cleanup.
324   std::vector<BugReportEquivClass *> EQClassesVector;
325
326 protected:
327   BugReporter(BugReporterData& d, Kind k) : BugTypes(F.getEmptySet()), kind(k),
328                                             D(d) {}
329
330 public:
331   BugReporter(BugReporterData& d) : BugTypes(F.getEmptySet()), kind(BaseBRKind),
332                                     D(d) {}
333   virtual ~BugReporter();
334
335   /// \brief Generate and flush diagnostics for all bug reports.
336   void FlushReports();
337
338   Kind getKind() const { return kind; }
339
340   DiagnosticsEngine& getDiagnostic() {
341     return D.getDiagnostic();
342   }
343
344   PathDiagnosticConsumer* getPathDiagnosticConsumer() {
345     return D.getPathDiagnosticConsumer();
346   }
347
348   /// \brief Iterator over the set of BugTypes tracked by the BugReporter.
349   typedef BugTypesTy::iterator iterator;
350   iterator begin() { return BugTypes.begin(); }
351   iterator end() { return BugTypes.end(); }
352
353   /// \brief Iterator over the set of BugReports tracked by the BugReporter.
354   typedef llvm::FoldingSet<BugReportEquivClass>::iterator EQClasses_iterator;
355   EQClasses_iterator EQClasses_begin() { return EQClasses.begin(); }
356   EQClasses_iterator EQClasses_end() { return EQClasses.end(); }
357
358   ASTContext &getContext() { return D.getASTContext(); }
359
360   SourceManager& getSourceManager() { return D.getSourceManager(); }
361
362   virtual void GeneratePathDiagnostic(PathDiagnostic& pathDiagnostic,
363         SmallVectorImpl<BugReport *> &bugReports) {}
364
365   void Register(BugType *BT);
366
367   /// \brief Add the given report to the set of reports tracked by BugReporter.
368   ///
369   /// The reports are usually generated by the checkers. Further, they are
370   /// folded based on the profile value, which is done to coalesce similar
371   /// reports.
372   void EmitReport(BugReport *R);
373
374   void EmitBasicReport(const Decl *DeclWithIssue,
375                        StringRef BugName, StringRef BugCategory,
376                        StringRef BugStr, PathDiagnosticLocation Loc,
377                        SourceRange* RangeBeg, unsigned NumRanges);
378
379   void EmitBasicReport(const Decl *DeclWithIssue,
380                        StringRef BugName, StringRef BugCategory,
381                        StringRef BugStr, PathDiagnosticLocation Loc) {
382     EmitBasicReport(DeclWithIssue, BugName, BugCategory, BugStr, Loc, 0, 0);
383   }
384
385   void EmitBasicReport(const Decl *DeclWithIssue,
386                        StringRef BugName, StringRef Category,
387                        StringRef BugStr, PathDiagnosticLocation Loc,
388                        SourceRange R) {
389     EmitBasicReport(DeclWithIssue, BugName, Category, BugStr, Loc, &R, 1);
390   }
391
392   static bool classof(const BugReporter* R) { return true; }
393
394 private:
395   llvm::StringMap<BugType *> StrBugTypes;
396
397   /// \brief Returns a BugType that is associated with the given name and
398   /// category.
399   BugType *getBugTypeForName(StringRef name, StringRef category);
400 };
401
402 // FIXME: Get rid of GRBugReporter.  It's the wrong abstraction.
403 class GRBugReporter : public BugReporter {
404   ExprEngine& Eng;
405 public:
406   GRBugReporter(BugReporterData& d, ExprEngine& eng)
407     : BugReporter(d, GRBugReporterKind), Eng(eng) {}
408
409   virtual ~GRBugReporter();
410
411   /// getEngine - Return the analysis engine used to analyze a given
412   ///  function or method.
413   ExprEngine &getEngine() { return Eng; }
414
415   /// getGraph - Get the exploded graph created by the analysis engine
416   ///  for the analyzed method or function.
417   ExplodedGraph &getGraph();
418
419   /// getStateManager - Return the state manager used by the analysis
420   ///  engine.
421   ProgramStateManager &getStateManager();
422
423   virtual void GeneratePathDiagnostic(PathDiagnostic &pathDiagnostic,
424                      SmallVectorImpl<BugReport*> &bugReports);
425
426   /// classof - Used by isa<>, cast<>, and dyn_cast<>.
427   static bool classof(const BugReporter* R) {
428     return R->getKind() == GRBugReporterKind;
429   }
430 };
431
432 class BugReporterContext {
433   virtual void anchor();
434   GRBugReporter &BR;
435 public:
436   BugReporterContext(GRBugReporter& br) : BR(br) {}
437
438   virtual ~BugReporterContext() {}
439
440   GRBugReporter& getBugReporter() { return BR; }
441
442   ExplodedGraph &getGraph() { return BR.getGraph(); }
443
444   ProgramStateManager& getStateManager() {
445     return BR.getStateManager();
446   }
447
448   SValBuilder& getSValBuilder() {
449     return getStateManager().getSValBuilder();
450   }
451
452   ASTContext &getASTContext() {
453     return BR.getContext();
454   }
455
456   SourceManager& getSourceManager() {
457     return BR.getSourceManager();
458   }
459
460   virtual BugReport::NodeResolver& getNodeResolver() = 0;
461 };
462
463 } // end GR namespace
464
465 } // end clang namespace
466
467 #endif