|
|
|||
File indexing completed on 2026-05-10 08:36:24
0001 //===-- Arena.h -------------------------------*- C++ -------------------*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE__ARENA_H 0009 #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE__ARENA_H 0010 0011 #include "clang/Analysis/FlowSensitive/Formula.h" 0012 #include "clang/Analysis/FlowSensitive/StorageLocation.h" 0013 #include "clang/Analysis/FlowSensitive/Value.h" 0014 #include "llvm/ADT/StringRef.h" 0015 #include <vector> 0016 0017 namespace clang::dataflow { 0018 0019 /// The Arena owns the objects that model data within an analysis. 0020 /// For example, `Value`, `StorageLocation`, `Atom`, and `Formula`. 0021 class Arena { 0022 public: 0023 Arena() 0024 : True(Formula::create(Alloc, Formula::Literal, {}, 1)), 0025 False(Formula::create(Alloc, Formula::Literal, {}, 0)) {} 0026 Arena(const Arena &) = delete; 0027 Arena &operator=(const Arena &) = delete; 0028 0029 /// Creates a `T` (some subclass of `StorageLocation`), forwarding `args` to 0030 /// the constructor, and returns a reference to it. 0031 /// 0032 /// The `Arena` takes ownership of the created object. The object will be 0033 /// destroyed when the `Arena` is destroyed. 0034 template <typename T, typename... Args> 0035 std::enable_if_t<std::is_base_of<StorageLocation, T>::value, T &> 0036 create(Args &&...args) { 0037 // Note: If allocation of individual `StorageLocation`s turns out to be 0038 // costly, consider creating specializations of `create<T>` for commonly 0039 // used `StorageLocation` subclasses and make them use a `BumpPtrAllocator`. 0040 return *cast<T>( 0041 Locs.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)) 0042 .get()); 0043 } 0044 0045 /// Creates a `T` (some subclass of `Value`), forwarding `args` to the 0046 /// constructor, and returns a reference to it. 0047 /// 0048 /// The `Arena` takes ownership of the created object. The object will be 0049 /// destroyed when the `Arena` is destroyed. 0050 template <typename T, typename... Args> 0051 std::enable_if_t<std::is_base_of<Value, T>::value, T &> 0052 create(Args &&...args) { 0053 // Note: If allocation of individual `Value`s turns out to be costly, 0054 // consider creating specializations of `create<T>` for commonly used 0055 // `Value` subclasses and make them use a `BumpPtrAllocator`. 0056 return *cast<T>( 0057 Vals.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)) 0058 .get()); 0059 } 0060 0061 /// Creates a BoolValue wrapping a particular formula. 0062 /// 0063 /// Passing in the same formula will result in the same BoolValue. 0064 /// FIXME: Interning BoolValues but not other Values is inconsistent. 0065 /// Decide whether we want Value interning or not. 0066 BoolValue &makeBoolValue(const Formula &); 0067 0068 /// Creates a fresh atom and wraps in in an AtomicBoolValue. 0069 /// FIXME: For now, identical-address AtomicBoolValue <=> identical atom. 0070 /// Stop relying on pointer identity and remove this guarantee. 0071 AtomicBoolValue &makeAtomValue() { 0072 return cast<AtomicBoolValue>(makeBoolValue(makeAtomRef(makeAtom()))); 0073 } 0074 0075 /// Creates a fresh Top boolean value. 0076 TopBoolValue &makeTopValue() { 0077 // No need for deduplicating: there's no way to create aliasing Tops. 0078 return create<TopBoolValue>(makeAtomRef(makeAtom())); 0079 } 0080 0081 /// Returns a symbolic integer value that models an integer literal equal to 0082 /// `Value`. These literals are the same every time. 0083 /// Integer literals are not typed; the type is determined by the `Expr` that 0084 /// an integer literal is associated with. 0085 IntegerValue &makeIntLiteral(llvm::APInt Value); 0086 0087 // Factories for boolean formulas. 0088 // Formulas are interned: passing the same arguments return the same result. 0089 // For commutative operations like And/Or, interning ignores order. 0090 // Simplifications are applied: makeOr(X, X) => X, etc. 0091 0092 /// Returns a formula for the conjunction of `LHS` and `RHS`. 0093 const Formula &makeAnd(const Formula &LHS, const Formula &RHS); 0094 0095 /// Returns a formula for the disjunction of `LHS` and `RHS`. 0096 const Formula &makeOr(const Formula &LHS, const Formula &RHS); 0097 0098 /// Returns a formula for the negation of `Val`. 0099 const Formula &makeNot(const Formula &Val); 0100 0101 /// Returns a formula for `LHS => RHS`. 0102 const Formula &makeImplies(const Formula &LHS, const Formula &RHS); 0103 0104 /// Returns a formula for `LHS <=> RHS`. 0105 const Formula &makeEquals(const Formula &LHS, const Formula &RHS); 0106 0107 /// Returns a formula for the variable A. 0108 const Formula &makeAtomRef(Atom A); 0109 0110 /// Returns a formula for a literal true/false. 0111 const Formula &makeLiteral(bool Value) { return Value ? True : False; } 0112 0113 // Parses a formula from its textual representation. 0114 // This may refer to atoms that were not produced by makeAtom() yet! 0115 llvm::Expected<const Formula &> parseFormula(llvm::StringRef); 0116 0117 /// Returns a new atomic boolean variable, distinct from any other. 0118 Atom makeAtom() { return static_cast<Atom>(NextAtom++); }; 0119 0120 /// Creates a fresh flow condition and returns a token that identifies it. The 0121 /// token can be used to perform various operations on the flow condition such 0122 /// as adding constraints to it, forking it, joining it with another flow 0123 /// condition, or checking implications. 0124 Atom makeFlowConditionToken() { return makeAtom(); } 0125 0126 private: 0127 llvm::BumpPtrAllocator Alloc; 0128 0129 // Storage for the state of a program. 0130 std::vector<std::unique_ptr<StorageLocation>> Locs; 0131 std::vector<std::unique_ptr<Value>> Vals; 0132 0133 // Indices that are used to avoid recreating the same integer literals and 0134 // composite boolean values. 0135 llvm::DenseMap<llvm::APInt, IntegerValue *> IntegerLiterals; 0136 using FormulaPair = std::pair<const Formula *, const Formula *>; 0137 llvm::DenseMap<FormulaPair, const Formula *> Ands; 0138 llvm::DenseMap<FormulaPair, const Formula *> Ors; 0139 llvm::DenseMap<const Formula *, const Formula *> Nots; 0140 llvm::DenseMap<FormulaPair, const Formula *> Implies; 0141 llvm::DenseMap<FormulaPair, const Formula *> Equals; 0142 llvm::DenseMap<Atom, const Formula *> AtomRefs; 0143 0144 llvm::DenseMap<const Formula *, BoolValue *> FormulaValues; 0145 unsigned NextAtom = 0; 0146 0147 const Formula &True, &False; 0148 }; 0149 0150 } // namespace clang::dataflow 0151 0152 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE__ARENA_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|