1 //ProgramStateTrait.h - Partial implementations of ProgramStateTrait -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines partial implementations of template specializations of
11 // the class ProgramStateTrait<>. ProgramStateTrait<> is used by ProgramState
12 // to implement set/get methods for manipulating a ProgramState's
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATETRAIT_H
18 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATETRAIT_H
20 #include "llvm/ADT/ImmutableList.h"
21 #include "llvm/ADT/ImmutableMap.h"
22 #include "llvm/ADT/ImmutableSet.h"
23 #include "llvm/Support/Allocator.h"
29 template <typename T> struct ProgramStatePartialTrait;
31 /// Declares a program state trait for type \p Type called \p Name, and
32 /// introduce a type named \c NameTy.
33 /// The macro should not be used inside namespaces, or for traits that must
34 /// be accessible from more than one translation unit.
35 #define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type) \
38 using Name ## Ty = Type; \
43 struct ProgramStateTrait<Name> \
44 : public ProgramStatePartialTrait<Name ## Ty> { \
45 static void *GDMIndex() { static int Index; return &Index; } \
50 // Partial-specialization for ImmutableMap.
51 template <typename Key, typename Data, typename Info>
52 struct ProgramStatePartialTrait<llvm::ImmutableMap<Key, Data, Info>> {
53 using data_type = llvm::ImmutableMap<Key, Data, Info>;
54 using context_type = typename data_type::Factory &;
56 using value_type = Data;
57 using lookup_type = const value_type *;
59 static data_type MakeData(void *const *p) {
60 return p ? data_type((typename data_type::TreeTy *) *p)
64 static void *MakeVoidPtr(data_type B) {
68 static lookup_type Lookup(data_type B, key_type K) {
72 static data_type Set(data_type B, key_type K, value_type E,
74 return F.add(B, K, E);
77 static data_type Remove(data_type B, key_type K, context_type F) {
78 return F.remove(B, K);
81 static bool Contains(data_type B, key_type K) {
85 static context_type MakeContext(void *p) {
86 return *((typename data_type::Factory *) p);
89 static void *CreateContext(llvm::BumpPtrAllocator& Alloc) {
90 return new typename data_type::Factory(Alloc);
93 static void DeleteContext(void *Ctx) {
94 delete (typename data_type::Factory *) Ctx;
98 /// Helper for registering a map trait.
100 /// If the map type were written directly in the invocation of
101 /// REGISTER_TRAIT_WITH_PROGRAMSTATE, the comma in the template arguments
102 /// would be treated as a macro argument separator, which is wrong.
103 /// This allows the user to specify a map type in a way that the preprocessor
105 #define CLANG_ENTO_PROGRAMSTATE_MAP(Key, Value) llvm::ImmutableMap<Key, Value>
107 // Partial-specialization for ImmutableSet.
108 template <typename Key, typename Info>
109 struct ProgramStatePartialTrait<llvm::ImmutableSet<Key, Info>> {
110 using data_type = llvm::ImmutableSet<Key, Info>;
111 using context_type = typename data_type::Factory &;
112 using key_type = Key;
114 static data_type MakeData(void *const *p) {
115 return p ? data_type((typename data_type::TreeTy *) *p)
116 : data_type(nullptr);
119 static void *MakeVoidPtr(data_type B) {
123 static data_type Add(data_type B, key_type K, context_type F) {
127 static data_type Remove(data_type B, key_type K, context_type F) {
128 return F.remove(B, K);
131 static bool Contains(data_type B, key_type K) {
132 return B.contains(K);
135 static context_type MakeContext(void *p) {
136 return *((typename data_type::Factory *) p);
139 static void *CreateContext(llvm::BumpPtrAllocator &Alloc) {
140 return new typename data_type::Factory(Alloc);
143 static void DeleteContext(void *Ctx) {
144 delete (typename data_type::Factory *) Ctx;
148 // Partial-specialization for ImmutableList.
149 template <typename T>
150 struct ProgramStatePartialTrait<llvm::ImmutableList<T>> {
151 using data_type = llvm::ImmutableList<T>;
153 using context_type = typename data_type::Factory &;
155 static data_type Add(data_type L, key_type K, context_type F) {
159 static bool Contains(data_type L, key_type K) {
160 return L.contains(K);
163 static data_type MakeData(void *const *p) {
164 return p ? data_type((const llvm::ImmutableListImpl<T> *) *p)
165 : data_type(nullptr);
168 static void *MakeVoidPtr(data_type D) {
169 return const_cast<llvm::ImmutableListImpl<T> *>(D.getInternalPointer());
172 static context_type MakeContext(void *p) {
173 return *((typename data_type::Factory *) p);
176 static void *CreateContext(llvm::BumpPtrAllocator &Alloc) {
177 return new typename data_type::Factory(Alloc);
180 static void DeleteContext(void *Ctx) {
181 delete (typename data_type::Factory *) Ctx;
185 // Partial specialization for bool.
186 template <> struct ProgramStatePartialTrait<bool> {
187 using data_type = bool;
189 static data_type MakeData(void *const *p) {
190 return p ? (data_type) (uintptr_t) *p
194 static void *MakeVoidPtr(data_type d) {
195 return (void *) (uintptr_t) d;
199 // Partial specialization for unsigned.
200 template <> struct ProgramStatePartialTrait<unsigned> {
201 using data_type = unsigned;
203 static data_type MakeData(void *const *p) {
204 return p ? (data_type) (uintptr_t) *p
208 static void *MakeVoidPtr(data_type d) {
209 return (void *) (uintptr_t) d;
213 // Partial specialization for void*.
214 template <> struct ProgramStatePartialTrait<void *> {
215 using data_type = void *;
217 static data_type MakeData(void *const *p) {
222 static void *MakeVoidPtr(data_type d) {
227 // Partial specialization for const void *.
228 template <> struct ProgramStatePartialTrait<const void *> {
229 using data_type = const void *;
231 static data_type MakeData(void *const *p) {
232 return p ? *p : data_type();
235 static void *MakeVoidPtr(data_type d) {
236 return const_cast<void *>(d);
243 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATETRAIT_H