[clang] Add Clang attribute to ensure that fields are initialized explicitly (PR #102040)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 25 10:43:40 PDT 2024
================
@@ -1419,6 +1419,44 @@ 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:
+
+.. code-block:: c++
+
+ struct ArrayIOParams {
+ size_t count [[clang::requires_explicit_initialization]];
+ size_t element_size [[clang::requires_explicit_initialization]];
+ int flags = 0;
+ };
+
+ size_t ReadArray(FILE *file, void *buffer, ArrayIOParams params);
+
+ int main() {
+ unsigned int buf[512];
+ ReadArray(stdin, buf, {
+ .count = sizeof(buf) / sizeof(*buf),
+ .element_size = sizeof(*buf)
+ });
+ }
+
+This attribute marks such "named parameters" as mandatory, thus informing
----------------
erichkeane wrote:
mandatory implies an error diagnostic here without further elaboration. Something that shows the warning behavior/example + output if you were to forget this would be appreciated.
https://github.com/llvm/llvm-project/pull/102040
More information about the cfe-commits
mailing list