gecko-dev/build/clang-plugin/tests/TestCustomHeap.cpp
Ting-Yu Chou 4e68f1ffa7 Bug 1322465 part 8 - Use explicit/MOZ_IMPLICIT for the unary constructors in mfbt/. r=Ehsan
MozReview-Commit-ID: 2TpfrAaAIuu

--HG--
extra : rebase_source : 4fb5af1beb94c059cbadfaf27fe3949ae8b85efb
2016-12-16 15:57:15 +08:00

30 lines
936 B
C++

#define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
#ifndef MOZ_HEAP_ALLOCATOR
#define MOZ_HEAP_ALLOCATOR \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wgcc-compat\"") \
__attribute__((annotate("moz_heap_allocator"))) \
_Pragma("GCC diagnostic pop")
#endif
#include <stdlib.h>
#include <memory>
struct MOZ_NONHEAP_CLASS X {
};
void *operator new(size_t x, int qual) MOZ_HEAP_ALLOCATOR {
return ::operator new(x);
}
template <typename T>
T *customAlloc() MOZ_HEAP_ALLOCATOR {
T *arg = static_cast<T*>(malloc(sizeof(T)));
return new (arg) T();
}
void misuseX() {
X *foo = customAlloc<X>(); // expected-error {{variable of type 'X' is not valid on the heap}} expected-note {{value incorrectly allocated on the heap}}
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}}
}