Skip to content
Open
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ Improvements to Clang's diagnostics
- Clang now emits a diagnostic in case `vector_size` or `ext_vector_type`
attributes are used with a negative size (#GH165463).

- The warning about static local variables declared inside `inline`
functions is now correctly converted to an error if `-pedantic-errors` is
passed (#GH39524).

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -6339,7 +6339,7 @@ def warn_c2y_compat_internal_in_extern_inline : Warning<
"using static %select{function|variable}0 %1 in an inline function with "
"external linkage is incompatible with standards before C2y">,
InGroup<CPre2yCompat>, DefaultIgnore;
def warn_static_local_in_extern_inline : Warning<
def warn_static_local_in_extern_inline : ExtWarn<
"non-constant static local variable in inline function may be different "
"in different files">, InGroup<StaticLocalInInline>;
def note_convert_inline_to_static : Note<
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Sema/inline-static-var.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -std=c11 -pedantic-errors -verify %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the test should have a triple; I would expect this diagnostic to be target-agnostic based on the code which emits the diagnostic. Can you hook up a debugger to see why the diagnostic was being silenced on Windows targets in the first place?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AaronBallman,

Thanks for the review. I checked it, and it seems the diagnostic is intentionally target-dependent. I'd be grateful if you could sanity-check my reasoning below:

My understanding is that this all comes down to the behavior of isFunctionDefinitionDiscarded() in this block in SemaDecl.cpp:

  • On Windows (MSVC): Clang emulates the MSVC model, where inline acts like C++ and is the "real" external definition. The linker just merges all the identical copies. Because this definition is not "discardable," isFunctionDefinitionDiscarded() returns false, and our diagnostic (correctly) doesn't fire.

  • On Linux (GNU): Clang uses the C99 model, where inline is just a "discardable copy" that's separate from the "real" definition. Because this definition is "discardable," isFunctionDefinitionDiscarded() returns true, which is what triggers our ExtWarn.

So, it seems the CI failed on Windows simply because, under MSVC's rules, this code isn't an error.

If that sounds right, then adding the -triple x86_64-pc-linux-gnu to the test file feels like the correct solution, as it forces the test to check against the GNU/C99 semantics we're actually trying to fix.

I'm not completely sure if this is the standard way to handle a target-specific test, though. Please let me know if there's a better approach!


inline void f(void) { // expected-note {{use 'static' to give inline function 'f' internal linkage}}
static int x; // expected-error {{non-constant static local variable in inline function may be different in different files}}
}