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

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 30 02:35:15 PDT 2024


================
@@ -1419,6 +1419,28 @@ is not specified.
   }];
 }
 
+def ExplicitInitDocs : Documentation {
+  let Category = DocCatField;
+  let Content = [{
+The ``clang::explicit_init`` attribute indicates that the field must be
+initialized explicitly by the caller when the class is constructed.
----------------
ilya-biryukov wrote:

Maybe document that the caveats here?
I could see us exploring the extension of this analysis to make sure the "explicit_init" fields are all written before they're read, but that's not what's happening today IIUC.

Another caveat, is that the attribute is ignored on non-aggregate classes.

```cpp
struct NonAgg {

  NonAgg()  {}
  int x [[clang::explicit_init]]; // attribute ignored.
}
```

And maybe also mention that these warnings do not aim to comprehensively prevent uses of uninitialized memory, but are, rather, supporting tools for aiding to write code in certain code style if people choose to (e.g. using it for parameter objects in combination with C++20 designated initializer).

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


More information about the cfe-commits mailing list