92 lines · cpp
1// RUN: %check_clang_tidy %s fuchsia-statically-constructed-objects %t2 3// Trivial static is fine4static int i;5 6class ClassWithNoCtor {};7 8class ClassWithCtor {9public:10 ClassWithCtor(int Val) : Val(Val) {}11private:12 int Val;13};14 15class ClassWithConstexpr {16public:17 ClassWithConstexpr(int Val1, int Val2) : Val(Val1) {}18 constexpr ClassWithConstexpr(int Val) : Val(Val) {}19 20private:21 int Val;22};23 24ClassWithNoCtor A;25ClassWithConstexpr C(0);26ClassWithConstexpr 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);29ClassWithCtor 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 33static ClassWithNoCtor A2;34static ClassWithConstexpr C2(0);35static 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);38static 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 42struct StructWithConstexpr { constexpr StructWithConstexpr(int Val) {} };43struct StructWithNoCtor {};44struct StructWithCtor { StructWithCtor(); };45 46StructWithNoCtor SNoCtor;47StructWithConstexpr SConstexpr(0);48StructWithCtor 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 52static StructWithConstexpr SConstexpr2(0);53static StructWithNoCtor SNoCtor2;54static 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 58extern StructWithCtor SCtor3;59 60class ClassWithStaticMember {61private:62 static StructWithNoCtor S;63};64 65ClassWithStaticMember Z();66 67class S {68 int Val;69public:70 constexpr S(int i) : Val(100 / i) {}71 int getVal() const { return Val; }72};73 74static S s1(1);75static 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 79extern int get_i();80static 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 84void f() {85 // Locally static is fine86 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