Debian Codegraph @

build/clang-plugin/tests/TestGlobalVariableInitialization.cpp

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