Bug 1814798 - pt 1. Add bool to enable/disable PHC at runtime r=glandium
[gecko.git] / build / clang-plugin / tests / TestTemporaryClass.cpp
blobf00acf0d7ae5c54b7c5b2f9a533b714aa28b0b7d
1 #define MOZ_TEMPORARY_CLASS __attribute__((annotate("moz_temporary_class")))
2 #define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
4 #include <stddef.h>
6 struct MOZ_TEMPORARY_CLASS Temporary {
7 int i;
8 Temporary() {}
9 MOZ_IMPLICIT Temporary(int a) {}
10 Temporary(int a, int b) {}
11 void *operator new(size_t x) throw() { return 0; }
12 void *operator new(size_t blah, char *buffer) { return buffer; }
15 template <class T>
16 struct MOZ_TEMPORARY_CLASS TemplateClass {
17 T i;
20 void gobble(void *) { }
22 void gobbleref(const Temporary&) { }
24 template <class T>
25 void gobbleanyref(const T&) { }
27 void misuseNonTemporaryClass(int len) {
28 // All of these should error.
29 Temporary invalid; // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated in an automatic variable}}
30 Temporary alsoInvalid[2]; // expected-error-re {{variable of type 'Temporary{{ ?}}[2]' is only valid as a temporary}} expected-note {{value incorrectly allocated in an automatic variable}} expected-note-re {{'Temporary{{ ?}}[2]' is a temporary type because it is an array of temporary type 'Temporary'}}
31 static Temporary invalidStatic; // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated in a global variable}}
32 static Temporary alsoInvalidStatic[2]; // expected-error-re {{variable of type 'Temporary{{ ?}}[2]' is only valid as a temporary}} expected-note {{value incorrectly allocated in a global variable}} expected-note-re {{'Temporary{{ ?}}[2]' is a temporary type because it is an array of temporary type 'Temporary'}}
34 gobble(&invalid);
35 gobble(&invalidStatic);
36 gobble(&alsoInvalid[0]);
38 // All of these should be fine.
39 gobbleref(Temporary());
40 gobbleref(Temporary(10, 20));
41 gobbleref(Temporary(10));
42 gobbleref(10);
43 gobbleanyref(TemplateClass<int>());
45 // All of these should error.
46 gobble(new Temporary); // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated on the heap}}
47 gobble(new Temporary[10]); // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated on the heap}}
48 gobble(new TemplateClass<int>); // expected-error {{variable of type 'TemplateClass<int>' is only valid as a temporary}} expected-note {{value incorrectly allocated on the heap}}
49 gobble(len <= 5 ? &invalid : new Temporary); // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated on the heap}}
51 // Placement new is odd, but okay.
52 char buffer[sizeof(Temporary)];
53 gobble(new (buffer) Temporary);
56 void defaultArg(const Temporary& arg = Temporary()) { // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated in an automatic variable}}
59 // Can't be a global, this should error.
60 Temporary invalidStatic; // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated in a global variable}}
62 struct RandomClass {
63 Temporary nonstaticMember; // This is okay if RandomClass is only used as a temporary.
64 static Temporary staticMember; // expected-error {{variable of type 'Temporary' is only valid as a temporary}} expected-note {{value incorrectly allocated in a global variable}}
67 struct BadInherit : Temporary {};
69 void useStuffWrongly() {
70 gobbleanyref(BadInherit());
71 gobbleanyref(RandomClass());