]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Checker/BugReporter/BugReporter.h
Update clang to r96341.
[FreeBSD/FreeBSD.git] / include / clang / Checker / 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 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/Checker/PathSensitive/GRState.h"
21 #include "clang/Checker/PathSensitive/ExplodedGraph.h"
22 #include "clang/Checker/BugReporter/BugType.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallString.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, llvm::StringRef desc, const ExplodedNode *n)
84     : BT(bt), Description(desc), EndNode(n) {}
85
86   BugReport(BugType& bt, llvm::StringRef shortDesc, llvm::StringRef 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 llvm::StringRef getDescription() const { return Description; }
107
108   const llvm::StringRef 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
187 //===----------------------------------------------------------------------===//
188 // Specialized subclasses of BugReport.
189 //===----------------------------------------------------------------------===//
190
191 // FIXME: Collapse this with the default BugReport class.
192 class RangedBugReport : public BugReport {
193   std::vector<SourceRange> Ranges;
194 public:
195   RangedBugReport(BugType& D, llvm::StringRef description, ExplodedNode *n)
196     : BugReport(D, description, n) {}
197
198   RangedBugReport(BugType& D, llvm::StringRef shortDescription,
199                   llvm::StringRef description, ExplodedNode *n)
200   : BugReport(D, shortDescription, description, n) {}
201
202   ~RangedBugReport();
203
204   // FIXME: Move this out of line.
205   void addRange(SourceRange R) {
206     assert(R.isValid());
207     Ranges.push_back(R);
208   }
209
210   // FIXME: Move this out of line.
211   void getRanges(const SourceRange*& beg, const SourceRange*& end) {
212
213     if (Ranges.empty()) {
214       beg = NULL;
215       end = NULL;
216     }
217     else {
218       beg = &Ranges[0];
219       end = beg + Ranges.size();
220     }
221   }
222 };
223
224 class EnhancedBugReport : public RangedBugReport {
225 public:
226   typedef void (*VisitorCreator)(BugReporterContext &BRcC, const void *data,
227                                  const ExplodedNode *N);
228
229 private:
230   typedef std::vector<std::pair<VisitorCreator, const void*> > Creators;
231   Creators creators;
232
233 public:
234   EnhancedBugReport(BugType& D, llvm::StringRef description, ExplodedNode *n)
235    : RangedBugReport(D, description, n) {}
236
237   EnhancedBugReport(BugType& D, llvm::StringRef shortDescription,
238                    llvm::StringRef description, ExplodedNode *n)
239     : RangedBugReport(D, shortDescription, description, n) {}
240
241   ~EnhancedBugReport() {}
242
243   void registerInitialVisitors(BugReporterContext& BRC, const ExplodedNode* N) {
244     for (Creators::iterator I = creators.begin(), E = creators.end(); I!=E; ++I)
245       I->first(BRC, I->second, N);
246   }
247
248   void addVisitorCreator(VisitorCreator creator, const void *data) {
249     creators.push_back(std::make_pair(creator, data));
250   }
251 };
252
253 //===----------------------------------------------------------------------===//
254 // BugReporter and friends.
255 //===----------------------------------------------------------------------===//
256
257 class BugReporterData {
258 public:
259   virtual ~BugReporterData();
260   virtual Diagnostic& getDiagnostic() = 0;
261   virtual PathDiagnosticClient* getPathDiagnosticClient() = 0;
262   virtual ASTContext& getASTContext() = 0;
263   virtual SourceManager& getSourceManager() = 0;
264 };
265
266 class BugReporter {
267 public:
268   enum Kind { BaseBRKind, GRBugReporterKind };
269
270 private:
271   typedef llvm::ImmutableSet<BugType*> BugTypesTy;
272   BugTypesTy::Factory F;
273   BugTypesTy BugTypes;
274
275   const Kind kind;
276   BugReporterData& D;
277
278   void FlushReport(BugReportEquivClass& EQ);
279
280 protected:
281   BugReporter(BugReporterData& d, Kind k) : BugTypes(F.GetEmptySet()), kind(k), D(d) {}
282
283 public:
284   BugReporter(BugReporterData& d) : BugTypes(F.GetEmptySet()), kind(BaseBRKind), D(d) {}
285   virtual ~BugReporter();
286
287   void FlushReports();
288
289   Kind getKind() const { return kind; }
290
291   Diagnostic& getDiagnostic() {
292     return D.getDiagnostic();
293   }
294
295   PathDiagnosticClient* getPathDiagnosticClient() {
296     return D.getPathDiagnosticClient();
297   }
298
299   typedef BugTypesTy::iterator iterator;
300   iterator begin() { return BugTypes.begin(); }
301   iterator end() { return BugTypes.end(); }
302
303   ASTContext& getContext() { return D.getASTContext(); }
304
305   SourceManager& getSourceManager() { return D.getSourceManager(); }
306
307   virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
308                                       BugReportEquivClass& EQ) {}
309
310   void Register(BugType *BT);
311
312   void EmitReport(BugReport *R);
313
314   void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr,
315                        SourceLocation Loc,
316                        SourceRange* RangeBeg, unsigned NumRanges);
317
318   void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugCategory,
319                        llvm::StringRef BugStr, SourceLocation Loc,
320                        SourceRange* RangeBeg, unsigned NumRanges);
321
322
323   void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr,
324                        SourceLocation Loc) {
325     EmitBasicReport(BugName, BugStr, Loc, 0, 0);
326   }
327
328   void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugCategory,
329                        llvm::StringRef BugStr, SourceLocation Loc) {
330     EmitBasicReport(BugName, BugCategory, BugStr, Loc, 0, 0);
331   }
332
333   void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr,
334                        SourceLocation Loc, SourceRange R) {
335     EmitBasicReport(BugName, BugStr, Loc, &R, 1);
336   }
337
338   void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef Category,
339                        llvm::StringRef BugStr, SourceLocation Loc,
340                        SourceRange R) {
341     EmitBasicReport(BugName, Category, BugStr, Loc, &R, 1);
342   }
343
344   static bool classof(const BugReporter* R) { return true; }
345 };
346
347 // FIXME: Get rid of GRBugReporter.  It's the wrong abstraction.
348 class GRBugReporter : public BugReporter {
349   GRExprEngine& Eng;
350   llvm::SmallSet<SymbolRef, 10> NotableSymbols;
351 public:
352   GRBugReporter(BugReporterData& d, GRExprEngine& eng)
353     : BugReporter(d, GRBugReporterKind), Eng(eng) {}
354
355   virtual ~GRBugReporter();
356
357   /// getEngine - Return the analysis engine used to analyze a given
358   ///  function or method.
359   GRExprEngine &getEngine() { return Eng; }
360
361   /// getGraph - Get the exploded graph created by the analysis engine
362   ///  for the analyzed method or function.
363   ExplodedGraph &getGraph();
364
365   /// getStateManager - Return the state manager used by the analysis
366   ///  engine.
367   GRStateManager &getStateManager();
368
369   virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
370                                       BugReportEquivClass& R);
371
372   void addNotableSymbol(SymbolRef Sym) {
373     NotableSymbols.insert(Sym);
374   }
375
376   bool isNotable(SymbolRef Sym) const {
377     return (bool) NotableSymbols.count(Sym);
378   }
379
380   /// classof - Used by isa<>, cast<>, and dyn_cast<>.
381   static bool classof(const BugReporter* R) {
382     return R->getKind() == GRBugReporterKind;
383   }
384 };
385
386 class BugReporterContext {
387   GRBugReporter &BR;
388   std::vector<BugReporterVisitor*> Callbacks;
389 public:
390   BugReporterContext(GRBugReporter& br) : BR(br) {}
391   virtual ~BugReporterContext();
392
393   void addVisitor(BugReporterVisitor* visitor) {
394     if (visitor) Callbacks.push_back(visitor);
395   }
396
397   typedef std::vector<BugReporterVisitor*>::iterator visitor_iterator;
398   visitor_iterator visitor_begin() { return Callbacks.begin(); }
399   visitor_iterator visitor_end() { return Callbacks.end(); }
400
401   GRBugReporter& getBugReporter() { return BR; }
402
403   ExplodedGraph &getGraph() { return BR.getGraph(); }
404
405   void addNotableSymbol(SymbolRef Sym) {
406     // FIXME: For now forward to GRBugReporter.
407     BR.addNotableSymbol(Sym);
408   }
409
410   bool isNotable(SymbolRef Sym) const {
411     // FIXME: For now forward to GRBugReporter.
412     return BR.isNotable(Sym);
413   }
414
415   GRStateManager& getStateManager() {
416     return BR.getStateManager();
417   }
418
419   ValueManager& getValueManager() {
420     return getStateManager().getValueManager();
421   }
422
423   ASTContext& getASTContext() {
424     return BR.getContext();
425   }
426
427   SourceManager& getSourceManager() {
428     return BR.getSourceManager();
429   }
430
431   virtual BugReport::NodeResolver& getNodeResolver() = 0;
432 };
433
434 class DiagBugReport : public RangedBugReport {
435   std::list<std::string> Strs;
436   FullSourceLoc L;
437 public:
438   DiagBugReport(BugType& D, llvm::StringRef desc, FullSourceLoc l) :
439   RangedBugReport(D, desc, 0), L(l) {}
440
441   virtual ~DiagBugReport() {}
442
443   // FIXME: Move out-of-line (virtual function).
444   SourceLocation getLocation() const { return L; }
445
446   void addString(llvm::StringRef s) { Strs.push_back(s); }
447
448   typedef std::list<std::string>::const_iterator str_iterator;
449   str_iterator str_begin() const { return Strs.begin(); }
450   str_iterator str_end() const { return Strs.end(); }
451 };
452
453 //===----------------------------------------------------------------------===//
454 //===----------------------------------------------------------------------===//
455
456 namespace bugreporter {
457
458 const Stmt *GetDerefExpr(const ExplodedNode *N);
459 const Stmt *GetReceiverExpr(const ExplodedNode *N);
460 const Stmt *GetDenomExpr(const ExplodedNode *N);
461 const Stmt *GetCalleeExpr(const ExplodedNode *N);
462 const Stmt *GetRetValExpr(const ExplodedNode *N);
463
464 void registerTrackNullOrUndefValue(BugReporterContext& BRC, const void *stmt,
465                                    const ExplodedNode* N);
466
467 void registerFindLastStore(BugReporterContext& BRC, const void *memregion,
468                            const ExplodedNode *N);
469
470
471 } // end namespace clang::bugreporter
472
473 //===----------------------------------------------------------------------===//
474
475 } // end clang namespace
476
477 #endif