]> granicus.if.org Git - clang/blob - test/Analysis/Inputs/system-header-simulator-cxx.h
[analyzer] Add checker for iterators dereferenced beyond their range.
[clang] / test / Analysis / Inputs / system-header-simulator-cxx.h
1 // Like the compiler, the static analyzer treats some functions differently if
2 // they come from a system header -- for example, it is assumed that system
3 // functions do not arbitrarily free() their parameters, and that some bugs
4 // found in system headers cannot be fixed by the user and should be
5 // suppressed.
6 #pragma clang system_header
7
8 typedef unsigned char uint8_t;
9
10 typedef __typeof__(sizeof(int)) size_t;
11 void *memmove(void *s1, const void *s2, size_t n);
12
13 template <typename T, typename Ptr, typename Ref> struct __iterator {
14   typedef __iterator<T, T *, T &> iterator;
15   typedef __iterator<T, const T *, const T &> const_iterator;
16
17   __iterator(const Ptr p) : ptr(p) {}
18
19   __iterator<T, Ptr, Ref> operator++() { return *this; }
20   __iterator<T, Ptr, Ref> operator++(int) { return *this; }
21   __iterator<T, Ptr, Ref> operator--() { return *this; }
22   __iterator<T, Ptr, Ref> operator--(int) { return *this; }
23   Ref operator*() const { return *ptr; }
24   Ptr operator->() const { return *ptr; }
25
26   bool operator==(const iterator &rhs) const { return ptr == rhs.ptr; }
27   bool operator==(const const_iterator &rhs) const { return ptr == rhs.ptr; }
28
29   bool operator!=(const iterator &rhs) const { return ptr != rhs.ptr; }
30   bool operator!=(const const_iterator &rhs) const { return ptr != rhs.ptr; }
31
32 private:
33   Ptr ptr;
34 };
35
36 namespace std {
37   template <class T1, class T2>
38   struct pair {
39     T1 first;
40     T2 second;
41     
42     pair() : first(), second() {}
43     pair(const T1 &a, const T2 &b) : first(a), second(b) {}
44     
45     template<class U1, class U2>
46     pair(const pair<U1, U2> &other) : first(other.first), second(other.second) {}
47   };
48   
49   typedef __typeof__(sizeof(int)) size_t;
50   
51   template<typename T>
52   class vector {
53     typedef __iterator<T, T *, T &> iterator;
54     typedef __iterator<T, const T *, const T &> const_iterator;
55
56     T *_start;
57     T *_finish;
58     T *_end_of_storage;
59   public:
60     vector() : _start(0), _finish(0), _end_of_storage(0) {}
61     ~vector();
62     
63     size_t size() const {
64       return size_t(_finish - _start);
65     }
66     
67     void push_back();
68     T pop_back();
69
70     T &operator[](size_t n) {
71       return _start[n];
72     }
73     
74     const T &operator[](size_t n) const {
75       return _start[n];
76     }
77     
78     iterator begin() { return iterator(_start); }
79     const_iterator begin() const { return const_iterator(_start); }
80     iterator end() { return iterator(_finish); }
81     const_iterator end() const { return const_iterator(_finish); }
82   };
83   
84   class exception {
85   public:
86     exception() throw();
87     virtual ~exception() throw();
88     virtual const char *what() const throw() {
89       return 0;
90     }
91   };
92
93   class bad_alloc : public exception {
94     public:
95     bad_alloc() throw();
96     bad_alloc(const bad_alloc&) throw();
97     bad_alloc& operator=(const bad_alloc&) throw();
98     virtual const char* what() const throw() {
99       return 0;
100     }
101   };
102
103   struct nothrow_t {};
104
105   extern const nothrow_t nothrow;
106
107   // libc++'s implementation
108   template <class _E>
109   class initializer_list
110   {
111     const _E* __begin_;
112     size_t    __size_;
113
114     initializer_list(const _E* __b, size_t __s)
115       : __begin_(__b),
116         __size_(__s)
117     {}
118
119   public:
120     typedef _E        value_type;
121     typedef const _E& reference;
122     typedef const _E& const_reference;
123     typedef size_t    size_type;
124
125     typedef const _E* iterator;
126     typedef const _E* const_iterator;
127
128     initializer_list() : __begin_(0), __size_(0) {}
129
130     size_t    size()  const {return __size_;}
131     const _E* begin() const {return __begin_;}
132     const _E* end()   const {return __begin_ + __size_;}
133   };
134
135   template <bool, class _Tp = void> struct enable_if {};
136   template <class _Tp> struct enable_if<true, _Tp> {typedef _Tp type;};
137
138   template <class _Tp, _Tp __v>
139   struct integral_constant
140   {
141       static const _Tp      value = __v;
142       typedef _Tp               value_type;
143       typedef integral_constant type;
144
145      operator value_type() const {return value;}
146
147      value_type operator ()() const {return value;}
148   };
149
150   template <class _Tp, _Tp __v>
151   const _Tp integral_constant<_Tp, __v>::value;
152
153     template <class _Tp, class _Arg>
154     struct is_trivially_assignable
155       : integral_constant<bool, __is_trivially_assignable(_Tp, _Arg)>
156     {
157     };
158
159   typedef integral_constant<bool,true>  true_type;
160   typedef integral_constant<bool,false> false_type;
161
162   template <class _Tp> struct is_const            : public false_type {};
163   template <class _Tp> struct is_const<_Tp const> : public true_type {};
164
165   template <class _Tp> struct  is_reference        : public false_type {};
166   template <class _Tp> struct  is_reference<_Tp&>  : public true_type {};
167
168   template <class _Tp, class _Up> struct  is_same           : public false_type {};
169   template <class _Tp>            struct  is_same<_Tp, _Tp> : public true_type {};
170
171   template <class _Tp, bool = is_const<_Tp>::value || is_reference<_Tp>::value    >
172   struct __add_const             {typedef _Tp type;};
173
174   template <class _Tp>
175   struct __add_const<_Tp, false> {typedef const _Tp type;};
176
177   template <class _Tp> struct add_const {typedef typename __add_const<_Tp>::type type;};
178
179   template <class _Tp> struct  remove_const            {typedef _Tp type;};
180   template <class _Tp> struct  remove_const<const _Tp> {typedef _Tp type;};
181
182   template <class _Tp> struct  add_lvalue_reference    {typedef _Tp& type;};
183
184   template <class _Tp> struct is_trivially_copy_assignable
185       : public is_trivially_assignable<typename add_lvalue_reference<_Tp>::type,
186             typename add_lvalue_reference<typename add_const<_Tp>::type>::type> {};
187
188     template<class InputIter, class OutputIter>
189     OutputIter __copy(InputIter II, InputIter IE, OutputIter OI) {
190       while (II != IE)
191         *OI++ = *II++;
192
193       return OI;
194     }
195
196   template <class _Tp, class _Up>
197   inline
198   typename enable_if
199   <
200       is_same<typename remove_const<_Tp>::type, _Up>::value &&
201       is_trivially_copy_assignable<_Up>::value,
202       _Up*
203   >::type __copy(_Tp* __first, _Tp* __last, _Up* __result) {
204       size_t __n = __last - __first;
205
206       if (__n > 0)
207         memmove(__result, __first, __n * sizeof(_Up));
208
209       return __result + __n;
210     }
211
212   template<class InputIter, class OutputIter>
213   OutputIter copy(InputIter II, InputIter IE, OutputIter OI) {
214     return __copy(II, IE, OI);
215   }
216
217   template <class _BidirectionalIterator, class _OutputIterator>
218   inline
219   _OutputIterator
220   __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last,
221                   _OutputIterator __result)
222   {
223       while (__first != __last)
224           *--__result = *--__last;
225       return __result;
226   }
227
228   template <class _Tp, class _Up>
229   inline
230   typename enable_if
231   <
232       is_same<typename remove_const<_Tp>::type, _Up>::value &&
233       is_trivially_copy_assignable<_Up>::value,
234       _Up*
235   >::type __copy_backward(_Tp* __first, _Tp* __last, _Up* __result) {
236       size_t __n = __last - __first;
237
238     if (__n > 0)
239     {
240         __result -= __n;
241         memmove(__result, __first, __n * sizeof(_Up));
242     }
243     return __result;
244   }
245
246   template<class InputIter, class OutputIter>
247   OutputIter copy_backward(InputIter II, InputIter IE, OutputIter OI) {
248     return __copy_backward(II, IE, OI);
249   }
250
251   template <class InputIterator, class T>
252   InputIterator find(InputIterator first, InputIterator last, const T &val);
253   template <class ForwardIterator1, class ForwardIterator2>
254   ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
255                             ForwardIterator2 first2, ForwardIterator2 last2);
256   template <class ForwardIterator1, class ForwardIterator2>
257   ForwardIterator1 find_first_of(ForwardIterator1 first1,
258                                  ForwardIterator1 last1,
259                                  ForwardIterator2 first2,
260                                  ForwardIterator2 last2);
261   template <class InputIterator, class UnaryPredicate>
262   InputIterator find_if(InputIterator first, InputIterator last,
263                         UnaryPredicate pred);
264   template <class InputIterator, class UnaryPredicate>
265   InputIterator find_if_not(InputIterator first, InputIterator last,
266                             UnaryPredicate pred);
267   template <class InputIterator, class T>
268   InputIterator lower_bound(InputIterator first, InputIterator last,
269                             const T &val);
270   template <class InputIterator, class T>
271   InputIterator upper_bound(InputIterator first, InputIterator last,
272                             const T &val);
273   template <class ForwardIterator1, class ForwardIterator2>
274   ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
275                           ForwardIterator2 first2, ForwardIterator2 last2);
276   template <class ForwardIterator1, class ForwardIterator2>
277   ForwardIterator1 search_n(ForwardIterator1 first1, ForwardIterator1 last1,
278                             ForwardIterator2 first2, ForwardIterator2 last2);
279
280   struct input_iterator_tag { };
281   struct output_iterator_tag { };
282   struct forward_iterator_tag : public input_iterator_tag { };
283   struct bidirectional_iterator_tag : public forward_iterator_tag { };
284   struct random_access_iterator_tag : public bidirectional_iterator_tag { };
285
286 }
287
288 void* operator new(std::size_t, const std::nothrow_t&) throw();
289 void* operator new[](std::size_t, const std::nothrow_t&) throw();
290 void operator delete(void*, const std::nothrow_t&) throw();
291 void operator delete[](void*, const std::nothrow_t&) throw();
292
293 void* operator new (std::size_t size, void* ptr) throw() { return ptr; };
294 void* operator new[] (std::size_t size, void* ptr) throw() { return ptr; };
295 void operator delete (void* ptr, void*) throw() {};
296 void operator delete[] (void* ptr, void*) throw() {};
297
298 namespace __cxxabiv1 {
299 extern "C" {
300 extern char *__cxa_demangle(const char *mangled_name,
301                             char *output_buffer,
302                             size_t *length,
303                             int *status);
304 }}
305 namespace abi = __cxxabiv1;