build/clang-plugin/tests/TestCustomHeap.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/build/clang-plugin/tests/TestCustomHeap.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,28 @@
     1.4 +#define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
     1.5 +#define MOZ_HEAP_ALLOCATOR \
     1.6 +  _Pragma("GCC diagnostic push") \
     1.7 +  _Pragma("GCC diagnostic ignored \"-Wgcc-compat\"") \
     1.8 +  __attribute__((annotate("moz_heap_allocator"))) \
     1.9 +  _Pragma("GCC diagnostic pop")
    1.10 +
    1.11 +#include <stdlib.h>
    1.12 +#include <memory>
    1.13 +
    1.14 +struct MOZ_NONHEAP_CLASS X {
    1.15 +};
    1.16 +
    1.17 +void *operator new(size_t x, int qual) MOZ_HEAP_ALLOCATOR {
    1.18 +  return ::operator new(x);
    1.19 +}
    1.20 +
    1.21 +template <typename T>
    1.22 +T *customAlloc() MOZ_HEAP_ALLOCATOR {
    1.23 +  T *arg =  static_cast<T*>(malloc(sizeof(T)));
    1.24 +  return new (arg) T();
    1.25 +}
    1.26 +
    1.27 +template <typename T>
    1.28 +void misuseX(T q) {
    1.29 +  X *foo = customAlloc<X>(); // expected-error {{variable of type 'X' is not valid on the heap}}
    1.30 +  X *foo2 = new (100) X(); // expected-error {{variable of type 'X' is not valid on the heap}}
    1.31 +}

mercurial