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