[clang] [C] Warn on uninitialized const objects (PR #137166)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri May 2 04:35:21 PDT 2025


================
@@ -8197,6 +8197,16 @@ def err_address_space_qualified_new : Error<
 def err_address_space_qualified_delete : Error<
   "'delete' cannot delete objects of type %0 in address space '%1'">;
 
+def note_default_init_const_member : Note<
+  "member %0 declared 'const' here">;
+def warn_default_init_const : Warning<
+  "default initialization of an object of type %0%select{| with const member}1 "
+  "is incompatible with C++">,
+  InGroup<DefaultConstInit>, DefaultIgnore;
+def warn_default_init_const_unsafe : Warning<
+  "default initialization of an object of type %0%select{| with const member}1 "
+  "leaves the object uninitialized and is incompatible with C++">,
----------------
AaronBallman wrote:

Good question! I wish I didn't. :-D We don't have a way to know whether a particular warning *group* is enabled, so there's no way to tell the difference between the user passing no flags and getting the default-enabled `-Wdefault-const-init-unsafe`, whether they passed `-Wdefault-const-init-unsafe` explicitly, or whether they passed `-Wc++-compat`. We just can tell that the `warn_default_init_const_unsafe` diagnostic itself is/is not ignored at a particular source location. So I don't have a good way to drop/add the "and is incompatible with C++" because I don't have a way to test "did the user explicitly opt in to `-Wc++-compat`.

However, maybe I can hack around this by adding a new off-by-default fake diagnostic that is never emitted by Clang but is in the `-Wc++-compat` warning group. If *that* diagnostic is enabled, it must mean the user passed `-Wc++-compat`...

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


More information about the cfe-commits mailing list