1 //===- ThreadSafetyUtil.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines some basic utility classes for use by ThreadSafetyTIL.h
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
14 #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/Support/Allocator.h"
33 namespace threadSafety {
36 // Simple wrapper class to abstract away from the details of memory management.
37 // SExprs are allocated in pools, and deallocated all at once.
48 MemRegionRef() = default;
49 MemRegionRef(llvm::BumpPtrAllocator *A) : Allocator(A) {}
51 void *allocate(size_t Sz) {
52 return Allocator->Allocate(Sz, alignof(AlignmentType));
55 template <typename T> T *allocateT() { return Allocator->Allocate<T>(); }
57 template <typename T> T *allocateT(size_t NumElems) {
58 return Allocator->Allocate<T>(NumElems);
62 llvm::BumpPtrAllocator *Allocator = nullptr;
66 } // namespace threadSafety
70 inline void *operator new(size_t Sz,
71 clang::threadSafety::til::MemRegionRef &R) {
72 return R.allocate(Sz);
76 namespace threadSafety {
78 std::string getSourceLiteralString(const Expr *CE);
82 // A simple fixed size array class that does not manage its own memory,
83 // suitable for use with bump pointer allocation.
84 template <class T> class SimpleArray {
86 SimpleArray() = default;
87 SimpleArray(T *Dat, size_t Cp, size_t Sz = 0)
88 : Data(Dat), Size(Sz), Capacity(Cp) {}
89 SimpleArray(MemRegionRef A, size_t Cp)
90 : Data(Cp == 0 ? nullptr : A.allocateT<T>(Cp)), Capacity(Cp) {}
91 SimpleArray(const SimpleArray<T> &A) = delete;
93 SimpleArray(SimpleArray<T> &&A)
94 : Data(A.Data), Size(A.Size), Capacity(A.Capacity) {
100 SimpleArray &operator=(SimpleArray &&RHS) {
104 Capacity = RHS.Capacity;
107 RHS.Size = RHS.Capacity = 0;
112 // Reserve space for at least Ncp items, reallocating if necessary.
113 void reserve(size_t Ncp, MemRegionRef A) {
117 Data = A.allocateT<T>(Ncp);
119 memcpy(Data, Odata, sizeof(T) * Size);
122 // Reserve space for at least N more items.
123 void reserveCheck(size_t N, MemRegionRef A) {
125 reserve(u_max(InitialCapacity, N), A);
126 else if (Size + N < Capacity)
127 reserve(u_max(Size + N, Capacity * 2), A);
130 using iterator = T *;
131 using const_iterator = const T *;
132 using reverse_iterator = std::reverse_iterator<iterator>;
133 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
135 size_t size() const { return Size; }
136 size_t capacity() const { return Capacity; }
138 T &operator[](unsigned i) {
139 assert(i < Size && "Array index out of bounds.");
143 const T &operator[](unsigned i) const {
144 assert(i < Size && "Array index out of bounds.");
149 assert(Size && "No elements in the array.");
150 return Data[Size - 1];
153 const T &back() const {
154 assert(Size && "No elements in the array.");
155 return Data[Size - 1];
158 iterator begin() { return Data; }
159 iterator end() { return Data + Size; }
161 const_iterator begin() const { return Data; }
162 const_iterator end() const { return Data + Size; }
164 const_iterator cbegin() const { return Data; }
165 const_iterator cend() const { return Data + Size; }
167 reverse_iterator rbegin() { return reverse_iterator(end()); }
168 reverse_iterator rend() { return reverse_iterator(begin()); }
170 const_reverse_iterator rbegin() const {
171 return const_reverse_iterator(end());
174 const_reverse_iterator rend() const {
175 return const_reverse_iterator(begin());
178 void push_back(const T &Elem) {
179 assert(Size < Capacity);
183 // drop last n elements from array
184 void drop(unsigned n = 0) {
189 void setValues(unsigned Sz, const T& C) {
190 assert(Sz <= Capacity);
192 for (unsigned i = 0; i < Sz; ++i) {
197 template <class Iter> unsigned append(Iter I, Iter E) {
200 for (; J < Capacity && I != E; ++J, ++I)
206 llvm::iterator_range<reverse_iterator> reverse() {
207 return llvm::make_range(rbegin(), rend());
210 llvm::iterator_range<const_reverse_iterator> reverse() const {
211 return llvm::make_range(rbegin(), rend());
215 // std::max is annoying here, because it requires a reference,
216 // thus forcing InitialCapacity to be initialized outside the .h file.
217 size_t u_max(size_t i, size_t j) { return (i < j) ? j : i; }
219 static const size_t InitialCapacity = 4;
228 // A copy on write vector.
229 // The vector can be in one of three states:
230 // * invalid -- no operations are permitted.
231 // * read-only -- read operations are permitted.
232 // * writable -- read and write operations are permitted.
233 // The init(), destroy(), and makeWritable() methods will change state.
235 class CopyOnWriteVector {
238 unsigned NumRefs = 1;
241 VectorData() = default;
242 VectorData(const VectorData &VD) : Vect(VD.Vect) {}
246 CopyOnWriteVector() = default;
247 CopyOnWriteVector(CopyOnWriteVector &&V) : Data(V.Data) { V.Data = nullptr; }
249 CopyOnWriteVector &operator=(CopyOnWriteVector &&V) {
256 // No copy constructor or copy assignment. Use clone() with move assignment.
257 CopyOnWriteVector(const CopyOnWriteVector &) = delete;
258 CopyOnWriteVector &operator=(const CopyOnWriteVector &) = delete;
260 ~CopyOnWriteVector() { destroy(); }
262 // Returns true if this holds a valid vector.
263 bool valid() const { return Data; }
265 // Returns true if this vector is writable.
266 bool writable() const { return Data && Data->NumRefs == 1; }
268 // If this vector is not valid, initialize it to a valid vector.
271 Data = new VectorData();
275 // Destroy this vector; thus making it invalid.
279 if (Data->NumRefs <= 1)
286 // Make this vector writable, creating a copy if needed.
287 void makeWritable() {
289 Data = new VectorData();
292 if (Data->NumRefs == 1)
293 return; // already writeable.
295 Data = new VectorData(*Data);
298 // Create a lazy copy of this vector.
299 CopyOnWriteVector clone() { return CopyOnWriteVector(Data); }
301 using const_iterator = typename std::vector<T>::const_iterator;
303 const std::vector<T> &elements() const { return Data->Vect; }
305 const_iterator begin() const { return elements().cbegin(); }
306 const_iterator end() const { return elements().cend(); }
308 const T& operator[](unsigned i) const { return elements()[i]; }
310 unsigned size() const { return Data ? elements().size() : 0; }
312 // Return true if V and this vector refer to the same data.
313 bool sameAs(const CopyOnWriteVector &V) const { return Data == V.Data; }
315 // Clear vector. The vector must be writable.
317 assert(writable() && "Vector is not writable!");
321 // Push a new element onto the end. The vector must be writable.
322 void push_back(const T &Elem) {
323 assert(writable() && "Vector is not writable!");
324 Data->Vect.push_back(Elem);
327 // Gets a mutable reference to the element at index(i).
328 // The vector must be writable.
329 T& elem(unsigned i) {
330 assert(writable() && "Vector is not writable!");
331 return Data->Vect[i];
334 // Drops elements from the back until the vector has size i.
335 void downsize(unsigned i) {
336 assert(writable() && "Vector is not writable!");
337 Data->Vect.erase(Data->Vect.begin() + i, Data->Vect.end());
341 CopyOnWriteVector(VectorData *D) : Data(D) {
347 VectorData *Data = nullptr;
350 inline std::ostream& operator<<(std::ostream& ss, const StringRef str) {
351 return ss.write(str.data(), str.size());
354 } // namespace threadSafety
357 #endif // LLVM_CLANG_THREAD_SAFETY_UTIL_H