tor-browser

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

TestGlobalVariableInitialization.cpp (2379B)


      1 #define MOZ_RUNINIT  __attribute__((annotate("moz_global_var")))
      2 #define MOZ_GLOBAL_CLASS __attribute__((annotate("moz_global_class")))
      3 
      4 // POD Type
      5 struct POD {
      6  int i, j, k;
      7 };
      8 
      9 POD g0;
     10 
     11 // constexpr constructor
     12 struct ConstexprGlobal {
     13  int i, j, k;
     14  constexpr ConstexprGlobal() : i(0), j(1), k(2) {}
     15 };
     16 
     17 ConstexprGlobal g1;
     18 
     19 // Global with extern constructor
     20 struct Global {
     21  Global();
     22 };
     23 
     24 Global g2; // expected-error {{Global variable has runtime initialisation, try to remove it, make it constexpr or constinit if possible, or as a last resort flag it as MOZ_RUNINIT.}}
     25 
     26 // Global with extern constructor *but* marked MOZ_GLOBAL_CLASS
     27 struct MOZ_GLOBAL_CLASS GlobalCls {
     28  GlobalCls();
     29 };
     30 
     31 GlobalCls g3;
     32 
     33 // Global with extern constructor *but* marked MOZ_RUNINIT
     34 struct RuninitGlobal {
     35  RuninitGlobal();
     36 };
     37 
     38 MOZ_RUNINIT RuninitGlobal g4;
     39 
     40 // Global with constexpr constructor *but* marked MOZ_RUNINIT
     41 struct InvalidRuninitGlobal {
     42  constexpr InvalidRuninitGlobal() {}
     43 };
     44 
     45 MOZ_RUNINIT InvalidRuninitGlobal g5; // expected-error {{Global variable flagged as MOZ_RUNINIT but actually has constinit initialisation. Consider flagging it as constexpr or constinit instead.}}
     46 constexpr InvalidRuninitGlobal g5a;
     47 
     48 struct InvalidRuninitGlobal2 {
     49  int i;
     50 };
     51 
     52 MOZ_RUNINIT InvalidRuninitGlobal2 g5b; // expected-error {{Global variable flagged as MOZ_RUNINIT but actually has constant initialisation. Consider removing the annotation or (as a last resort) flagging it as MOZ_GLOBINIT.}}
     53 InvalidRuninitGlobal2 g5c;
     54 
     55 // Static variable with extern constructor
     56 Global g6;  // expected-error {{Global variable has runtime initialisation, try to remove it, make it constexpr or constinit if possible, or as a last resort flag it as MOZ_RUNINIT.}}
     57 
     58 // Static variable with extern constructor within a function
     59 void foo() { static Global g7; }
     60 
     61 // Global variable with extern constructor in a namespace
     62 namespace bar {Global g8;}  // expected-error {{Global variable has runtime initialisation, try to remove it, make it constexpr or constinit if possible, or as a last resort flag it as MOZ_RUNINIT.}}
     63 
     64 // Static variable with extern constructor in a class
     65 class foobar {static Global g9;};
     66 Global foobar::g9; // expected-error {{Global variable has runtime initialisation, try to remove it, make it constexpr or constinit if possible, or as a last resort flag it as MOZ_RUNINIT.}}