1// RUN: %check_clang_tidy %s cppcoreguidelines-interfaces-global-init %t
2
3constexpr int makesInt() { return 3; }
4constexpr int takesInt(int i) { return i + 1; }
5constexpr int takesIntPtr(int *i) { return *i; }
6
7extern int ExternGlobal;
8static 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'
10static 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'
12static 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'
14static 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
17namespace ns {
18static int NamespaceScope = makesInt();
19static int NamespaceScopeBadInit = takesInt(i: ExternGlobal);
20// CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal'
21
22struct A {
23 static int ClassScope;
24 static int ClassScopeBadInit;
25};
26
27int A::ClassScopeBadInit = takesInt(i: ExternGlobal);
28// CHECK-MESSAGES: [[@LINE-1]]:8: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ExternGlobal'
29
30static int FromClassBadInit = takesInt(i: A::ClassScope);
31// CHECK-MESSAGES: [[@LINE-1]]:12: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'ClassScope'
32} // namespace ns
33
34// "const int B::I;" is fine, it just ODR-defines B::I. See [9.4.3] Static
35// members [class.static]. However the ODR-definitions are not in the right
36// order (C::I after C::J, see [3.6.2.3]).
37class B1 {
38 static const int I = 0;
39 static const int J = I;
40};
41const int B1::J;
42// CHECK-MESSAGES: [[@LINE-1]]:15: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'I'
43const int B1::I;
44
45void f() {
46 // This is fine, it's executed after dynamic initialization occurs.
47 static int G = takesInt(i: ExternGlobal);
48}
49
50// Declaration then definition then usage is fine.
51extern int ExternGlobal2;
52extern int ExternGlobal2;
53int ExternGlobal2 = 123;
54static int GlobalScopeGoodInit1 = ExternGlobal2;
55
56
57// Defined global variables are fine:
58static int GlobalScope = makesInt();
59static int GlobalScopeGoodInit2 = takesInt(i: GlobalScope);
60static int GlobalScope2 = takesInt(i: ns::NamespaceScope);
61// Enums are fine.
62enum Enum { kEnumValue = 1 };
63static int GlobalScopeFromEnum = takesInt(i: kEnumValue);
64
65// Leave constexprs alone.
66extern constexpr int GlobalScopeConstexpr = makesInt();
67static constexpr int GlobalScopeConstexprOk =
68 takesInt(i: GlobalScopeConstexpr);
69
70// This is a pretty common instance: People are usually not using constexpr, but
71// this is what they should write:
72static constexpr const char kValue[] = "value";
73constexpr int Fingerprint(const char *value) { return 0; }
74static int kFingerprint = Fingerprint(value: kValue);
75
76// This is fine because the ODR-definitions are in the right order (C::J after
77// C::I).
78class B2 {
79 static const int I = 0;
80 static const int J = I;
81};
82const int B2::I;
83const int B2::J;
84
85

source code of clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/interfaces-global-init.cpp