1 #define MOZ_TEMPORARY_CLASS __attribute__((annotate("moz_temporary_class")))
2 #define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
6 struct MOZ_TEMPORARY_CLASS 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
; }
16 struct MOZ_TEMPORARY_CLASS TemplateClass
{
20 void gobble(void *) { }
22 void gobbleref(const Temporary
&) { }
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'}}
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));
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}}
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());