[clang] Add Clang attribute to ensure that fields are initialized explicitly (PR #102040)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 25 03:04:28 PST 2024


================
@@ -1472,3 +1472,144 @@ template<typename T> struct Outer {
   };
 };
 Outer<int>::Inner outerinner;
+
+struct Polymorphic { virtual ~Polymorphic() { } };
+
+template<class... Bases>
+struct Inherit : Bases... { // #TYPE_INHERIT
+  int g1; // #FIELD_G1
+};
+
+template<class... Bases>
+struct InheritWithExplicit : Bases... { // #TYPE_INHERIT_WITH_EXPLICIT
+  int g2 [[clang::requires_explicit_initialization]]; // #FIELD_G2
+};
+
+struct Special {};
+
+template<>
+struct Inherit<Special> {
+  int g3 [[clang::requires_explicit_initialization]]; // #FIELD_G3
+};
+
+template<>
+struct InheritWithExplicit<Special> {
+  int g4; // #FIELD_G4
+};
+
+void aggregate() {
+  struct NonAgg {
+    NonAgg() { }
+    [[clang::requires_explicit_initialization]] int na;  // expected-warning {{'requires_explicit_initialization' attribute is ignored in non-aggregate type 'NonAgg'}}
+  };
+  NonAgg nonagg;  // no-warning
+  (void)nonagg;
+
+  struct S {
+    [[clang::requires_explicit_initialization]] int s1; // #FIELD_S1
+    int s2;
+    int s3 = 12;
+    [[clang::requires_explicit_initialization]] int s4 = 100; // #FIELD_S4
+    static void foo(S) { }
+  };
+
+  struct C {
+    [[clang::requires_explicit_initialization]] int c1; // #FIELD_C1
+#if 201703L <= __cplusplus && __cplusplus < 202002L
----------------
higher-performance wrote:

Yup done.

https://github.com/llvm/llvm-project/pull/102040


More information about the cfe-commits mailing list