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