Debian Codegraph @

build/clang-plugin/tests/TestCustomHeap.cpp

raw
1 #define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
2 #ifndef MOZ_HEAP_ALLOCATOR
3 #define MOZ_HEAP_ALLOCATOR \
4 _Pragma("GCC diagnostic push") \
5 _Pragma("GCC diagnostic ignored \"-Wgcc-compat\"") \
6 __attribute__((annotate("moz_heap_allocator"))) \
7 _Pragma("GCC diagnostic pop")
8 #endif
9
10 #include <stdlib.h>
11 #include <memory>
12
13 struct MOZ_NONHEAP_CLASS X {
14 };
15
16 void *operator new(size_t x, int qual) MOZ_HEAP_ALLOCATOR {
17 return ::operator new(x);
18 }
19
20 template <typename T>
21 T *customAlloc() MOZ_HEAP_ALLOCATOR {
22 T *arg = static_cast<T*>(malloc(sizeof(T)));
23 return new (arg) T();
24 }
25
26 void misuseX() {
27 X *foo = customAlloc<X>(); // expected-error {{variable of type 'X' is not valid on the heap}} expected-note {{value incorrectly allocated on the heap}}
28 X *foo2 = new (100) X(); // expected-error {{variable of type 'X' is not valid on the heap}} expected-note {{value incorrectly allocated on the heap}}
29 }