[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:18 PST 2024


================
@@ -3141,6 +3148,10 @@ def warn_attribute_ignored_no_calls_in_stmt: Warning<
   "statement">,
   InGroup<IgnoredAttributes>;
 
+def warn_attribute_needs_aggregate : Warning<
----------------
AaronBallman wrote:

Thank you for your patience, sorry it took so long to get to this PR in my queue!

I think I come down on the side of `warn_attribute_needs_aggregate` being a warning rather than an error. We have a direct recovery path (ignore the attribute, proceed as normal, which is what every other compiler will do anyway), but also, this leaves the door a bit more open to supporting non-aggregates in the future because it means users can put the attribute on a non-aggregate in Clang 21 and it doesn't fail compilation in Clang 20, without requiring conditional compilation.

`warn_field_requires_explicit_init` seems more defensible as an error because that's "you're misusing this". But if it's a hard error, I don't know what kind of escape hatch we would give users who are getting a type declared in a header they can't modify but they somehow have a legit use case without naming the field. (Maybe they're doing partial initialization and filling out the rest in subsequent statements, etc) So maybe a warning that defaults to an error is a better approach because you still get the strong encouragement to not do that, but you have an escape hatch (pragmas if you want to only silence once instance, command line flag otherwise). WDYT?

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


More information about the cfe-commits mailing list