PathDiagnostic.h revision 23803374d8db054192ea6fcb766b87e04f26c8fb
1//===--- PathDiagnostic.h - Path-Specific Diagnostic Handling ---*- 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 PathDiagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_PATH_DIAGNOSTIC_H
15#define LLVM_CLANG_PATH_DIAGNOSTIC_H
16
17#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/FoldingSet.h"
19#include <deque>
20#include <iterator>
21#include <string>
22#include <vector>
23
24namespace clang {
25
26class BinaryOperator;
27class CompoundStmt;
28class Decl;
29class LocationContext;
30class ParentMap;
31class ProgramPoint;
32class SourceManager;
33class Stmt;
34
35namespace ento {
36
37class ExplodedNode;
38
39//===----------------------------------------------------------------------===//
40// High-level interface for handlers of path-sensitive diagnostics.
41//===----------------------------------------------------------------------===//
42
43class PathDiagnostic;
44
45class PathDiagnosticClient : public DiagnosticClient  {
46public:
47  PathDiagnosticClient() {}
48
49  virtual ~PathDiagnosticClient() {}
50
51  virtual void
52  FlushDiagnostics(SmallVectorImpl<std::string> *FilesMade = 0) = 0;
53
54  void FlushDiagnostics(SmallVectorImpl<std::string> &FilesMade) {
55    FlushDiagnostics(&FilesMade);
56  }
57
58  virtual StringRef getName() const = 0;
59
60  virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
61                                const DiagnosticInfo &Info);
62  void HandlePathDiagnostic(const PathDiagnostic* D);
63
64  enum PathGenerationScheme { Minimal, Extensive };
65  virtual PathGenerationScheme getGenerationScheme() const { return Minimal; }
66  virtual bool supportsLogicalOpControlFlow() const { return false; }
67  virtual bool supportsAllBlockEdges() const { return false; }
68  virtual bool useVerboseDescription() const { return true; }
69
70protected:
71  /// The actual logic for handling path diagnostics, as implemented
72  /// by subclasses of PathDiagnosticClient.
73  virtual void HandlePathDiagnosticImpl(const PathDiagnostic* D) = 0;
74
75};
76
77//===----------------------------------------------------------------------===//
78// Path-sensitive diagnostics.
79//===----------------------------------------------------------------------===//
80
81class PathDiagnosticRange : public SourceRange {
82public:
83  bool isPoint;
84
85  PathDiagnosticRange(const SourceRange &R, bool isP = false)
86    : SourceRange(R), isPoint(isP) {}
87
88  PathDiagnosticRange() : isPoint(false) {}
89};
90
91class PathDiagnosticLocation {
92private:
93  enum Kind { RangeK, SingleLocK, StmtK, DeclK } K;
94  SourceRange R;
95  const Stmt *S;
96  const Decl *D;
97  const SourceManager *SM;
98  FullSourceLoc Loc;
99  PathDiagnosticRange Range;
100
101  FullSourceLoc genLocation(const ParentMap *PM=0) const;
102  PathDiagnosticRange genRange(const ParentMap *PM=0) const;
103
104public:
105  PathDiagnosticLocation()
106    : K(SingleLocK), S(0), D(0), SM(0) {
107  }
108
109  PathDiagnosticLocation(FullSourceLoc L)
110    : K(SingleLocK), R(L, L), S(0), D(0), SM(&L.getManager()),
111      Loc(genLocation()), Range(genRange()) {
112  }
113
114  PathDiagnosticLocation(SourceLocation L, const SourceManager &sm,
115                         Kind kind = SingleLocK)
116    : K(kind), R(L, L), S(0), D(0), SM(&sm),
117      Loc(genLocation()), Range(genRange()) {
118  }
119
120  PathDiagnosticLocation(const Stmt *s,
121                         const SourceManager &sm,
122                         const LocationContext *lc);
123
124  PathDiagnosticLocation(const Decl *d, const SourceManager &sm)
125    : K(DeclK), S(0), D(d), SM(&sm),
126      Loc(genLocation()), Range(genRange()) {
127  }
128
129  // Create a location for the beginning of the statement.
130  static PathDiagnosticLocation createBeginStmt(const Stmt *S,
131                                                const SourceManager &SM,
132                                                const LocationContext *LC);
133
134  /// Create the location for the operator of the binary expression.
135  /// Assumes the statement has a valid location.
136  static PathDiagnosticLocation createOperatorLoc(const BinaryOperator *BO,
137                                                  const SourceManager &SM);
138
139  /// Create a location for the beginning of the compound statement.
140  /// Assumes the statement has a valid location.
141  static PathDiagnosticLocation createBeginBrace(const CompoundStmt *CS,
142                                                 const SourceManager &SM);
143
144  /// Create a location for the end of the compound statement.
145  /// Assumes the statement has a valid location.
146  static PathDiagnosticLocation createEndBrace(const CompoundStmt *CS,
147                                               const SourceManager &SM);
148
149  /// Create a location for the beginning of the enclosing declaration body.
150  /// Defaults to the beginning of the first statement in the declaration body.
151  static PathDiagnosticLocation createDeclBegin(const LocationContext *LC,
152                                                const SourceManager &SM);
153
154  /// Constructs a location for the end of the enclosing declaration body.
155  /// Defaults to the end of brace.
156  static PathDiagnosticLocation createDeclEnd(const LocationContext *LC,
157                                                   const SourceManager &SM);
158
159  /// Create a location corresponding to the given valid ExplodedNode.
160  static PathDiagnosticLocation create(const ProgramPoint& P,
161                                       const SourceManager &SMng);
162
163  /// Create a location corresponding to the next valid ExplodedNode as end
164  /// of path location.
165  static PathDiagnosticLocation createEndOfPath(const ExplodedNode* N,
166                                                const SourceManager &SM);
167
168  /// Convert the given location into a single kind location.
169  static PathDiagnosticLocation createSingleLocation(
170                                             const PathDiagnosticLocation &PDL);
171
172  bool operator==(const PathDiagnosticLocation &X) const {
173    return K == X.K && R == X.R && S == X.S && D == X.D;
174  }
175
176  bool operator!=(const PathDiagnosticLocation &X) const {
177    return !(*this == X);
178  }
179
180  bool isValid() const {
181    return SM != 0;
182  }
183
184  FullSourceLoc asLocation() const {
185    return Loc;
186  }
187
188  PathDiagnosticRange asRange() const {
189    return Range;
190  }
191
192  const Stmt *asStmt() const { assert(isValid()); return S; }
193  const Decl *asDecl() const { assert(isValid()); return D; }
194
195  bool hasRange() const { return K == StmtK || K == RangeK || K == DeclK; }
196
197  void invalidate() {
198    *this = PathDiagnosticLocation();
199  }
200
201  void flatten();
202
203  const SourceManager& getManager() const { assert(isValid()); return *SM; }
204
205  void Profile(llvm::FoldingSetNodeID &ID) const;
206};
207
208class PathDiagnosticLocationPair {
209private:
210  PathDiagnosticLocation Start, End;
211public:
212  PathDiagnosticLocationPair(const PathDiagnosticLocation &start,
213                             const PathDiagnosticLocation &end)
214    : Start(start), End(end) {}
215
216  const PathDiagnosticLocation &getStart() const { return Start; }
217  const PathDiagnosticLocation &getEnd() const { return End; }
218
219  void flatten() {
220    Start.flatten();
221    End.flatten();
222  }
223
224  void Profile(llvm::FoldingSetNodeID &ID) const {
225    Start.Profile(ID);
226    End.Profile(ID);
227  }
228};
229
230//===----------------------------------------------------------------------===//
231// Path "pieces" for path-sensitive diagnostics.
232//===----------------------------------------------------------------------===//
233
234class PathDiagnosticPiece {
235public:
236  enum Kind { ControlFlow, Event, Macro };
237  enum DisplayHint { Above, Below };
238
239private:
240  const std::string str;
241  std::vector<FixItHint> FixItHints;
242  const Kind kind;
243  const DisplayHint Hint;
244  std::vector<SourceRange> ranges;
245
246  // Do not implement:
247  PathDiagnosticPiece();
248  PathDiagnosticPiece(const PathDiagnosticPiece &P);
249  PathDiagnosticPiece& operator=(const PathDiagnosticPiece &P);
250
251protected:
252  PathDiagnosticPiece(StringRef s, Kind k, DisplayHint hint = Below);
253
254  PathDiagnosticPiece(Kind k, DisplayHint hint = Below);
255
256public:
257  virtual ~PathDiagnosticPiece();
258
259  const std::string& getString() const { return str; }
260
261  /// getDisplayHint - Return a hint indicating where the diagnostic should
262  ///  be displayed by the PathDiagnosticClient.
263  DisplayHint getDisplayHint() const { return Hint; }
264
265  virtual PathDiagnosticLocation getLocation() const = 0;
266  virtual void flattenLocations() = 0;
267
268  Kind getKind() const { return kind; }
269
270  void addRange(SourceRange R) { ranges.push_back(R); }
271
272  void addRange(SourceLocation B, SourceLocation E) {
273    ranges.push_back(SourceRange(B,E));
274  }
275
276  void addFixItHint(const FixItHint& Hint) {
277    FixItHints.push_back(Hint);
278  }
279
280  typedef const SourceRange* range_iterator;
281
282  range_iterator ranges_begin() const {
283    return ranges.empty() ? NULL : &ranges[0];
284  }
285
286  range_iterator ranges_end() const {
287    return ranges_begin() + ranges.size();
288  }
289
290  typedef const FixItHint *fixit_iterator;
291
292  fixit_iterator fixit_begin() const {
293    return FixItHints.empty()? 0 : &FixItHints[0];
294  }
295
296  fixit_iterator fixit_end() const {
297    return FixItHints.empty()? 0
298                   : &FixItHints[0] + FixItHints.size();
299  }
300
301  static inline bool classof(const PathDiagnosticPiece *P) {
302    return true;
303  }
304
305  virtual void Profile(llvm::FoldingSetNodeID &ID) const;
306};
307
308class PathDiagnosticSpotPiece : public PathDiagnosticPiece {
309private:
310  PathDiagnosticLocation Pos;
311public:
312  PathDiagnosticSpotPiece(const PathDiagnosticLocation &pos,
313                          StringRef s,
314                          PathDiagnosticPiece::Kind k,
315                          bool addPosRange = true)
316  : PathDiagnosticPiece(s, k), Pos(pos) {
317    assert(Pos.asLocation().isValid() &&
318           "PathDiagnosticSpotPiece's must have a valid location.");
319    if (addPosRange && Pos.hasRange()) addRange(Pos.asRange());
320  }
321
322  PathDiagnosticLocation getLocation() const { return Pos; }
323  virtual void flattenLocations() { Pos.flatten(); }
324
325  virtual void Profile(llvm::FoldingSetNodeID &ID) const;
326};
327
328class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece {
329
330public:
331  PathDiagnosticEventPiece(const PathDiagnosticLocation &pos,
332                           StringRef s, bool addPosRange = true)
333    : PathDiagnosticSpotPiece(pos, s, Event, addPosRange) {}
334
335  ~PathDiagnosticEventPiece();
336
337  static inline bool classof(const PathDiagnosticPiece *P) {
338    return P->getKind() == Event;
339  }
340};
341
342class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece {
343  std::vector<PathDiagnosticLocationPair> LPairs;
344public:
345  PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos,
346                                 const PathDiagnosticLocation &endPos,
347                                 StringRef s)
348    : PathDiagnosticPiece(s, ControlFlow) {
349      LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos));
350    }
351
352  PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos,
353                                 const PathDiagnosticLocation &endPos)
354    : PathDiagnosticPiece(ControlFlow) {
355      LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos));
356    }
357
358  ~PathDiagnosticControlFlowPiece();
359
360  PathDiagnosticLocation getStartLocation() const {
361    assert(!LPairs.empty() &&
362           "PathDiagnosticControlFlowPiece needs at least one location.");
363    return LPairs[0].getStart();
364  }
365
366  PathDiagnosticLocation getEndLocation() const {
367    assert(!LPairs.empty() &&
368           "PathDiagnosticControlFlowPiece needs at least one location.");
369    return LPairs[0].getEnd();
370  }
371
372  void push_back(const PathDiagnosticLocationPair &X) { LPairs.push_back(X); }
373
374  virtual PathDiagnosticLocation getLocation() const {
375    return getStartLocation();
376  }
377
378  typedef std::vector<PathDiagnosticLocationPair>::iterator iterator;
379  iterator begin() { return LPairs.begin(); }
380  iterator end()   { return LPairs.end(); }
381
382  virtual void flattenLocations() {
383    for (iterator I=begin(), E=end(); I!=E; ++I) I->flatten();
384  }
385
386  typedef std::vector<PathDiagnosticLocationPair>::const_iterator
387          const_iterator;
388  const_iterator begin() const { return LPairs.begin(); }
389  const_iterator end() const   { return LPairs.end(); }
390
391  static inline bool classof(const PathDiagnosticPiece *P) {
392    return P->getKind() == ControlFlow;
393  }
394
395  virtual void Profile(llvm::FoldingSetNodeID &ID) const;
396};
397
398class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece {
399  std::vector<PathDiagnosticPiece*> SubPieces;
400public:
401  PathDiagnosticMacroPiece(const PathDiagnosticLocation &pos)
402    : PathDiagnosticSpotPiece(pos, "", Macro) {}
403
404  ~PathDiagnosticMacroPiece();
405
406  bool containsEvent() const;
407
408  void push_back(PathDiagnosticPiece *P) { SubPieces.push_back(P); }
409
410  typedef std::vector<PathDiagnosticPiece*>::iterator iterator;
411  iterator begin() { return SubPieces.begin(); }
412  iterator end() { return SubPieces.end(); }
413
414  virtual void flattenLocations() {
415    PathDiagnosticSpotPiece::flattenLocations();
416    for (iterator I=begin(), E=end(); I!=E; ++I) (*I)->flattenLocations();
417  }
418
419  typedef std::vector<PathDiagnosticPiece*>::const_iterator const_iterator;
420  const_iterator begin() const { return SubPieces.begin(); }
421  const_iterator end() const { return SubPieces.end(); }
422
423  static inline bool classof(const PathDiagnosticPiece *P) {
424    return P->getKind() == Macro;
425  }
426
427  virtual void Profile(llvm::FoldingSetNodeID &ID) const;
428};
429
430/// PathDiagnostic - PathDiagnostic objects represent a single path-sensitive
431///  diagnostic.  It represents an ordered-collection of PathDiagnosticPieces,
432///  each which represent the pieces of the path.
433class PathDiagnostic : public llvm::FoldingSetNode {
434  std::deque<PathDiagnosticPiece*> path;
435  unsigned Size;
436  std::string BugType;
437  std::string Desc;
438  std::string Category;
439  std::deque<std::string> OtherDesc;
440
441public:
442  PathDiagnostic();
443
444  PathDiagnostic(StringRef bugtype, StringRef desc,
445                 StringRef category);
446
447  ~PathDiagnostic();
448
449  StringRef getDescription() const { return Desc; }
450  StringRef getBugType() const { return BugType; }
451  StringRef getCategory() const { return Category; }
452
453  typedef std::deque<std::string>::const_iterator meta_iterator;
454  meta_iterator meta_begin() const { return OtherDesc.begin(); }
455  meta_iterator meta_end() const { return OtherDesc.end(); }
456  void addMeta(StringRef s) { OtherDesc.push_back(s); }
457
458  PathDiagnosticLocation getLocation() const {
459    assert(Size > 0 && "getLocation() requires a non-empty PathDiagnostic.");
460    return rbegin()->getLocation();
461  }
462
463  void push_front(PathDiagnosticPiece *piece) {
464    assert(piece);
465    path.push_front(piece);
466    ++Size;
467  }
468
469  void push_back(PathDiagnosticPiece *piece) {
470    assert(piece);
471    path.push_back(piece);
472    ++Size;
473  }
474
475  PathDiagnosticPiece *back() {
476    return path.back();
477  }
478
479  const PathDiagnosticPiece *back() const {
480    return path.back();
481  }
482
483  unsigned size() const { return Size; }
484  bool empty() const { return Size == 0; }
485
486  void resetPath(bool deletePieces = true);
487
488  class iterator {
489  public:
490    typedef std::deque<PathDiagnosticPiece*>::iterator ImplTy;
491
492    typedef PathDiagnosticPiece              value_type;
493    typedef value_type&                      reference;
494    typedef value_type*                      pointer;
495    typedef ptrdiff_t                        difference_type;
496    typedef std::bidirectional_iterator_tag  iterator_category;
497
498  private:
499    ImplTy I;
500
501  public:
502    iterator(const ImplTy& i) : I(i) {}
503
504    bool operator==(const iterator &X) const { return I == X.I; }
505    bool operator!=(const iterator &X) const { return I != X.I; }
506
507    PathDiagnosticPiece& operator*() const { return **I; }
508    PathDiagnosticPiece *operator->() const { return *I; }
509
510    iterator &operator++() { ++I; return *this; }
511    iterator &operator--() { --I; return *this; }
512  };
513
514  class const_iterator {
515  public:
516    typedef std::deque<PathDiagnosticPiece*>::const_iterator ImplTy;
517
518    typedef const PathDiagnosticPiece        value_type;
519    typedef value_type&                      reference;
520    typedef value_type*                      pointer;
521    typedef ptrdiff_t                        difference_type;
522    typedef std::bidirectional_iterator_tag  iterator_category;
523
524  private:
525    ImplTy I;
526
527  public:
528    const_iterator(const ImplTy& i) : I(i) {}
529
530    bool operator==(const const_iterator &X) const { return I == X.I; }
531    bool operator!=(const const_iterator &X) const { return I != X.I; }
532
533    reference operator*() const { return **I; }
534    pointer operator->() const { return *I; }
535
536    const_iterator &operator++() { ++I; return *this; }
537    const_iterator &operator--() { --I; return *this; }
538  };
539
540  typedef std::reverse_iterator<iterator>       reverse_iterator;
541  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
542
543  // forward iterator creation methods.
544
545  iterator begin() { return path.begin(); }
546  iterator end() { return path.end(); }
547
548  const_iterator begin() const { return path.begin(); }
549  const_iterator end() const { return path.end(); }
550
551  // reverse iterator creation methods.
552  reverse_iterator rbegin()            { return reverse_iterator(end()); }
553  const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
554  reverse_iterator rend()              { return reverse_iterator(begin()); }
555  const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
556
557  void flattenLocations() {
558    for (iterator I = begin(), E = end(); I != E; ++I) I->flattenLocations();
559  }
560
561  void Profile(llvm::FoldingSetNodeID &ID) const;
562};
563
564} // end GR namespace
565
566} //end clang namespace
567
568#endif
569