clang  7.0.0svn
SymExpr.h
Go to the documentation of this file.
1 //===- SymExpr.h - Management of Symbolic Values ----------------*- 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 SymExpr and SymbolData.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMEXPR_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMEXPR_H
16 
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include <cassert>
22 
23 namespace clang {
24 namespace ento {
25 
26 class MemRegion;
27 
28 /// Symbolic value. These values used to capture symbolic execution of
29 /// the program.
30 class SymExpr : public llvm::FoldingSetNode {
31  virtual void anchor();
32 
33 public:
34  enum Kind {
35 #define SYMBOL(Id, Parent) Id##Kind,
36 #define SYMBOL_RANGE(Id, First, Last) BEGIN_##Id = First, END_##Id = Last,
37 #include "clang/StaticAnalyzer/Core/PathSensitive/Symbols.def"
38  };
39 
40 private:
41  Kind K;
42 
43 protected:
44  SymExpr(Kind k) : K(k) {}
45 
47  // FIXME: Depending on whether we choose to deprecate structural symbols,
48  // this may become much stricter.
49  return !T.isNull() && !T->isVoidType();
50  }
51 
52 public:
53  virtual ~SymExpr() = default;
54 
55  Kind getKind() const { return K; }
56 
57  virtual void dump() const;
58 
59  virtual void dumpToStream(raw_ostream &os) const {}
60 
61  virtual QualType getType() const = 0;
62  virtual void Profile(llvm::FoldingSetNodeID &profile) = 0;
63 
64  /// Iterator over symbols that the current symbol depends on.
65  ///
66  /// For SymbolData, it's the symbol itself; for expressions, it's the
67  /// expression symbol and all the operands in it. Note, SymbolDerived is
68  /// treated as SymbolData - the iterator will NOT visit the parent region.
71 
72  void expand();
73 
74  public:
75  symbol_iterator() = default;
76  symbol_iterator(const SymExpr *SE);
77 
79  const SymExpr *operator*();
80 
81  bool operator==(const symbol_iterator &X) const;
82  bool operator!=(const symbol_iterator &X) const;
83  };
84 
85  symbol_iterator symbol_begin() const { return symbol_iterator(this); }
87 
88  unsigned computeComplexity() const;
89 
90  /// Find the region from which this symbol originates.
91  ///
92  /// Whenever the symbol was constructed to denote an unknown value of
93  /// a certain memory region, return this region. This method
94  /// allows checkers to make decisions depending on the origin of the symbol.
95  /// Symbol classes for which the origin region is known include
96  /// SymbolRegionValue which denotes the value of the region before
97  /// the beginning of the analysis, and SymbolDerived which denotes the value
98  /// of a certain memory region after its super region (a memory space or
99  /// a larger record region) is default-bound with a certain symbol.
100  virtual const MemRegion *getOriginRegion() const { return nullptr; }
101 };
102 
103 inline raw_ostream &operator<<(raw_ostream &os,
104  const clang::ento::SymExpr *SE) {
105  SE->dumpToStream(os);
106  return os;
107 }
108 
109 using SymbolRef = const SymExpr *;
111 using SymbolID = unsigned;
112 
113 /// A symbol representing data which can be stored in a memory location
114 /// (region).
115 class SymbolData : public SymExpr {
116  const SymbolID Sym;
117 
118  void anchor() override;
119 
120 protected:
121  SymbolData(Kind k, SymbolID sym) : SymExpr(k), Sym(sym) {
122  assert(classof(this));
123  }
124 
125 public:
126  ~SymbolData() override = default;
127 
128  SymbolID getSymbolID() const { return Sym; }
129 
130  // Implement isa<T> support.
131  static inline bool classof(const SymExpr *SE) {
132  Kind k = SE->getKind();
133  return k >= BEGIN_SYMBOLS && k <= END_SYMBOLS;
134  }
135 };
136 
137 } // namespace ento
138 } // namespace clang
139 
140 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMEXPR_H
A (possibly-)qualified type.
Definition: Type.h:655
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
C Language Family Type Representation.
bool operator!=(const symbol_iterator &X) const
virtual void dump() const
virtual const MemRegion * getOriginRegion() const
Find the region from which this symbol originates.
Definition: SymExpr.h:100
Symbolic value.
Definition: SymExpr.h:30
symbol_iterator symbol_begin() const
Definition: SymExpr.h:85
virtual void dumpToStream(raw_ostream &os) const
Definition: SymExpr.h:59
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
virtual QualType getType() const =0
virtual ~SymExpr()=default
SymbolID getSymbolID() const
Definition: SymExpr.h:128
const FunctionProtoType * T
static bool classof(const SymExpr *SE)
Definition: SymExpr.h:131
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:720
Kind
virtual void Profile(llvm::FoldingSetNodeID &profile)=0
static bool isValidTypeForSymbol(QualType T)
Definition: SymExpr.h:46
Dataflow Directional Tag Classes.
raw_ostream & operator<<(raw_ostream &Out, const CheckerBase &Checker)
Dump checker name to stream.
Definition: Checker.cpp:34
static symbol_iterator symbol_end()
Definition: SymExpr.h:86
SymbolData(Kind k, SymbolID sym)
Definition: SymExpr.h:121
unsigned computeComplexity() const
static bool classof(const OMPClause *T)
Kind getKind() const
Definition: SymExpr.h:55
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13462
unsigned SymbolID
Definition: SymExpr.h:111
bool isVoidType() const
Definition: Type.h:6276
bool operator==(const symbol_iterator &X) const
A symbol representing data which can be stored in a memory location (region).
Definition: SymExpr.h:115
Iterator over symbols that the current symbol depends on.
Definition: SymExpr.h:69