llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang] require_explicit_initialization not diagnosed for C tentative definitions that are never completed

Open kees opened this issue 1 month ago • 3 comments

In C, -Wuninitialized-explicit-init is not issued for a file-scope declaration with no initializer, such as struct ops g; or static struct ops g;, when nothing later in the translation unit initializes it. The same declaration warns inside a function, with or without static, and at namespace scope in C++.

struct ops {
	int (*show)(void) __attribute__((require_explicit_initialization));
	int flags;
};

struct ops never_completed;
static struct ops never_completed_static;

struct ops completed_later;
struct ops completed_later = { .flags = 1 };

void f(void)
{
	static struct ops local_static;
	struct ops local;

	(void)&local_static;
	(void)&local;
}

With Clang 24.0.0git (c275ebd64bf5); Clang 22.1.8 prints the same:

$ clang -std=c17 -fsyntax-only tentative.c
tentative.c:10:30: warning: field 'show' requires explicit initialization but is not explicitly initialized [-Wuninitialized-explicit-init]
   10 | struct ops completed_later = { .flags = 1 };
      |                              ^
tentative.c:2:8: note: 'show' declared here
    2 |         int (*show)(void) __attribute__((require_explicit_initialization));
      |               ^
tentative.c:14:20: warning: field in 'ops' requires explicit initialization but is not explicitly initialized [-Wuninitialized-explicit-init]
   14 |         static struct ops local_static;
      |                           ^
tentative.c:2:8: note: 'show' declared here
    2 |         int (*show)(void) __attribute__((require_explicit_initialization));
      |               ^
tentative.c:15:13: warning: field in 'ops' requires explicit initialization but is not explicitly initialized [-Wuninitialized-explicit-init]
   15 |         struct ops local;
      |                    ^
tentative.c:2:8: note: 'show' declared here
    2 |         int (*show)(void) __attribute__((require_explicit_initialization));
      |               ^
3 warnings generated.

Lines 6 and 7 get no warning. Compiled as C++ (-x c++), both do.

Both are tentative definitions (C17 6.9.2). Sema::ActOnUninitializedDecl() records them and returns before any default initialization is built, so the check in InitializationSequence::InitializeFrom() never sees them: https://github.com/llvm/llvm-project/blob/05e432a8597fde38ea36d5885215415d879dc959/clang/lib/Sema/SemaDecl.cpp#L14862-L14896 https://github.com/llvm/llvm-project/blob/05e432a8597fde38ea36d5885215415d879dc959/clang/lib/Sema/SemaInit.cpp#L6762-L6772

Staying quiet at the declaration is right, since a later declaration may still supply an initializer, and that one is checked (line 10 above). A tentative definition that is never completed, though, behaves "as if the translation unit contains a file scope declaration of that identifier ... with an initializer equal to 0" (6.9.2p2), so its fields that require explicit initialization never get one. That is the same state as local_static, which warns.

Sema::ActOnEndOfTranslationUnit() already diagnoses never-completed tentative definitions for the analogous const case, issuing warn_default_init_const for a const-qualified one left without an initializer: https://github.com/llvm/llvm-project/blob/05e432a8597fde38ea36d5885215415d879dc959/clang/lib/Sema/Sema.cpp#L1536-L1551

A hasUninitializedExplicitInitFields() check beside it would cover this case. It would not warn for forward declarations that are completed later, which are common: Linux has ten file-scope forward declarations of struct seq_operations alone, the first structure a pending kernel series marks with this attribute (https://lore.kernel.org/all/[email protected]/).

CC @higher-performance @ojeda @captain5050

kees avatar Oct 15 '26 20:10 kees

Hi! I'm planning to work on this issue as part of the CS3423 Compilers II hackathon at IIT Hyderabad. I'd like to investigate the missing -Wuninitialized-explicit-init diagnostic for C tentative definitions, implement a fix, and add the corresponding regression tests. Could you please assign this issue to me? Thank you!

PathriVidyaPraveen avatar Oct 19 '26 03:10 PathriVidyaPraveen

3. Hi! I'm planning to work on this issue as part of the CS3423 Compilers II hackathon at IIT Hyderabad. I'd like to investigate the missing -Wuninitialized-explicit-init diagnostic for C tentative definitions, implement a fix, and add the corresponding regression tests. Could you please assign this issue to me? Thank you!

We don't assign issues until there is a PR and it is moving in the right direction.

This is unlikely to be a good hackathon problem since it can takes weeks or longer to finally land a PR, especially if this is your first time.

shafik avatar Oct 19 '26 21:10 shafik

Thanks for fixing this, @PathriVidyaPraveen !

nickdesaulniers avatar Oct 22 '26 21:10 nickdesaulniers