clang 23.0.0git
SymbolManager.h
Go to the documentation of this file.
1//===- SymbolManager.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 SymbolManager, a class that manages symbolic values
10// created for use by ExprEngine and related classes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
15#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
16
17#include "clang/AST/Expr.h"
18#include "clang/AST/Type.h"
20#include "clang/Basic/LLVM.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/DenseSet.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/ADT/ImmutableSet.h"
29#include "llvm/ADT/iterator_range.h"
30#include "llvm/Support/Allocator.h"
31#include <cassert>
32
33namespace clang {
34
35class ASTContext;
36class Stmt;
37
38namespace ento {
39
41class StoreManager;
42
43///A symbol representing the value stored at a MemRegion.
44class SymbolRegionValue : public SymbolData {
45 const TypedValueRegion *R;
46
47 friend class SymExprAllocator;
48 SymbolRegionValue(SymbolID sym, const TypedValueRegion *r)
49 : SymbolData(ClassKind, sym), R(r) {
50 assert(r);
52 }
53
54public:
55 LLVM_ATTRIBUTE_RETURNS_NONNULL
56 const TypedValueRegion *getRegion() const { return R; }
57
58 static void Profile(llvm::FoldingSetNodeID& profile, const TypedValueRegion* R) {
59 profile.AddInteger((unsigned)ClassKind);
60 profile.AddPointer(R);
61 }
62
63 void Profile(llvm::FoldingSetNodeID& profile) override {
64 Profile(profile, R);
65 }
66
67 StringRef getKindStr() const override;
68
69 void dumpToStream(raw_ostream &os) const override;
70 const MemRegion *getOriginRegion() const override { return getRegion(); }
71
72 QualType getType() const override;
73
74 // Implement isa<T> support.
75 static constexpr Kind ClassKind = SymbolRegionValueKind;
76 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
77 static constexpr bool classof(Kind K) { return K == ClassKind; }
78};
79
80/// A symbol representing the result of an expression in the case when we do
81/// not know anything about what the expression is.
82class SymbolConjured : public SymbolData {
84 QualType T;
85 unsigned Count;
86 const StackFrame *SF;
87 const void *SymbolTag;
88
89 friend class SymExprAllocator;
90 SymbolConjured(SymbolID sym, ConstCFGElementRef elem, const StackFrame *SF,
91 QualType t, unsigned count, const void *symbolTag)
92 : SymbolData(ClassKind, sym), Elem(elem), T(t), Count(count), SF(SF),
93 SymbolTag(symbolTag) {
94 assert(SF);
95 assert(isValidTypeForSymbol(t));
96 }
97
98public:
99 ConstCFGElementRef getCFGElementRef() const { return Elem; }
100
101 // It might return null.
102 const Stmt *getStmt() const;
103
104 unsigned getCount() const { return Count; }
105 /// It might return null.
106 const void *getTag() const { return SymbolTag; }
107
108 QualType getType() const override;
109
110 StringRef getKindStr() const override;
111
112 void dumpToStream(raw_ostream &os) const override;
113
114 static void Profile(llvm::FoldingSetNodeID &profile, ConstCFGElementRef Elem,
115 const StackFrame *SF, QualType T, unsigned Count,
116 const void *SymbolTag) {
117 profile.AddInteger((unsigned)ClassKind);
118 profile.Add(Elem);
119 profile.AddPointer(SF);
120 profile.Add(T);
121 profile.AddInteger(Count);
122 profile.AddPointer(SymbolTag);
123 }
124
125 void Profile(llvm::FoldingSetNodeID& profile) override {
126 Profile(profile, Elem, SF, T, Count, SymbolTag);
127 }
128
129 // Implement isa<T> support.
130 static constexpr Kind ClassKind = SymbolConjuredKind;
131 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
132 static constexpr bool classof(Kind K) { return K == ClassKind; }
133};
134
135/// A symbol representing the value of a MemRegion whose parent region has
136/// symbolic value.
137class SymbolDerived : public SymbolData {
138 SymbolRef parentSymbol;
139 const TypedValueRegion *R;
140
141 friend class SymExprAllocator;
142 SymbolDerived(SymbolID sym, SymbolRef parent, const TypedValueRegion *r)
143 : SymbolData(ClassKind, sym), parentSymbol(parent), R(r) {
144 assert(parent);
145 assert(r);
147 }
148
149public:
150 LLVM_ATTRIBUTE_RETURNS_NONNULL
151 SymbolRef getParentSymbol() const { return parentSymbol; }
152 LLVM_ATTRIBUTE_RETURNS_NONNULL
153 const TypedValueRegion *getRegion() const { return R; }
154
155 QualType getType() const override;
156
157 StringRef getKindStr() const override;
158
159 void dumpToStream(raw_ostream &os) const override;
160 const MemRegion *getOriginRegion() const override { return getRegion(); }
161
162 static void Profile(llvm::FoldingSetNodeID& profile, SymbolRef parent,
163 const TypedValueRegion *r) {
164 profile.AddInteger((unsigned)ClassKind);
165 profile.AddPointer(r);
166 profile.AddPointer(parent);
167 }
168
169 void Profile(llvm::FoldingSetNodeID& profile) override {
170 Profile(profile, parentSymbol, R);
171 }
172
173 // Implement isa<T> support.
174 static constexpr Kind ClassKind = SymbolDerivedKind;
175 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
176 static constexpr bool classof(Kind K) { return K == ClassKind; }
177};
178
179/// SymbolExtent - Represents the extent (size in bytes) of a bounded region.
180/// Clients should not ask the SymbolManager for a region's extent. Always use
181/// SubRegion::getExtent instead -- the value returned may not be a symbol.
182class SymbolExtent : public SymbolData {
183 const SubRegion *R;
184
185 friend class SymExprAllocator;
186 SymbolExtent(SymbolID sym, const SubRegion *r)
187 : SymbolData(ClassKind, sym), R(r) {
188 assert(r);
189 }
190
191public:
192 LLVM_ATTRIBUTE_RETURNS_NONNULL
193 const SubRegion *getRegion() const { return R; }
194
195 QualType getType() const override;
196
197 StringRef getKindStr() const override;
198
199 void dumpToStream(raw_ostream &os) const override;
200
201 static void Profile(llvm::FoldingSetNodeID& profile, const SubRegion *R) {
202 profile.AddInteger((unsigned)ClassKind);
203 profile.AddPointer(R);
204 }
205
206 void Profile(llvm::FoldingSetNodeID& profile) override {
207 Profile(profile, R);
208 }
209
210 // Implement isa<T> support.
211 static constexpr Kind ClassKind = SymbolExtentKind;
212 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
213 static constexpr bool classof(Kind K) { return K == ClassKind; }
214};
215
216/// SymbolMetadata - Represents path-dependent metadata about a specific region.
217/// Metadata symbols remain live as long as they are marked as in use before
218/// dead-symbol sweeping AND their associated regions are still alive.
219/// Intended for use by checkers.
220class SymbolMetadata : public SymbolData {
221 const MemRegion* R;
222 const Stmt *S;
223 QualType T;
224 const StackFrame *SF;
225 /// Count can be used to differentiate regions corresponding to
226 /// different loop iterations, thus, making the symbol path-dependent.
227 unsigned Count;
228 const void *Tag;
229
230 friend class SymExprAllocator;
231 SymbolMetadata(SymbolID sym, const MemRegion *r, const Stmt *s, QualType t,
232 const StackFrame *SF, unsigned count, const void *tag)
233 : SymbolData(ClassKind, sym), R(r), S(s), T(t), SF(SF), Count(count),
234 Tag(tag) {
235 assert(r);
236 assert(s);
237 assert(isValidTypeForSymbol(t));
238 assert(SF);
239 assert(tag);
240 }
241
242 public:
243 LLVM_ATTRIBUTE_RETURNS_NONNULL
244 const MemRegion *getRegion() const { return R; }
245
246 LLVM_ATTRIBUTE_RETURNS_NONNULL
247 const Stmt *getStmt() const { return S; }
248
249 LLVM_ATTRIBUTE_RETURNS_NONNULL
250 const StackFrame *getStackFrame() const { return SF; }
251
252 unsigned getCount() const { return Count; }
253
254 LLVM_ATTRIBUTE_RETURNS_NONNULL
255 const void *getTag() const { return Tag; }
256
257 QualType getType() const override;
258
259 StringRef getKindStr() const override;
260
261 void dumpToStream(raw_ostream &os) const override;
262
263 static void Profile(llvm::FoldingSetNodeID &profile, const MemRegion *R,
264 const Stmt *S, QualType T, const StackFrame *SF,
265 unsigned Count, const void *Tag) {
266 profile.AddInteger((unsigned)ClassKind);
267 profile.AddPointer(R);
268 profile.AddPointer(S);
269 profile.Add(T);
270 profile.AddPointer(SF);
271 profile.AddInteger(Count);
272 profile.AddPointer(Tag);
273 }
274
275 void Profile(llvm::FoldingSetNodeID& profile) override {
276 Profile(profile, R, S, T, SF, Count, Tag);
277 }
278
279 // Implement isa<T> support.
280 static constexpr Kind ClassKind = SymbolMetadataKind;
281 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
282 static constexpr bool classof(Kind K) { return K == ClassKind; }
283};
284
285/// Represents a cast expression.
286class SymbolCast : public SymExpr {
287 const SymExpr *Operand;
288
289 /// Type of the operand.
290 QualType FromTy;
291
292 /// The type of the result.
293 QualType ToTy;
294
295 friend class SymExprAllocator;
296 SymbolCast(SymbolID Sym, const SymExpr *In, QualType From, QualType To)
297 : SymExpr(ClassKind, Sym), Operand(In), FromTy(From), ToTy(To) {
298 assert(In);
299 assert(isValidTypeForSymbol(From));
300 // FIXME: GenericTaintChecker creates symbols of void type.
301 // Otherwise, 'To' should also be a valid type.
302 }
303
304public:
305 unsigned computeComplexity() const override {
306 if (Complexity == 0)
307 Complexity = 1 + Operand->computeComplexity();
308 return Complexity;
309 }
310
311 QualType getType() const override { return ToTy; }
312
313 LLVM_ATTRIBUTE_RETURNS_NONNULL
314 const SymExpr *getOperand() const { return Operand; }
315
316 void dumpToStream(raw_ostream &os) const override;
317
318 static void Profile(llvm::FoldingSetNodeID& ID,
319 const SymExpr *In, QualType From, QualType To) {
320 ID.AddInteger((unsigned)ClassKind);
321 ID.AddPointer(In);
322 ID.Add(From);
323 ID.Add(To);
324 }
325
326 void Profile(llvm::FoldingSetNodeID& ID) override {
327 Profile(ID, Operand, FromTy, ToTy);
328 }
329
330 // Implement isa<T> support.
331 static constexpr Kind ClassKind = SymbolCastKind;
332 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
333 static constexpr bool classof(Kind K) { return K == ClassKind; }
334};
335
336/// Represents a symbolic expression involving a unary operator.
337class UnarySymExpr : public SymExpr {
338 const SymExpr *Operand;
340 QualType T;
341
342 friend class SymExprAllocator;
343 UnarySymExpr(SymbolID Sym, const SymExpr *In, UnaryOperator::Opcode Op,
344 QualType T)
345 : SymExpr(ClassKind, Sym), Operand(In), Op(Op), T(T) {
346 // Note, some unary operators are modeled as a binary operator. E.g. ++x is
347 // modeled as x + 1.
348 assert((Op == UO_Minus || Op == UO_Not) && "non-supported unary expression");
349 // Unary expressions are results of arithmetic. Pointer arithmetic is not
350 // handled by unary expressions, but it is instead handled by applying
351 // sub-regions to regions.
352 assert(isValidTypeForSymbol(T) && "non-valid type for unary symbol");
353 assert(!Loc::isLocType(T) && "unary symbol should be nonloc");
354 }
355
356public:
357 unsigned computeComplexity() const override {
358 if (Complexity == 0)
359 Complexity = 1 + Operand->computeComplexity();
360 return Complexity;
361 }
362
363 const SymExpr *getOperand() const { return Operand; }
364 UnaryOperator::Opcode getOpcode() const { return Op; }
365 QualType getType() const override { return T; }
366
367 void dumpToStream(raw_ostream &os) const override;
368
369 static void Profile(llvm::FoldingSetNodeID &ID, const SymExpr *In,
371 ID.AddInteger((unsigned)ClassKind);
372 ID.AddPointer(In);
373 ID.AddInteger(Op);
374 ID.Add(T);
375 }
376
377 void Profile(llvm::FoldingSetNodeID &ID) override {
378 Profile(ID, Operand, Op, T);
379 }
380
381 // Implement isa<T> support.
382 static constexpr Kind ClassKind = UnarySymExprKind;
383 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
384 static constexpr bool classof(Kind K) { return K == ClassKind; }
385};
386
387/// Represents a symbolic expression involving a binary operator
388class BinarySymExpr : public SymExpr {
390 QualType T;
391
392protected:
394 : SymExpr(k, Sym), Op(op), T(t) {
395 assert(classof(this));
396 // Binary expressions are results of arithmetic. Pointer arithmetic is not
397 // handled by binary expressions, but it is instead handled by applying
398 // sub-regions to regions.
399 assert(isValidTypeForSymbol(t) && !Loc::isLocType(t));
400 }
401
402public:
403 // FIXME: We probably need to make this out-of-line to avoid redundant
404 // generation of virtual functions.
405 QualType getType() const override { return T; }
406
407 BinaryOperator::Opcode getOpcode() const { return Op; }
408
409 // Implement isa<T> support.
410 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
411 static constexpr bool classof(Kind K) {
412 return K >= BEGIN_BINARYSYMEXPRS && K <= END_BINARYSYMEXPRS;
413 }
414
415protected:
416 static unsigned computeOperandComplexity(const SymExpr *Value) {
417 return Value->computeComplexity();
418 }
419 static unsigned computeOperandComplexity(const llvm::APSInt &Value) {
420 return 1;
421 }
422
423 static const llvm::APSInt *getPointer(APSIntPtr Value) { return Value.get(); }
424 static const SymExpr *getPointer(const SymExpr *Value) { return Value; }
425
426 static void dumpToStreamImpl(raw_ostream &os, const SymExpr *Value);
427 static void dumpToStreamImpl(raw_ostream &os, const llvm::APSInt &Value);
428 static void dumpToStreamImpl(raw_ostream &os, BinaryOperator::Opcode op);
429};
430
431/// Template implementation for all binary symbolic expressions
432template <class LHSTYPE, class RHSTYPE, SymExpr::Kind ClassK>
433class BinarySymExprImpl : public BinarySymExpr {
434 LHSTYPE LHS;
435 RHSTYPE RHS;
436
437 friend class SymExprAllocator;
438 BinarySymExprImpl(SymbolID Sym, LHSTYPE lhs, BinaryOperator::Opcode op,
439 RHSTYPE rhs, QualType t)
440 : BinarySymExpr(Sym, ClassKind, op, t), LHS(lhs), RHS(rhs) {
441 assert(getPointer(lhs));
442 assert(getPointer(rhs));
443 }
444
445public:
446 void dumpToStream(raw_ostream &os) const override {
447 dumpToStreamImpl(os, LHS);
449 dumpToStreamImpl(os, RHS);
450 }
451
452 LHSTYPE getLHS() const { return LHS; }
453 RHSTYPE getRHS() const { return RHS; }
454
455 unsigned computeComplexity() const override {
456 if (Complexity == 0)
457 Complexity =
459 return Complexity;
460 }
461
462 static void Profile(llvm::FoldingSetNodeID &ID, LHSTYPE lhs,
463 BinaryOperator::Opcode op, RHSTYPE rhs, QualType t) {
464 ID.AddInteger((unsigned)ClassKind);
465 ID.AddPointer(getPointer(lhs));
466 ID.AddInteger(op);
467 ID.AddPointer(getPointer(rhs));
468 ID.Add(t);
469 }
470
471 void Profile(llvm::FoldingSetNodeID &ID) override {
472 Profile(ID, LHS, getOpcode(), RHS, getType());
473 }
474
475 // Implement isa<T> support.
476 static constexpr Kind ClassKind = ClassK;
477 static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
478 static constexpr bool classof(Kind K) { return K == ClassKind; }
479};
480
481/// Represents a symbolic expression like 'x' + 3.
483 SymExpr::Kind::SymIntExprKind>;
484
485/// Represents a symbolic expression like 3 - 'x'.
487 SymExpr::Kind::IntSymExprKind>;
488
489/// Represents a symbolic expression like 'x' + 'y'.
491 SymExpr::Kind::SymSymExprKind>;
492
494 SymbolID NextSymbolID = 0;
495 llvm::BumpPtrAllocator &Alloc;
496
497public:
498 explicit SymExprAllocator(llvm::BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
499
500 template <class SymT, typename... ArgsT> SymT *make(ArgsT &&...Args) {
501 return new (Alloc) SymT(nextID(), std::forward<ArgsT>(Args)...);
502 }
503
504private:
505 SymbolID nextID() { return NextSymbolID++; }
506};
507
509 using DataSetTy = llvm::FoldingSet<SymExpr>;
510 using SymbolDependTy =
511 llvm::DenseMap<SymbolRef, std::unique_ptr<SymbolRefSmallVectorTy>>;
512
513 DataSetTy DataSet;
514
515 /// Stores the extra dependencies between symbols: the data should be kept
516 /// alive as long as the key is live.
517 SymbolDependTy SymbolDependencies;
518
519 SymExprAllocator Alloc;
521 ASTContext &Ctx;
522
523public:
525 llvm::BumpPtrAllocator &bpalloc)
526 : SymbolDependencies(16), Alloc(bpalloc), BV(bv), Ctx(ctx) {}
527
528 static bool canSymbolicate(QualType T);
529
530 /// Create or retrieve a SymExpr of type \p SymExprT for the given arguments.
531 /// Use the arguments to check for an existing SymExpr and return it,
532 /// otherwise, create a new one and keep a pointer to it to avoid duplicates.
533 template <typename SymExprT, typename... Args>
534 const SymExprT *acquire(Args &&...args);
535
537 const StackFrame *SF, QualType T,
538 unsigned VisitCount,
539 const void *SymbolTag = nullptr) {
540
541 return acquire<SymbolConjured>(Elem, SF, T, VisitCount, SymbolTag);
542 }
543
544 QualType getType(const SymExpr *SE) const {
545 return SE->getType();
546 }
547
548 /// Add artificial symbol dependency.
549 ///
550 /// The dependent symbol should stay alive as long as the primary is alive.
551 void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent);
552
554
555 ASTContext &getContext() { return Ctx; }
557};
558
559/// A class responsible for cleaning up unused symbols.
561 enum SymbolStatus {
562 NotProcessed,
563 HaveMarkedDependents
564 };
565
566 using SymbolSetTy = llvm::DenseSet<SymbolRef>;
567 using SymbolMapTy = llvm::DenseMap<SymbolRef, SymbolStatus>;
568 using RegionSetTy = llvm::DenseSet<const MemRegion *>;
569
570 SymbolMapTy TheLiving;
571 SymbolSetTy MetadataInUse;
572
573 RegionSetTy LiveRegionRoots;
574 // The lazily copied regions are locations for which a program
575 // can access the value stored at that location, but not its address.
576 // These regions are constructed as a set of regions referred to by
577 // lazyCompoundVal.
578 RegionSetTy LazilyCopiedRegionRoots;
579
580 const StackFrame *SF;
581 const Stmt *Loc;
582 SymbolManager& SymMgr;
583 StoreRef reapedStore;
584 llvm::DenseMap<const MemRegion *, unsigned> includedRegionCache;
585
586public:
587 /// Construct a reaper object, which removes everything which is not
588 /// live before we execute statements in the given stack frame.
589 ///
590 /// If the statement is NULL, everything in this and parent stack frames are
591 /// considered live.
592 /// If the stack frame is NULL, everything on stack is considered dead.
593 SymbolReaper(const StackFrame *SF, const Stmt *s, SymbolManager &symmgr,
594 StoreManager &storeMgr)
595 : SF(SF), Loc(s), SymMgr(symmgr), reapedStore(nullptr, storeMgr) {}
596
597 /// It might return null.
598 const StackFrame *getStackFrame() const { return SF; }
599
600 bool isLive(SymbolRef sym);
601 bool isLiveRegion(const MemRegion *region);
602 bool isLive(const Expr *ExprVal, const StackFrame *SF) const;
603 bool isLive(const VarRegion *VR, bool includeStoreBindings = false) const;
604
605 /// Unconditionally marks a symbol as live.
606 ///
607 /// This should never be
608 /// used by checkers, only by the state infrastructure such as the store and
609 /// environment. Checkers should instead use metadata symbols and markInUse.
610 void markLive(SymbolRef sym);
611
612 /// Marks a symbol as important to a checker.
613 ///
614 /// For metadata symbols,
615 /// this will keep the symbol alive as long as its associated region is also
616 /// live. For other symbols, this has no effect; checkers are not permitted
617 /// to influence the life of other symbols. This should be used before any
618 /// symbol marking has occurred, i.e. in the MarkLiveSymbols callback.
619 void markInUse(SymbolRef sym);
620
621 llvm::iterator_range<RegionSetTy::const_iterator> regions() const {
622 return LiveRegionRoots;
623 }
624
625 /// Returns whether or not a symbol has been confirmed dead.
626 ///
627 /// This should only be called once all marking of dead symbols has completed.
628 /// (For checkers, this means only in the checkDeadSymbols callback.)
629 bool isDead(SymbolRef sym) {
630 return !isLive(sym);
631 }
632
633 void markLive(const MemRegion *region);
634 void markLazilyCopied(const MemRegion *region);
635 void markElementIndicesLive(const MemRegion *region);
636
637 /// Set to the value of the symbolic store after
638 /// StoreManager::removeDeadBindings has been called.
639 void setReapedStore(StoreRef st) { reapedStore = st; }
640
641private:
642 bool isLazilyCopiedRegion(const MemRegion *region) const;
643 // A readable region is a region that live or lazily copied.
644 // Any symbols that refer to values in regions are alive if the region
645 // is readable.
646 bool isReadableRegion(const MemRegion *region);
647
648 /// Mark the symbols dependent on the input symbol as live.
649 void markDependentsLive(SymbolRef sym);
650};
651
653protected:
654 ~SymbolVisitor() = default;
655
656public:
657 SymbolVisitor() = default;
658 SymbolVisitor(const SymbolVisitor &) = default;
660
661 // The copy and move assignment operator is defined as deleted pending further
662 // motivation.
665
666 /// A visitor method invoked by ProgramStateManager::scanReachableSymbols.
667 ///
668 /// The method returns \c true if symbols should continue be scanned and \c
669 /// false otherwise.
670 virtual bool VisitSymbol(SymbolRef sym) = 0;
671 virtual bool VisitMemRegion(const MemRegion *) { return true; }
672};
673
674template <typename T, typename... Args>
675const T *SymbolManager::acquire(Args &&...args) {
676 llvm::FoldingSetNodeID profile;
677 T::Profile(profile, args...);
678 void *InsertPos;
679 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
680 if (!SD) {
681 SD = Alloc.make<T>(std::forward<Args>(args)...);
682 DataSet.InsertNode(SD, InsertPos);
683 }
684 return cast<T>(SD);
685}
686
687} // namespace ento
688
689} // namespace clang
690
691// Override the default definition that would use pointer values of SymbolRefs
692// to order them, which is unstable due to ASLR.
693// Use the SymbolID instead which reflect the order in which the symbols were
694// allocated. This is usually stable across runs leading to the stability of
695// ConstraintMap and other containers using SymbolRef as keys.
696template <>
697struct llvm::ImutContainerInfo<clang::ento::SymbolRef>
698 : public ImutProfileInfo<clang::ento::SymbolRef> {
705
706 static key_type_ref KeyOfValue(value_type_ref D) { return D; }
707 static data_type_ref DataOfValue(value_type_ref) { return true; }
708
710 return LHS->getSymbolID() == RHS->getSymbolID();
711 }
712
714 return LHS->getSymbolID() < RHS->getSymbolID();
715 }
716
717 // This might seem redundant, but it is required because of the way
718 // ImmutableSet is implemented through AVLTree:
719 // same as ImmutableMap, but with a non-informative "data".
720 static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
721};
722
723#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
BinaryOperatorKind Opcode
Definition Expr.h:4046
This represents one expression.
Definition Expr.h:112
A (possibly-)qualified type.
Definition TypeBase.h:937
It represents a stack frame of the call stack.
Stmt - This represents one statement.
Definition Stmt.h:86
UnaryOperatorKind Opcode
Definition Expr.h:2261
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition APSIntPtr.h:19
Template implementation for all binary symbolic expressions.
static constexpr bool classof(Kind K)
unsigned computeComplexity() const override
static bool classof(const SymExpr *SE)
void Profile(llvm::FoldingSetNodeID &ID) override
static void Profile(llvm::FoldingSetNodeID &ID, LHSTYPE lhs, BinaryOperator::Opcode op, RHSTYPE rhs, QualType t)
void dumpToStream(raw_ostream &os) const override
QualType getType() const override
BinaryOperator::Opcode getOpcode() const
static unsigned computeOperandComplexity(const SymExpr *Value)
static void dumpToStreamImpl(raw_ostream &os, const SymExpr *Value)
static bool classof(const SymExpr *SE)
static unsigned computeOperandComplexity(const llvm::APSInt &Value)
BinarySymExpr(SymbolID Sym, Kind k, BinaryOperator::Opcode op, QualType t)
static constexpr bool classof(Kind K)
static const llvm::APSInt * getPointer(APSIntPtr Value)
static const SymExpr * getPointer(const SymExpr *Value)
static bool isLocType(QualType T)
Definition SVals.h:262
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:473
SymT * make(ArgsT &&...Args)
SymExprAllocator(llvm::BumpPtrAllocator &Alloc)
Symbolic value.
Definition SymExpr.h:32
static bool isValidTypeForSymbol(QualType T)
Definition SymExpr.h:58
SymExpr(Kind k, SymbolID Sym)
Definition SymExpr.h:56
virtual QualType getType() const =0
Kind getKind() const
Definition SymExpr.h:69
SymbolID getSymbolID() const
Get a unique identifier for this symbol.
Definition SymExpr.h:77
unsigned Complexity
Definition SymExpr.h:64
static bool classof(const SymExpr *SE)
static void Profile(llvm::FoldingSetNodeID &ID, const SymExpr *In, QualType From, QualType To)
QualType getType() const override
void Profile(llvm::FoldingSetNodeID &ID) override
void dumpToStream(raw_ostream &os) const override
static constexpr Kind ClassKind
LLVM_ATTRIBUTE_RETURNS_NONNULL const SymExpr * getOperand() const
friend class SymExprAllocator
static constexpr bool classof(Kind K)
unsigned computeComplexity() const override
A symbol representing the result of an expression in the case when we do not know anything about what...
void Profile(llvm::FoldingSetNodeID &profile) override
const Stmt * getStmt() const
static bool classof(const SymExpr *SE)
static void Profile(llvm::FoldingSetNodeID &profile, ConstCFGElementRef Elem, const StackFrame *SF, QualType T, unsigned Count, const void *SymbolTag)
StringRef getKindStr() const override
Get a string representation of the kind of the region.
ConstCFGElementRef getCFGElementRef() const
static constexpr Kind ClassKind
const void * getTag() const
It might return null.
static constexpr bool classof(Kind K)
void dumpToStream(raw_ostream &os) const override
QualType getType() const override
SymbolData(Kind k, SymbolID sym)
Definition SymExpr.h:142
LLVM_ATTRIBUTE_RETURNS_NONNULL SymbolRef getParentSymbol() const
StringRef getKindStr() const override
Get a string representation of the kind of the region.
void dumpToStream(raw_ostream &os) const override
const MemRegion * getOriginRegion() const override
Find the region from which this symbol originates.
static constexpr bool classof(Kind K)
static void Profile(llvm::FoldingSetNodeID &profile, SymbolRef parent, const TypedValueRegion *r)
void Profile(llvm::FoldingSetNodeID &profile) override
QualType getType() const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
static constexpr Kind ClassKind
static bool classof(const SymExpr *SE)
static bool classof(const SymExpr *SE)
LLVM_ATTRIBUTE_RETURNS_NONNULL const SubRegion * getRegion() const
static constexpr bool classof(Kind K)
void dumpToStream(raw_ostream &os) const override
QualType getType() const override
static void Profile(llvm::FoldingSetNodeID &profile, const SubRegion *R)
StringRef getKindStr() const override
Get a string representation of the kind of the region.
void Profile(llvm::FoldingSetNodeID &profile) override
static constexpr Kind ClassKind
SymbolManager(ASTContext &ctx, BasicValueFactory &bv, llvm::BumpPtrAllocator &bpalloc)
const SymExprT * acquire(Args &&...args)
Create or retrieve a SymExpr of type SymExprT for the given arguments.
void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent)
Add artificial symbol dependency.
BasicValueFactory & getBasicVals()
QualType getType(const SymExpr *SE) const
const SymbolConjured * conjureSymbol(ConstCFGElementRef Elem, const StackFrame *SF, QualType T, unsigned VisitCount, const void *SymbolTag=nullptr)
const SymbolRefSmallVectorTy * getDependentSymbols(const SymbolRef Primary)
static bool canSymbolicate(QualType T)
void dumpToStream(raw_ostream &os) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getRegion() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const Stmt * getStmt() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const StackFrame * getStackFrame() const
StringRef getKindStr() const override
Get a string representation of the kind of the region.
QualType getType() const override
static bool classof(const SymExpr *SE)
void Profile(llvm::FoldingSetNodeID &profile) override
static constexpr bool classof(Kind K)
LLVM_ATTRIBUTE_RETURNS_NONNULL const void * getTag() const
static constexpr Kind ClassKind
static void Profile(llvm::FoldingSetNodeID &profile, const MemRegion *R, const Stmt *S, QualType T, const StackFrame *SF, unsigned Count, const void *Tag)
const StackFrame * getStackFrame() const
It might return null.
void markLive(SymbolRef sym)
Unconditionally marks a symbol as live.
void markElementIndicesLive(const MemRegion *region)
bool isDead(SymbolRef sym)
Returns whether or not a symbol has been confirmed dead.
void markInUse(SymbolRef sym)
Marks a symbol as important to a checker.
bool isLiveRegion(const MemRegion *region)
void markLazilyCopied(const MemRegion *region)
SymbolReaper(const StackFrame *SF, const Stmt *s, SymbolManager &symmgr, StoreManager &storeMgr)
Construct a reaper object, which removes everything which is not live before we execute statements in...
void setReapedStore(StoreRef st)
Set to the value of the symbolic store after StoreManager::removeDeadBindings has been called.
bool isLive(SymbolRef sym)
llvm::iterator_range< RegionSetTy::const_iterator > regions() const
void dumpToStream(raw_ostream &os) const override
const MemRegion * getOriginRegion() const override
Find the region from which this symbol originates.
void Profile(llvm::FoldingSetNodeID &profile) override
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
static void Profile(llvm::FoldingSetNodeID &profile, const TypedValueRegion *R)
QualType getType() const override
StringRef getKindStr() const override
Get a string representation of the kind of the region.
static bool classof(const SymExpr *SE)
static constexpr Kind ClassKind
static constexpr bool classof(Kind K)
virtual bool VisitMemRegion(const MemRegion *)
SymbolVisitor(const SymbolVisitor &)=default
SymbolVisitor(SymbolVisitor &&)
SymbolVisitor & operator=(SymbolVisitor &&)=delete
SymbolVisitor & operator=(const SymbolVisitor &)=delete
virtual bool VisitSymbol(SymbolRef sym)=0
A visitor method invoked by ProgramStateManager::scanReachableSymbols.
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:562
virtual QualType getValueType() const =0
void dumpToStream(raw_ostream &os) const override
void Profile(llvm::FoldingSetNodeID &ID) override
static constexpr bool classof(Kind K)
QualType getType() const override
static constexpr Kind ClassKind
static bool classof(const SymExpr *SE)
UnaryOperator::Opcode getOpcode() const
unsigned computeComplexity() const override
static void Profile(llvm::FoldingSetNodeID &ID, const SymExpr *In, UnaryOperator::Opcode Op, QualType T)
const SymExpr * getOperand() const
SmallVector< SymbolRef, 2 > SymbolRefSmallVectorTy
Definition SymExpr.h:134
BinarySymExprImpl< APSIntPtr, const SymExpr *, SymExpr::Kind::IntSymExprKind > IntSymExpr
Represents a symbolic expression like 3 - 'x'.
const SymExpr * SymbolRef
Definition SymExpr.h:133
BinarySymExprImpl< const SymExpr *, const SymExpr *, SymExpr::Kind::SymSymExprKind > SymSymExpr
Represents a symbolic expression like 'x' + 'y'.
BinarySymExprImpl< const SymExpr *, APSIntPtr, SymExpr::Kind::SymIntExprKind > SymIntExpr
Represents a symbolic expression like 'x' + 3.
unsigned SymbolID
Definition SymExpr.h:28
The JSON file list parser is used to communicate input to InstallAPI.
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1248
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Dependent
Parse the block as a dependent block, which may be used in some template instantiations but not other...
Definition Parser.h:142
U cast(CodeGen::Address addr)
Definition Address.h:327
static key_type_ref KeyOfValue(value_type_ref D)
static data_type_ref DataOfValue(value_type_ref)
static bool isEqual(clang::ento::SymbolRef LHS, clang::ento::SymbolRef RHS)
static bool isLess(clang::ento::SymbolRef LHS, clang::ento::SymbolRef RHS)
static bool isDataEqual(data_type_ref, data_type_ref)