]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Analysis/PathSensitive/BugReporter.h
Update clang to r84119.
[FreeBSD/FreeBSD.git] / include / clang / Analysis / PathSensitive / 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 GRState.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_ANALYSIS_BUGREPORTER
16 #define LLVM_CLANG_ANALYSIS_BUGREPORTER
17
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Analysis/PathSensitive/GRState.h"
21 #include "clang/Analysis/PathSensitive/ExplodedGraph.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/ImmutableSet.h"
27 #include <list>
28
29 namespace clang {
30
31 class PathDiagnostic;
32 class PathDiagnosticPiece;
33 class PathDiagnosticClient;
34 class ASTContext;
35 class Diagnostic;
36 class BugReporter;
37 class BugReporterContext;
38 class GRExprEngine;
39 class GRState;
40 class Stmt;
41 class BugType;
42 class ParentMap;
43
44 //===----------------------------------------------------------------------===//
45 // Interface for individual bug reports.
46 //===----------------------------------------------------------------------===//
47
48 class BugReporterVisitor {
49 public:
50   virtual ~BugReporterVisitor();
51   virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
52                                          const ExplodedNode* PrevN,
53                                          BugReporterContext& BRC) = 0;
54
55   virtual bool isOwnedByReporterContext() { return true; }
56 };
57
58 // FIXME: Combine this with RangedBugReport and remove RangedBugReport.
59 class BugReport : public BugReporterVisitor {
60 protected:
61   BugType& BT;
62   std::string ShortDescription;
63   std::string Description;
64   const ExplodedNode *EndNode;
65   SourceRange R;
66
67 protected:
68   friend class BugReporter;
69   friend class BugReportEquivClass;
70
71   virtual void Profile(llvm::FoldingSetNodeID& hash) const {
72     hash.AddInteger(getLocation().getRawEncoding());
73   }
74
75 public:
76   class NodeResolver {
77   public:
78     virtual ~NodeResolver() {}
79     virtual const ExplodedNode*
80             getOriginalNode(const ExplodedNode* N) = 0;
81   };
82
83   BugReport(BugType& bt, const char* desc, const ExplodedNode *n)
84     : BT(bt), Description(desc), EndNode(n) {}
85
86   BugReport(BugType& bt, const char* shortDesc, const char* desc,
87             const ExplodedNode *n)
88   : BT(bt), ShortDescription(shortDesc), Description(desc), EndNode(n) {}
89
90   virtual ~BugReport();
91
92   virtual bool isOwnedByReporterContext() { return false; }
93
94   const BugType& getBugType() const { return BT; }
95   BugType& getBugType() { return BT; }
96
97   // FIXME: Perhaps this should be moved into a subclass?
98   const ExplodedNode* getEndNode() const { return EndNode; }
99
100   // FIXME: Do we need this?  Maybe getLocation() should return a ProgramPoint
101   // object.
102   // FIXME: If we do need it, we can probably just make it private to
103   // BugReporter.
104   const Stmt* getStmt() const;
105
106   const std::string& getDescription() const { return Description; }
107
108   const std::string& getShortDescription() const {
109     return ShortDescription.empty() ? Description : ShortDescription;
110   }
111
112   // FIXME: Is this needed?
113   virtual std::pair<const char**,const char**> getExtraDescriptiveText() {
114     return std::make_pair((const char**)0,(const char**)0);
115   }
116
117   // FIXME: Perhaps move this into a subclass.
118   virtual PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
119                                           const ExplodedNode* N);
120
121   /// getLocation - Return the "definitive" location of the reported bug.
122   ///  While a bug can span an entire path, usually there is a specific
123   ///  location that can be used to identify where the key issue occured.
124   ///  This location is used by clients rendering diagnostics.
125   virtual SourceLocation getLocation() const;
126
127   /// getRanges - Returns the source ranges associated with this bug.
128   virtual void getRanges(const SourceRange*& beg, const SourceRange*& end);
129
130   virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
131                                          const ExplodedNode* PrevN,
132                                          BugReporterContext& BR);
133
134   virtual void registerInitialVisitors(BugReporterContext& BRC,
135                                        const ExplodedNode* N) {}
136 };
137
138 //===----------------------------------------------------------------------===//
139 // BugTypes (collections of related reports).
140 //===----------------------------------------------------------------------===//
141
142 class BugReportEquivClass : public llvm::FoldingSetNode {
143   // List of *owned* BugReport objects.
144   std::list<BugReport*> Reports;
145
146   friend class BugReporter;
147   void AddReport(BugReport* R) { Reports.push_back(R); }
148 public:
149   BugReportEquivClass(BugReport* R) { Reports.push_back(R); }
150   ~BugReportEquivClass();
151
152   void Profile(llvm::FoldingSetNodeID& ID) const {
153     assert(!Reports.empty());
154     (*Reports.begin())->Profile(ID);
155   }
156
157   class iterator {
158     std::list<BugReport*>::iterator impl;
159   public:
160     iterator(std::list<BugReport*>::iterator i) : impl(i) {}
161     iterator& operator++() { ++impl; return *this; }
162     bool operator==(const iterator& I) const { return I.impl == impl; }
163     bool operator!=(const iterator& I) const { return I.impl != impl; }
164     BugReport* operator*() const { return *impl; }
165     BugReport* operator->() const { return *impl; }
166   };
167
168   class const_iterator {
169     std::list<BugReport*>::const_iterator impl;
170   public:
171     const_iterator(std::list<BugReport*>::const_iterator i) : impl(i) {}
172     const_iterator& operator++() { ++impl; return *this; }
173     bool operator==(const const_iterator& I) const { return I.impl == impl; }
174     bool operator!=(const const_iterator& I) const { return I.impl != impl; }
175     const BugReport* operator*() const { return *impl; }
176     const BugReport* operator->() const { return *impl; }
177   };
178
179   iterator begin() { return iterator(Reports.begin()); }
180   iterator end() { return iterator(Reports.end()); }
181
182   const_iterator begin() const { return const_iterator(Reports.begin()); }
183   const_iterator end() const { return const_iterator(Reports.end()); }
184 };
185
186 class BugType {
187 private:
188   const std::string Name;
189   const std::string Category;
190   llvm::FoldingSet<BugReportEquivClass> EQClasses;
191   friend class BugReporter;
192   bool SuppressonSink;
193 public:
194   BugType(const char *name, const char* cat)
195     : Name(name), Category(cat), SuppressonSink(false) {}
196   virtual ~BugType();
197
198   // FIXME: Should these be made strings as well?
199   const std::string& getName() const { return Name; }
200   const std::string& getCategory() const { return Category; }
201   
202   /// isSuppressOnSink - Returns true if bug reports associated with this bug
203   ///  type should be suppressed if the end node of the report is post-dominated
204   ///  by a sink node.
205   bool isSuppressOnSink() const { return SuppressonSink; }
206   void setSuppressOnSink(bool x) { SuppressonSink = x; }
207
208   virtual void FlushReports(BugReporter& BR);
209
210   typedef llvm::FoldingSet<BugReportEquivClass>::iterator iterator;
211   iterator begin() { return EQClasses.begin(); }
212   iterator end() { return EQClasses.end(); }
213
214   typedef llvm::FoldingSet<BugReportEquivClass>::const_iterator const_iterator;
215   const_iterator begin() const { return EQClasses.begin(); }
216   const_iterator end() const { return EQClasses.end(); }
217 };
218
219 //===----------------------------------------------------------------------===//
220 // Specialized subclasses of BugReport.
221 //===----------------------------------------------------------------------===//
222
223 // FIXME: Collapse this with the default BugReport class.
224 class RangedBugReport : public BugReport {
225   std::vector<SourceRange> Ranges;
226 public:
227   RangedBugReport(BugType& D, const char* description, ExplodedNode *n)
228     : BugReport(D, description, n) {}
229
230   RangedBugReport(BugType& D, const char *shortDescription,
231                   const char *description, ExplodedNode *n)
232   : BugReport(D, shortDescription, description, n) {}
233
234   ~RangedBugReport();
235
236   // FIXME: Move this out of line.
237   void addRange(SourceRange R) { Ranges.push_back(R); }
238
239   // FIXME: Move this out of line.
240   void getRanges(const SourceRange*& beg, const SourceRange*& end) {
241
242     if (Ranges.empty()) {
243       beg = NULL;
244       end = NULL;
245     }
246     else {
247       beg = &Ranges[0];
248       end = beg + Ranges.size();
249     }
250   }
251 };
252
253 class EnhancedBugReport : public RangedBugReport {
254 public:
255   typedef void (*VisitorCreator)(BugReporterContext &BRcC, const void *data,
256                                  const ExplodedNode *N);
257
258 private:
259   typedef std::vector<std::pair<VisitorCreator, const void*> > Creators;
260   Creators creators;
261
262 public:
263   EnhancedBugReport(BugType& D, const char* description, ExplodedNode *n)
264    : RangedBugReport(D, description, n) {}
265
266   EnhancedBugReport(BugType& D, const char *shortDescription,
267                   const char *description, ExplodedNode *n)
268     : RangedBugReport(D, shortDescription, description, n) {}
269
270   ~EnhancedBugReport() {}
271
272   void registerInitialVisitors(BugReporterContext& BRC, const ExplodedNode* N) {
273     for (Creators::iterator I = creators.begin(), E = creators.end(); I!=E; ++I)
274       I->first(BRC, I->second, N);
275   }
276
277   void addVisitorCreator(VisitorCreator creator, const void *data) {
278     creators.push_back(std::make_pair(creator, data));
279   }
280 };
281
282 //===----------------------------------------------------------------------===//
283 // BugReporter and friends.
284 //===----------------------------------------------------------------------===//
285
286 class BugReporterData {
287 public:
288   virtual ~BugReporterData();
289   virtual Diagnostic& getDiagnostic() = 0;
290   virtual PathDiagnosticClient* getPathDiagnosticClient() = 0;
291   virtual ASTContext& getASTContext() = 0;
292   virtual SourceManager& getSourceManager() = 0;
293 };
294
295 class BugReporter {
296 public:
297   enum Kind { BaseBRKind, GRBugReporterKind };
298
299 private:
300   typedef llvm::ImmutableSet<BugType*> BugTypesTy;
301   BugTypesTy::Factory F;
302   BugTypesTy BugTypes;
303
304   const Kind kind;
305   BugReporterData& D;
306
307   void FlushReport(BugReportEquivClass& EQ);
308
309 protected:
310   BugReporter(BugReporterData& d, Kind k) : BugTypes(F.GetEmptySet()), kind(k), D(d) {}
311
312 public:
313   BugReporter(BugReporterData& d) : BugTypes(F.GetEmptySet()), kind(BaseBRKind), D(d) {}
314   virtual ~BugReporter();
315
316   void FlushReports();
317
318   Kind getKind() const { return kind; }
319
320   Diagnostic& getDiagnostic() {
321     return D.getDiagnostic();
322   }
323
324   PathDiagnosticClient* getPathDiagnosticClient() {
325     return D.getPathDiagnosticClient();
326   }
327
328   typedef BugTypesTy::iterator iterator;
329   iterator begin() { return BugTypes.begin(); }
330   iterator end() { return BugTypes.end(); }
331
332   ASTContext& getContext() { return D.getASTContext(); }
333
334   SourceManager& getSourceManager() { return D.getSourceManager(); }
335
336   virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
337                                       BugReportEquivClass& EQ) {}
338
339   void Register(BugType *BT);
340
341   void EmitReport(BugReport *R);
342
343   void EmitBasicReport(const char* BugName, const char* BugStr,
344                        SourceLocation Loc,
345                        SourceRange* RangeBeg, unsigned NumRanges);
346
347   void EmitBasicReport(const char* BugName, const char* BugCategory,
348                        const char* BugStr, SourceLocation Loc,
349                        SourceRange* RangeBeg, unsigned NumRanges);
350
351
352   void EmitBasicReport(const char* BugName, const char* BugStr,
353                        SourceLocation Loc) {
354     EmitBasicReport(BugName, BugStr, Loc, 0, 0);
355   }
356
357   void EmitBasicReport(const char* BugName, const char* BugCategory,
358                        const char* BugStr, SourceLocation Loc) {
359     EmitBasicReport(BugName, BugCategory, BugStr, Loc, 0, 0);
360   }
361
362   void EmitBasicReport(const char* BugName, const char* BugStr,
363                        SourceLocation Loc, SourceRange R) {
364     EmitBasicReport(BugName, BugStr, Loc, &R, 1);
365   }
366
367   void EmitBasicReport(const char* BugName, const char* Category,
368                        const char* BugStr, SourceLocation Loc, SourceRange R) {
369     EmitBasicReport(BugName, Category, BugStr, Loc, &R, 1);
370   }
371
372   static bool classof(const BugReporter* R) { return true; }
373 };
374
375 // FIXME: Get rid of GRBugReporter.  It's the wrong abstraction.
376 class GRBugReporter : public BugReporter {
377   GRExprEngine& Eng;
378   llvm::SmallSet<SymbolRef, 10> NotableSymbols;
379 public:
380   GRBugReporter(BugReporterData& d, GRExprEngine& eng)
381     : BugReporter(d, GRBugReporterKind), Eng(eng) {}
382
383   virtual ~GRBugReporter();
384
385   /// getEngine - Return the analysis engine used to analyze a given
386   ///  function or method.
387   GRExprEngine &getEngine() { return Eng; }
388
389   /// getGraph - Get the exploded graph created by the analysis engine
390   ///  for the analyzed method or function.
391   ExplodedGraph &getGraph();
392
393   /// getStateManager - Return the state manager used by the analysis
394   ///  engine.
395   GRStateManager &getStateManager();
396
397   virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
398                                       BugReportEquivClass& R);
399
400   void addNotableSymbol(SymbolRef Sym) {
401     NotableSymbols.insert(Sym);
402   }
403
404   bool isNotable(SymbolRef Sym) const {
405     return (bool) NotableSymbols.count(Sym);
406   }
407
408   /// classof - Used by isa<>, cast<>, and dyn_cast<>.
409   static bool classof(const BugReporter* R) {
410     return R->getKind() == GRBugReporterKind;
411   }
412 };
413
414 class BugReporterContext {
415   GRBugReporter &BR;
416   std::vector<BugReporterVisitor*> Callbacks;
417 public:
418   BugReporterContext(GRBugReporter& br) : BR(br) {}
419   virtual ~BugReporterContext();
420
421   void addVisitor(BugReporterVisitor* visitor) {
422     if (visitor) Callbacks.push_back(visitor);
423   }
424
425   typedef std::vector<BugReporterVisitor*>::iterator visitor_iterator;
426   visitor_iterator visitor_begin() { return Callbacks.begin(); }
427   visitor_iterator visitor_end() { return Callbacks.end(); }
428
429   GRBugReporter& getBugReporter() { return BR; }
430
431   ExplodedGraph &getGraph() { return BR.getGraph(); }
432
433   void addNotableSymbol(SymbolRef Sym) {
434     // FIXME: For now forward to GRBugReporter.
435     BR.addNotableSymbol(Sym);
436   }
437
438   bool isNotable(SymbolRef Sym) const {
439     // FIXME: For now forward to GRBugReporter.
440     return BR.isNotable(Sym);
441   }
442
443   GRStateManager& getStateManager() {
444     return BR.getStateManager();
445   }
446
447   ValueManager& getValueManager() {
448     return getStateManager().getValueManager();
449   }
450
451   ASTContext& getASTContext() {
452     return BR.getContext();
453   }
454
455   SourceManager& getSourceManager() {
456     return BR.getSourceManager();
457   }
458
459   virtual BugReport::NodeResolver& getNodeResolver() = 0;
460 };
461
462 class DiagBugReport : public RangedBugReport {
463   std::list<std::string> Strs;
464   FullSourceLoc L;
465 public:
466   DiagBugReport(BugType& D, const char* desc, FullSourceLoc l) :
467   RangedBugReport(D, desc, 0), L(l) {}
468
469   virtual ~DiagBugReport() {}
470
471   // FIXME: Move out-of-line (virtual function).
472   SourceLocation getLocation() const { return L; }
473
474   void addString(const std::string& s) { Strs.push_back(s); }
475
476   typedef std::list<std::string>::const_iterator str_iterator;
477   str_iterator str_begin() const { return Strs.begin(); }
478   str_iterator str_end() const { return Strs.end(); }
479 };
480
481 //===----------------------------------------------------------------------===//
482 //===----------------------------------------------------------------------===//
483
484 namespace bugreporter {
485
486 const Stmt *GetDerefExpr(const ExplodedNode *N);
487 const Stmt *GetReceiverExpr(const ExplodedNode *N);
488 const Stmt *GetDenomExpr(const ExplodedNode *N);
489 const Stmt *GetCalleeExpr(const ExplodedNode *N);
490 const Stmt *GetRetValExpr(const ExplodedNode *N);
491
492 void registerTrackNullOrUndefValue(BugReporterContext& BRC, const void *stmt,
493                                    const ExplodedNode* N);
494
495 } // end namespace clang::bugreporter
496
497 //===----------------------------------------------------------------------===//
498
499 } // end clang namespace
500
501 #endif