| 1 | // RUN: %check_clang_tidy %s cppcoreguidelines-interfaces-global-init %t |
| 2 | |
| 3 | constexpr int makesInt() { return 3; } |
| 4 | constexpr int takesInt(int i) { return i + 1; } |
| 5 | constexpr int takesIntPtr(int *i) { return *i; } |
| 6 | |
| 7 | extern int ExternGlobal; |
| 8 | static int GlobalScopeBadInit1 = ExternGlobal; |
| 9 | // CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal' |
| 10 | static int GlobalScopeBadInit2 = takesInt(i: ExternGlobal); |
| 11 | // CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal' |
| 12 | static int GlobalScopeBadInit3 = takesIntPtr(i: &ExternGlobal); |
| 13 | // CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal' |
| 14 | static int GlobalScopeBadInit4 = 3 * (ExternGlobal + 2); |
| 15 | // CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal' |
| 16 | |
| 17 | #if __cplusplus >= 202002L |
| 18 | extern constinit int ExternConstinitGlobal; |
| 19 | static int GlobalScopeConstinit1 = ExternConstinitGlobal; |
| 20 | static int GlobalScopeConstinit2 = takesInt(ExternConstinitGlobal); |
| 21 | static int GlobalScopeConstinit3 = takesIntPtr(&ExternConstinitGlobal); |
| 22 | static int GlobalScopeConstinit4 = 3 * (ExternConstinitGlobal + 2); |
| 23 | #endif |
| 24 | |
| 25 | namespace ns { |
| 26 | static int NamespaceScope = makesInt(); |
| 27 | static int NamespaceScopeBadInit = takesInt(i: ExternGlobal); |
| 28 | // CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal' |
| 29 | |
| 30 | #if __cplusplus >= 202002L |
| 31 | static int NamespaceScopeConstinit = takesInt(ExternConstinitGlobal); |
| 32 | #endif |
| 33 | |
| 34 | struct A { |
| 35 | static int ClassScope; |
| 36 | static int ClassScopeBadInit; |
| 37 | }; |
| 38 | |
| 39 | int A::ClassScopeBadInit = takesInt(i: ExternGlobal); |
| 40 | // CHECK-MESSAGES: [[@LINE-1]]:8: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal' |
| 41 | |
| 42 | static int FromClassBadInit = takesInt(i: A::ClassScope); |
| 43 | // CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ClassScope' |
| 44 | |
| 45 | #if __cplusplus >= 202002L |
| 46 | struct B { |
| 47 | static constinit int ClassScopeConstinit; |
| 48 | static int ClassScopeFromConstinit; |
| 49 | }; |
| 50 | |
| 51 | int B::ClassScopeFromConstinit = takesInt(ExternConstinitGlobal); |
| 52 | static int FromClassScopeConstinit = takesInt(B::ClassScopeConstinit); |
| 53 | #endif |
| 54 | |
| 55 | } // namespace ns |
| 56 | |
| 57 | // "const int B::I;" is fine, it just ODR-defines B::I. See [9.4.3] Static |
| 58 | // members [class.static]. However the ODR-definitions are not in the right |
| 59 | // order (C::I after C::J, see [3.6.2.3]). |
| 60 | class B1 { |
| 61 | static const int I = 0; |
| 62 | static const int J = I; |
| 63 | }; |
| 64 | const int B1::J; |
| 65 | // CHECK-MESSAGES: [[@LINE-1]]:15: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'I' |
| 66 | const int B1::I; |
| 67 | |
| 68 | #if __cplusplus >= 202002L |
| 69 | class D { |
| 70 | static const constinit int I = 0; |
| 71 | static const int J = I; |
| 72 | }; |
| 73 | |
| 74 | const int D::J; |
| 75 | const int D::I; |
| 76 | #endif |
| 77 | |
| 78 | void f() { |
| 79 | // This is fine, it's executed after dynamic initialization occurs. |
| 80 | static int G = takesInt(i: ExternGlobal); |
| 81 | } |
| 82 | |
| 83 | // Declaration then definition then usage is fine. |
| 84 | extern int ExternGlobal2; |
| 85 | extern int ExternGlobal2; |
| 86 | int ExternGlobal2 = 123; |
| 87 | static int GlobalScopeGoodInit1 = ExternGlobal2; |
| 88 | |
| 89 | |
| 90 | // Defined global variables are fine: |
| 91 | static int GlobalScope = makesInt(); |
| 92 | static int GlobalScopeGoodInit2 = takesInt(i: GlobalScope); |
| 93 | static int GlobalScope2 = takesInt(i: ns::NamespaceScope); |
| 94 | // Enums are fine. |
| 95 | enum Enum { kEnumValue = 1 }; |
| 96 | static int = takesInt(i: kEnumValue); |
| 97 | |
| 98 | // Leave constexprs alone. |
| 99 | extern constexpr int GlobalScopeConstexpr = makesInt(); |
| 100 | static constexpr int GlobalScopeConstexprOk = |
| 101 | takesInt(i: GlobalScopeConstexpr); |
| 102 | |
| 103 | // This is a pretty common instance: People are usually not using constexpr, but |
| 104 | // this is what they should write: |
| 105 | static constexpr const char kValue[] = "value" ; |
| 106 | constexpr int Fingerprint(const char *value) { return 0; } |
| 107 | static int kFingerprint = Fingerprint(value: kValue); |
| 108 | |
| 109 | // This is fine because the ODR-definitions are in the right order (C::J after |
| 110 | // C::I). |
| 111 | class B2 { |
| 112 | static const int I = 0; |
| 113 | static const int J = I; |
| 114 | }; |
| 115 | const int B2::I; |
| 116 | const int B2::J; |
| 117 | |