1 #define MOZ_STATIC_LOCAL_CLASS __attribute__((annotate("moz_static_local_class")))
2 #include <stddef.h>
3 
4 struct MOZ_STATIC_LOCAL_CLASS StaticLocal {
5   int i;
operator newStaticLocal6   void *operator new(size_t x) throw() { return 0; }
operator newStaticLocal7   void *operator new(size_t blah, char *buffer) { return buffer; }
8 };
9 
10 template <class T>
11 struct MOZ_STATIC_LOCAL_CLASS TemplateClass {
12   T i;
13 };
14 
gobble(void *)15 void gobble(void *) { }
16 
misuseStaticLocalClass(int len)17 void misuseStaticLocalClass(int len) {
18   StaticLocal notValid; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in an automatic variable}}
19   StaticLocal alsoNotValid[2]; // expected-error-re {{variable of type 'StaticLocal{{ ?}}[2]' is only valid as a static local}} expected-note-re {{'StaticLocal{{ ?}}[2]' is a static-local type because it is an array of static-local type 'StaticLocal'}} expected-note {{value incorrectly allocated in an automatic variable}}
20   static StaticLocal valid;
21   static StaticLocal alsoValid[2];
22 
23   gobble(&notValid);
24   gobble(&valid);
25   gobble(&alsoValid[0]);
26 
27   gobble(new StaticLocal); // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
28   gobble(new StaticLocal[10]); // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
29   gobble(new TemplateClass<int>); // expected-error {{variable of type 'TemplateClass<int>' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
30   gobble(len <= 5 ? &valid : new StaticLocal); // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
31 
32   char buffer[sizeof(StaticLocal)];
33   gobble(new (buffer) StaticLocal);
34 }
35 
36 StaticLocal notValid; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in a global variable}}
37 
38 struct RandomClass {
39   StaticLocal nonstaticMember; // expected-note {{'RandomClass' is a static-local type because member 'nonstaticMember' is a static-local type 'StaticLocal'}}
40   static StaticLocal staticMember; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in a global variable}}
41 };
42 
43 struct MOZ_STATIC_LOCAL_CLASS RandomStaticLocalClass {
44   StaticLocal nonstaticMember;
45   static StaticLocal staticMember; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in a global variable}}
46 };
47 
48 struct BadInherit : StaticLocal {}; // expected-note {{'BadInherit' is a static-local type because it inherits from a static-local type 'StaticLocal'}}
49 struct MOZ_STATIC_LOCAL_CLASS GoodInherit : StaticLocal {};
50 
misuseStaticLocalClassEvenMore(int len)51 void misuseStaticLocalClassEvenMore(int len) {
52   BadInherit moreInvalid; // expected-error {{variable of type 'BadInherit' is only valid as a static local}} expected-note {{value incorrectly allocated in an automatic variable}}
53   RandomClass evenMoreInvalid; // expected-error {{variable of type 'RandomClass' is only valid as a static local}} expected-note {{value incorrectly allocated in an automatic variable}}
54 }
55