xref: /llvm-project/clang/test/Analysis/Checkers/WebKit/mock-types.h (revision d3c4637cbbd5f0a84811abe195098ce714a2cc32)
1 #ifndef mock_types_1103988513531
2 #define mock_types_1103988513531
3 
4 namespace std {
5 
6 template <typename T>
7 class unique_ptr {
8 private:
9   T *t;
10 
11 public:
12   unique_ptr() : t(nullptr) { }
13   unique_ptr(T *t) : t(t) { }
14   ~unique_ptr() {
15     if (t)
16       delete t;
17   }
18   template <typename U> unique_ptr(unique_ptr<U>&& u)
19     : t(u.t)
20   {
21     u.t = nullptr;
22   }
23   T *get() const { return t; }
24   T *operator->() const { return t; }
25   T &operator*() const { return *t; }
26   unique_ptr &operator=(T *) { return *this; }
27   explicit operator bool() const { return !!t; }
28 };
29 
30 };
31 
32 template<typename T>
33 struct RawPtrTraits {
34   using StorageType = T*;
35 
36   template<typename U>
37   static T* exchange(StorageType& ptr, U&& newValue)
38   {
39     StorageType oldValue = static_cast<StorageType&&>(ptr);
40     ptr = static_cast<U&&>(newValue);
41     return oldValue;
42   }
43 
44   static void swap(StorageType& a, StorageType& b)
45   {
46     StorageType temp = static_cast<StorageType&&>(a);
47     a = static_cast<StorageType&&>(b);
48     b = static_cast<StorageType&&>(temp);
49   }
50   static T* unwrap(const StorageType& ptr) { return ptr; }
51 };
52 
53 template<typename T> struct DefaultRefDerefTraits {
54   static T* refIfNotNull(T* ptr)
55   {
56     if (ptr)
57       ptr->ref();
58     return ptr;
59   }
60 
61   static T& ref(T& ref)
62   {
63     ref.ref();
64     return ref;
65   }
66 
67   static void derefIfNotNull(T* ptr)
68   {
69     if (ptr)
70       ptr->deref();
71   }
72 };
73 
74 template <typename T, typename PtrTraits = RawPtrTraits<T>, typename RefDerefTraits = DefaultRefDerefTraits<T>> struct Ref {
75   typename PtrTraits::StorageType t;
76 
77   enum AdoptTag { Adopt };
78 
79   Ref() : t{} {};
80   Ref(T &t, AdoptTag) : t(&t) { }
81   Ref(T &t) : t(&RefDerefTraits::ref(t)) { }
82   Ref(const Ref& o) : t(RefDerefTraits::refIfNotNull(PtrTraits::unwrap(o.t))) { }
83   Ref(Ref&& o) : t(o.leakRef()) { }
84   ~Ref() { RefDerefTraits::derefIfNotNull(PtrTraits::exchange(t, nullptr)); }
85   Ref& operator=(T &t) {
86     Ref o(t);
87     swap(o);
88     return *this;
89   }
90   Ref& operator=(Ref &&o) {
91     Ref m(o);
92     swap(m);
93     return *this;
94   }
95   void swap(Ref& o) {
96     typename PtrTraits::StorageType tmp = t;
97     t = o.t;
98     o.t = tmp;
99   }
100   T &get() const { return *PtrTraits::unwrap(t); }
101   T *ptr() const { return PtrTraits::unwrap(t); }
102   T *operator->() const { return PtrTraits::unwrap(t); }
103   operator T &() const { return *PtrTraits::unwrap(t); }
104   T* leakRef() { return PtrTraits::exchange(t, nullptr); }
105 };
106 
107 template <typename T> Ref<T> adoptRef(T& t) {
108   using Ref = Ref<T>;
109   return Ref(t, Ref::Adopt);
110 }
111 
112 template<typename T> class RefPtr;
113 template<typename T> RefPtr<T> adoptRef(T*);
114 
115 template <typename T> struct RefPtr {
116   T *t;
117 
118   RefPtr() : t(nullptr) { }
119 
120   RefPtr(T *t)
121     : t(t) {
122     if (t)
123       t->ref();
124   }
125   RefPtr(Ref<T>&& o)
126     : t(o.leakRef())
127   { }
128   RefPtr(RefPtr&& o)
129     : t(o.t)
130   {
131     o.t = nullptr;
132   }
133   RefPtr(const RefPtr& o)
134     : t(o.t)
135   {
136     if (t)
137       t->ref();
138   }
139   RefPtr operator=(const RefPtr& o)
140   {
141     if (t)
142       t->deref();
143     t = o.t;
144     if (t)
145       t->ref();
146     return *this;
147   }
148   ~RefPtr() {
149     if (t)
150       t->deref();
151   }
152   Ref<T> releaseNonNull() {
153     Ref<T> tmp(*t);
154     if (t)
155       t->deref();
156     t = nullptr;
157     return tmp;
158   }
159   void swap(RefPtr& o) {
160     T* tmp = t;
161     t = o.t;
162     o.t = tmp;
163   }
164   T *get() const { return t; }
165   T *operator->() const { return t; }
166   T &operator*() const { return *t; }
167   RefPtr &operator=(T *t) {
168     RefPtr o(t);
169     swap(o);
170     return *this;
171   }
172   operator bool() const { return t; }
173 
174 private:
175   friend RefPtr adoptRef<T>(T*);
176 
177   // call_with_adopt_ref in call-args.cpp requires this method to be private.
178   enum AdoptTag { Adopt };
179   RefPtr(T *t, AdoptTag) : t(t) { }
180 };
181 
182 template <typename T> RefPtr<T> adoptRef(T* t) {
183   return RefPtr<T>(t, RefPtr<T>::Adopt);
184 }
185 
186 template <typename T> bool operator==(const RefPtr<T> &, const RefPtr<T> &) {
187   return false;
188 }
189 
190 template <typename T> bool operator==(const RefPtr<T> &, T *) { return false; }
191 
192 template <typename T> bool operator==(const RefPtr<T> &, T &) { return false; }
193 
194 template <typename T> bool operator!=(const RefPtr<T> &, const RefPtr<T> &) {
195   return false;
196 }
197 
198 template <typename T> bool operator!=(const RefPtr<T> &, T *) { return false; }
199 
200 template <typename T> bool operator!=(const RefPtr<T> &, T &) { return false; }
201 
202 struct RefCountable {
203   static Ref<RefCountable> create();
204   static std::unique_ptr<RefCountable> makeUnique();
205   void ref() {}
206   void deref() {}
207   void method();
208   void constMethod() const;
209   int trivial() { return 123; }
210   RefCountable* next();
211 };
212 
213 template <typename T> T *downcast(T *t) { return t; }
214 
215 template <typename T> struct CheckedRef {
216 private:
217   T *t;
218 
219 public:
220   CheckedRef() : t{} {};
221   CheckedRef(T &t) : t(&t) { t.incrementCheckedPtrCount(); }
222   CheckedRef(const CheckedRef &o) : t(o.t) { if (t) t->incrementCheckedPtrCount(); }
223   ~CheckedRef() { if (t) t->decrementCheckedPtrCount(); }
224   T &get() const { return *t; }
225   T *ptr() const { return t; }
226   T *operator->() const { return t; }
227   operator const T &() const { return *t; }
228   operator T &() { return *t; }
229 };
230 
231 template <typename T> struct CheckedPtr {
232 private:
233   T *t;
234 
235 public:
236   CheckedPtr() : t(nullptr) {}
237   CheckedPtr(T *t)
238     : t(t) {
239     if (t)
240       t->incrementCheckedPtrCount();
241   }
242   CheckedPtr(Ref<T> &&o)
243     : t(o.leakRef())
244   { }
245   ~CheckedPtr() {
246     if (t)
247       t->decrementCheckedPtrCount();
248   }
249   T *get() const { return t; }
250   T *operator->() const { return t; }
251   T &operator*() const { return *t; }
252   CheckedPtr &operator=(T *) { return *this; }
253   operator bool() const { return t; }
254 };
255 
256 class CheckedObj {
257 public:
258   void incrementCheckedPtrCount();
259   void decrementCheckedPtrCount();
260   void method();
261   int trivial() { return 123; }
262   CheckedObj* next();
263 };
264 
265 class RefCountableAndCheckable {
266 public:
267   void incrementCheckedPtrCount() const;
268   void decrementCheckedPtrCount() const;
269   void ref() const;
270   void deref() const;
271   void method();
272   int trivial() { return 0; }
273 };
274 
275 template <typename T>
276 class UniqueRef {
277 private:
278   T *t;
279 
280 public:
281   UniqueRef(T &t) : t(&t) { }
282   ~UniqueRef() {
283     if (t)
284       delete t;
285   }
286   template <typename U> UniqueRef(UniqueRef<U>&& u)
287     : t(u.t)
288   {
289     u.t = nullptr;
290   }
291   T &get() const { return *t; }
292   T *operator->() const { return t; }
293   UniqueRef &operator=(T &) { return *this; }
294 };
295 
296 #endif
297