clang 24.0.0git
SymExpr.h
Go to the documentation of this file.
1//===- SymExpr.h - Management of Symbolic Values ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines SymExpr and SymbolData.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMEXPR_H
14#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMEXPR_H
15
16#include "clang/AST/Type.h"
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/iterator_range.h"
21#include "llvm/Support/Compiler.h"
22#include <cassert>
23
24namespace clang {
25namespace ento {
26
27class MemRegion;
28
30
31/// Symbolic value. These values used to capture symbolic execution of
32/// the program.
33class SymExpr : public llvm::FoldingSetNode {
34 virtual void anchor();
35
36public:
37 enum Kind {
38#define SYMBOL(Id, Parent) Id##Kind,
39#define SYMBOL_RANGE(Id, First, Last) BEGIN_##Id = First, END_##Id = Last,
40#include "clang/StaticAnalyzer/Core/PathSensitive/Symbols.def"
41 };
42
43private:
44 /// A unique identifier for this symbol.
45 ///
46 /// It is useful for SymbolData to easily differentiate multiple symbols, but
47 /// also for "ephemeral" symbols, such as binary operations, because this id
48 /// can be used for arranging constraints or equivalence classes instead of
49 /// unstable pointer values.
50 ///
51 /// Note, however, that it can't be used in Profile because SymbolManager
52 /// needs to compute Profile before allocating SymExpr.
53 const SymbolID Sym;
54
55protected:
56 SymExpr(Kind k, SymbolID Sym) : Sym(Sym), K(k) {}
57
59 // FIXME: Depending on whether we choose to deprecate structural symbols,
60 // this may become much stricter.
61 return !T.isNull() && !T->isVoidType();
62 }
63
64 mutable unsigned Complexity = 0;
65
66private:
67 // Declared last so that subclasses can put small fields into tail padding.
68 LLVM_PREFERRED_TYPE(Kind)
69 unsigned K : 8;
70
71public:
72 virtual ~SymExpr() = default;
73
74 Kind getKind() const { return static_cast<Kind>(K); }
75
76 /// Get a unique identifier for this symbol.
77 /// The ID is unique across all SymExprs in a SymbolManager.
78 /// They reflect the allocation order of these SymExprs,
79 /// and are likely stable across runs.
80 /// Used as a key in SymbolRef containers and as part of identity
81 /// for SymbolData, e.g. SymbolConjured with ID = 7 is "conj_$7".
82 SymbolID getSymbolID() const { return Sym; }
83
84 virtual void dump() const;
85
86 virtual void dumpToStream(raw_ostream &os) const {}
87
88 virtual QualType getType() const = 0;
89 virtual void Profile(llvm::FoldingSetNodeID &profile) = 0;
90
91 /// Iterator over symbols that the current symbol depends on.
92 ///
93 /// For SymbolData, it's the symbol itself; for expressions, it's the
94 /// expression symbol and all the operands in it. Note, SymbolDerived is
95 /// treated as SymbolData - the iterator will NOT visit the parent region.
96 ///
97 /// The worklist is owned by the symbol_range, so the iterators are
98 /// single-pass and only meant to be compared against the end iterator.
101
102 void expand(const SymExpr *SE);
103 bool atEnd() const { return !WL || WL->empty(); }
104
105 public:
106 symbol_iterator() = default;
108
110
111 const SymExpr *operator*() const {
112 assert(!atEnd() && "attempting to dereference an 'end' iterator");
113 return WL->back();
114 }
115
116 bool operator==(const symbol_iterator &X) const {
117 return atEnd() == X.atEnd();
118 }
119 bool operator!=(const symbol_iterator &X) const { return !(*this == X); }
120 };
121
122 /// A single-pass range of symbol_iterator that owns the worklist.
125
126 public:
127 explicit symbol_range(const SymExpr *Root) {
128 if (Root)
129 Worklist.push_back(Root);
130 }
131 symbol_range(const symbol_range &) = delete;
135
136 symbol_iterator begin() LLVM_LIFETIME_BOUND {
137 return symbol_iterator(&Worklist);
138 }
140 };
141
142 symbol_range symbols() const { return symbol_range(this); }
143
144 virtual unsigned computeComplexity() const = 0;
145
146 /// Find the region from which this symbol originates.
147 ///
148 /// Whenever the symbol was constructed to denote an unknown value of
149 /// a certain memory region, return this region. This method
150 /// allows checkers to make decisions depending on the origin of the symbol.
151 /// Symbol classes for which the origin region is known include
152 /// SymbolRegionValue which denotes the value of the region before
153 /// the beginning of the analysis, and SymbolDerived which denotes the value
154 /// of a certain memory region after its super region (a memory space or
155 /// a larger record region) is default-bound with a certain symbol.
156 /// It might return null.
157 virtual const MemRegion *getOriginRegion() const { return nullptr; }
158};
159
160inline raw_ostream &operator<<(raw_ostream &os,
161 const clang::ento::SymExpr *SE) {
162 SE->dumpToStream(os);
163 return os;
164}
165
166using SymbolRef = const SymExpr *;
168
169/// A symbol representing data which can be stored in a memory location
170/// (region).
171class SymbolData : public SymExpr {
172 void anchor() override;
173
174protected:
175 SymbolData(Kind k, SymbolID sym) : SymExpr(k, sym) { assert(classof(this)); }
176
177public:
178 ~SymbolData() override = default;
179
180 /// Get a string representation of the kind of the region.
181 virtual StringRef getKindStr() const = 0;
182
183 unsigned computeComplexity() const override {
184 return 1;
185 };
186
187 // Implement isa<T> support.
188 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
189 static constexpr bool classof(Kind K) {
190 return K >= BEGIN_SYMBOLS && K <= END_SYMBOLS;
191 }
192};
193
195 assert(!atEnd() && "attempting to iterate on an 'end' iterator");
196 const SymExpr *SE = WL->pop_back_val();
197 // SymbolData has no operands, which is by far the most common case.
198 if (!isa<SymbolData>(SE))
199 expand(SE);
200 return *this;
201}
202
203} // namespace ento
204} // namespace clang
205
206#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMEXPR_H
#define X(type, name)
Definition Value.h:97
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
C Language Family Type Representation.
A (possibly-)qualified type.
Definition TypeBase.h:938
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
Iterator over symbols that the current symbol depends on.
Definition SymExpr.h:99
bool operator==(const symbol_iterator &X) const
Definition SymExpr.h:116
const SymExpr * operator*() const
Definition SymExpr.h:111
symbol_iterator(SmallVectorImpl< const SymExpr * > *WL)
Definition SymExpr.h:107
bool operator!=(const symbol_iterator &X) const
Definition SymExpr.h:119
A single-pass range of symbol_iterator that owns the worklist.
Definition SymExpr.h:123
symbol_range & operator=(symbol_range &&)=delete
symbol_range(const symbol_range &)=delete
symbol_range & operator=(const symbol_range &)=delete
symbol_range(symbol_range &&)=delete
symbol_range(const SymExpr *Root)
Definition SymExpr.h:127
symbol_iterator begin() LLVM_LIFETIME_BOUND
Definition SymExpr.h:136
Symbolic value.
Definition SymExpr.h:33
virtual void dumpToStream(raw_ostream &os) const
Definition SymExpr.h:86
static bool isValidTypeForSymbol(QualType T)
Definition SymExpr.h:58
virtual void Profile(llvm::FoldingSetNodeID &profile)=0
virtual const MemRegion * getOriginRegion() const
Find the region from which this symbol originates.
Definition SymExpr.h:157
SymExpr(Kind k, SymbolID Sym)
Definition SymExpr.h:56
virtual QualType getType() const =0
Kind getKind() const
Definition SymExpr.h:74
symbol_range symbols() const
Definition SymExpr.h:142
virtual unsigned computeComplexity() const =0
virtual ~SymExpr()=default
virtual void dump() const
SymbolID getSymbolID() const
Get a unique identifier for this symbol.
Definition SymExpr.h:82
unsigned Complexity
Definition SymExpr.h:64
virtual StringRef getKindStr() const =0
Get a string representation of the kind of the region.
static constexpr bool classof(Kind K)
Definition SymExpr.h:189
unsigned computeComplexity() const override
Definition SymExpr.h:183
~SymbolData() override=default
static bool classof(const SymExpr *SE)
Definition SymExpr.h:188
SymbolData(Kind k, SymbolID sym)
Definition SymExpr.h:175
SmallVector< SymbolRef, 2 > SymbolRefSmallVectorTy
Definition SymExpr.h:167
const SymExpr * SymbolRef
Definition SymExpr.h:166
raw_ostream & operator<<(raw_ostream &os, const MemRegion *R)
Definition MemRegion.h:1704
unsigned SymbolID
Definition SymExpr.h:29
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
const FunctionProtoType * T
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 unsigned