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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 11:35:15 PST 2024


================
@@ -1419,6 +1419,42 @@ is not specified.
   }];
 }
 
+def ExplicitInitDocs : Documentation {
+  let Category = DocCatField;
+  let Content = [{
+The ``clang::requires_explicit_initialization`` attribute indicates that the
+field of an aggregate must be initialized explicitly by users when the class
+is constructed. Its usage is invalid on non-aggregates.
+
+Note that this attribute is *not* a memory safety feature, and is *not* intended
+to guard against use of uninitialized memory.
+
+Rather, it is intended for use in "parameter-objects", used to simulate the
+passing of named parameters.
+The attribute generates a warning when explicit initializers for such
+"named parameters" are not provided:
----------------
AaronBallman wrote:

I think a few things may be missing here.

1) This implies to me that using initialization other than a designated initializer which names the field are not supported, so it might be useful to show a field initialized via position as part of the example to make it clear that the field has to be initialized, it doesn't have to be named in the initializer explicitly.
2) The phrasing is pretty C++ specific so perhaps this should mention C support.
3) You should mention what happens if the attribute is written on a field with an in-class initializer and there is no explicit initialization of that field.

WDYT?

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


More information about the cfe-commits mailing list