tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestGlobalClass.cpp (2535B)


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