1 #define MOZ_HEAP_CLASS __attribute__((annotate("moz_heap_class")))
2 #define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
3 
4 #include <stddef.h>
5 
6 struct MOZ_HEAP_CLASS Heap {
7   int i;
HeapHeap8   Heap() {}
HeapHeap9   MOZ_IMPLICIT Heap(int a) {}
HeapHeap10   Heap(int a, int b) {}
operator newHeap11   void *operator new(size_t x) throw() { return 0; }
operator newHeap12   void *operator new(size_t blah, char *buffer) { return buffer; }
13 };
14 
15 template <class T>
16 struct MOZ_HEAP_CLASS TemplateClass {
17   T i;
18 };
19 
gobble(void *)20 void gobble(void *) { }
21 
gobbleref(const Heap &)22 void gobbleref(const Heap&) { }
23 
misuseHeapClass(int len)24 void misuseHeapClass(int len) {
25   Heap invalid; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in an automatic variable}}
26   Heap alsoInvalid[2]; // expected-error {{variable of type 'Heap [2]' only valid on the heap}} expected-note {{value incorrectly allocated in an automatic variable}} expected-note {{'Heap [2]' is a heap type because it is an array of heap type 'Heap'}}
27   static Heap invalidStatic; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}}
28   static Heap alsoInvalidStatic[2]; // expected-error {{variable of type 'Heap [2]' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}} expected-note {{'Heap [2]' is a heap type because it is an array of heap type 'Heap'}}
29 
30   gobble(&invalid);
31   gobble(&invalidStatic);
32   gobble(&alsoInvalid[0]);
33 
34   gobbleref(Heap()); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
35   gobbleref(Heap(10, 20)); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
36   gobbleref(Heap(10)); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
37   gobbleref(10); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
38 
39   gobble(new Heap);
40   gobble(new Heap[10]);
41   gobble(new TemplateClass<int>);
42   gobble(len <= 5 ? &invalid : new Heap);
43 
44   char buffer[sizeof(Heap)];
45   gobble(new (buffer) Heap);
46 }
47 
48 Heap invalidStatic; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}}
49 struct RandomClass {
50   Heap nonstaticMember; // expected-note {{'RandomClass' is a heap type because member 'nonstaticMember' is a heap type 'Heap'}}
51   static Heap staticMember; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}}
52 };
53 struct MOZ_HEAP_CLASS RandomHeapClass {
54   Heap nonstaticMember;
55   static Heap staticMember; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}}
56 };
57 
58 struct BadInherit : Heap {}; // expected-note {{'BadInherit' is a heap type because it inherits from a heap type 'Heap'}}
59 struct MOZ_HEAP_CLASS GoodInherit : Heap {};
60 
useStuffWrongly()61 void useStuffWrongly() {
62   BadInherit i; // expected-error {{variable of type 'BadInherit' only valid on the heap}} expected-note {{value incorrectly allocated in an automatic variable}}
63   RandomClass r; // expected-error {{variable of type 'RandomClass' only valid on the heap}} expected-note {{value incorrectly allocated in an automatic variable}}
64 }
65