1*89a1d03eSRichard // RUN: %check_clang_tidy %s fuchsia-statically-constructed-objects %t
2*89a1d03eSRichard
3*89a1d03eSRichard // Trivial static is fine
4*89a1d03eSRichard static int i;
5*89a1d03eSRichard
6*89a1d03eSRichard class ClassWithNoCtor {};
7*89a1d03eSRichard
8*89a1d03eSRichard class ClassWithCtor {
9*89a1d03eSRichard public:
ClassWithCtor(int Val)10*89a1d03eSRichard ClassWithCtor(int Val) : Val(Val) {}
11*89a1d03eSRichard private:
12*89a1d03eSRichard int Val;
13*89a1d03eSRichard };
14*89a1d03eSRichard
15*89a1d03eSRichard class ClassWithConstexpr {
16*89a1d03eSRichard public:
ClassWithConstexpr(int Val1,int Val2)17*89a1d03eSRichard ClassWithConstexpr(int Val1, int Val2) : Val(Val1) {}
ClassWithConstexpr(int Val)18*89a1d03eSRichard constexpr ClassWithConstexpr(int Val) : Val(Val) {}
19*89a1d03eSRichard
20*89a1d03eSRichard private:
21*89a1d03eSRichard int Val;
22*89a1d03eSRichard };
23*89a1d03eSRichard
24*89a1d03eSRichard ClassWithNoCtor A;
25*89a1d03eSRichard ClassWithConstexpr C(0);
26*89a1d03eSRichard ClassWithConstexpr E(0, 1);
27*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
28*89a1d03eSRichard // CHECK-MESSAGES-NEXT: ClassWithConstexpr E(0, 1);
29*89a1d03eSRichard ClassWithCtor G(0);
30*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
31*89a1d03eSRichard // CHECK-MESSAGES-NEXT: ClassWithCtor G(0);
32*89a1d03eSRichard
33*89a1d03eSRichard static ClassWithNoCtor A2;
34*89a1d03eSRichard static ClassWithConstexpr C2(0);
35*89a1d03eSRichard static ClassWithConstexpr E2(0, 1);
36*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
37*89a1d03eSRichard // CHECK-MESSAGES-NEXT: static ClassWithConstexpr E2(0, 1);
38*89a1d03eSRichard static ClassWithCtor G2(0);
39*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
40*89a1d03eSRichard // CHECK-MESSAGES-NEXT: static ClassWithCtor G2(0);
41*89a1d03eSRichard
StructWithConstexprStructWithConstexpr42*89a1d03eSRichard struct StructWithConstexpr { constexpr StructWithConstexpr(int Val) {} };
43*89a1d03eSRichard struct StructWithNoCtor {};
44*89a1d03eSRichard struct StructWithCtor { StructWithCtor(); };
45*89a1d03eSRichard
46*89a1d03eSRichard StructWithNoCtor SNoCtor;
47*89a1d03eSRichard StructWithConstexpr SConstexpr(0);
48*89a1d03eSRichard StructWithCtor SCtor;
49*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
50*89a1d03eSRichard // CHECK-MESSAGES-NEXT: StructWithCtor SCtor;
51*89a1d03eSRichard
52*89a1d03eSRichard static StructWithConstexpr SConstexpr2(0);
53*89a1d03eSRichard static StructWithNoCtor SNoCtor2;
54*89a1d03eSRichard static StructWithCtor SCtor2;
55*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
56*89a1d03eSRichard // CHECK-MESSAGES-NEXT: static StructWithCtor SCtor2;
57*89a1d03eSRichard
58*89a1d03eSRichard extern StructWithCtor SCtor3;
59*89a1d03eSRichard
60*89a1d03eSRichard class ClassWithStaticMember {
61*89a1d03eSRichard private:
62*89a1d03eSRichard static StructWithNoCtor S;
63*89a1d03eSRichard };
64*89a1d03eSRichard
65*89a1d03eSRichard ClassWithStaticMember Z();
66*89a1d03eSRichard
67*89a1d03eSRichard class S {
68*89a1d03eSRichard int Val;
69*89a1d03eSRichard public:
S(int i)70*89a1d03eSRichard constexpr S(int i) : Val(100 / i) {}
getVal() const71*89a1d03eSRichard int getVal() const { return Val; }
72*89a1d03eSRichard };
73*89a1d03eSRichard
74*89a1d03eSRichard static S s1(1);
75*89a1d03eSRichard static S s2(0);
76*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
77*89a1d03eSRichard // CHECK-MESSAGES-NEXT: static S s2(0);
78*89a1d03eSRichard
79*89a1d03eSRichard extern int get_i();
80*89a1d03eSRichard static S s3(get_i());
81*89a1d03eSRichard // CHECK-MESSAGES: [[@LINE-1]]:1: warning: static objects are disallowed; if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
82*89a1d03eSRichard // CHECK-MESSAGES-NEXT: static S s3(get_i());
83*89a1d03eSRichard
f()84*89a1d03eSRichard void f() {
85*89a1d03eSRichard // Locally static is fine
86*89a1d03eSRichard static int i;
87*89a1d03eSRichard static ClassWithNoCtor A2;
88*89a1d03eSRichard static ClassWithConstexpr C2(0);
89*89a1d03eSRichard static ClassWithConstexpr E2(0, 1);
90*89a1d03eSRichard static ClassWithCtor G2(0);
91*89a1d03eSRichard }
92