Skip to content

Commit 8e60adc

Browse files
authored
[C2y] Implement WG14 N3622 static used in an inline (#162877)
This paper removes the constraint that a static variable or function cannot be used within an extern inline function. The diagnostic is still being produced in earlier language modes for conformance reasons but has always been accepted as an extension.
1 parent 7dbf115 commit 8e60adc

File tree

7 files changed

+60
-23
lines changed

7 files changed

+60
-23
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ C Language Changes
180180

181181
C2y Feature Support
182182
^^^^^^^^^^^^^^^^^^^
183+
- No longer triggering ``-Wstatic-in-inline`` in C2y mode; use of a static
184+
function or variable within an extern inline function is no longer a
185+
constraint per `WG14 N3622 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3622.txt>`_.
183186
- Clang now supports `N3355 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm>`_ Named Loops.
184187

185188
C23 Feature Support

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ def C23 : DiagGroup<"c23-extensions", [VariadicMacroArgumentsOmitted]>;
14001400
def : DiagGroup<"c2x-extensions", [C23]>;
14011401

14021402
// A warning group for warnings about using C2y features as extensions.
1403-
def C2y : DiagGroup<"c2y-extensions">;
1403+
def C2y : DiagGroup<"c2y-extensions", [StaticInInline]>;
14041404

14051405
// Previously supported warning group which is no longer pertinent as binary
14061406
// literals are a C++14 and C23 extension now instead of a GNU extension.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6326,11 +6326,14 @@ def warn_internal_linkage_local_storage : Warning<
63266326
InGroup<IgnoredAttributes>;
63276327

63286328
def ext_internal_in_extern_inline : ExtWarn<
6329-
"static %select{function|variable}0 %1 is used in an inline function with "
6330-
"external linkage">, InGroup<StaticInInline>;
6329+
"using static %select{function|variable}0 %1 in an inline function with "
6330+
"external linkage is a C2y extension">, InGroup<StaticInInline>;
63316331
def ext_internal_in_extern_inline_quiet : Extension<
6332-
"static %select{function|variable}0 %1 is used in an inline function with "
6333-
"external linkage">, InGroup<StaticInInline>;
6332+
ext_internal_in_extern_inline.Summary>, InGroup<StaticInInline>;
6333+
def warn_c2y_compat_internal_in_extern_inline : Warning<
6334+
"using static %select{function|variable}0 %1 in an inline function with "
6335+
"external linkage is incompatible with standards before C2y">,
6336+
InGroup<CPre2yCompat>, DefaultIgnore;
63346337
def warn_static_local_in_extern_inline : Warning<
63356338
"non-constant static local variable in inline function may be different "
63366339
"in different files">, InGroup<StaticLocalInInline>;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S,
166166
// This is disabled under C++; there are too many ways for this to fire in
167167
// contexts where the warning is a false positive, or where it is technically
168168
// correct but benign.
169+
//
170+
// WG14 N3622 which removed the constraint entirely in C2y. It is left
171+
// enabled in earlier language modes because this is a constraint in those
172+
// language modes. But in C2y mode, we still want to issue the "incompatible
173+
// with previous standards" diagnostic, too.
169174
if (S.getLangOpts().CPlusPlus)
170175
return;
171176

@@ -190,16 +195,17 @@ static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S,
190195
// This last can give us false negatives, but it's better than warning on
191196
// wrappers for simple C library functions.
192197
const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D);
193-
bool DowngradeWarning = S.getSourceManager().isInMainFile(Loc);
194-
if (!DowngradeWarning && UsedFn)
195-
DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>();
196-
197-
S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline_quiet
198-
: diag::ext_internal_in_extern_inline)
199-
<< /*IsVar=*/!UsedFn << D;
198+
unsigned DiagID;
199+
if (S.getLangOpts().C2y)
200+
DiagID = diag::warn_c2y_compat_internal_in_extern_inline;
201+
else if ((UsedFn && (UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>())) ||
202+
S.getSourceManager().isInMainFile(Loc))
203+
DiagID = diag::ext_internal_in_extern_inline_quiet;
204+
else
205+
DiagID = diag::ext_internal_in_extern_inline;
200206

207+
S.Diag(Loc, DiagID) << /*IsVar=*/!UsedFn << D;
201208
S.MaybeSuggestAddingStaticToDecl(Current);
202-
203209
S.Diag(D->getCanonicalDecl()->getLocation(), diag::note_entity_declared_at)
204210
<< D;
205211
}

clang/test/C/C2y/n3622.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -verify=good -pedantic -Wall -std=c2y %s
2+
// RUN: %clang_cc1 -verify=compat,expected -pedantic -Wall -Wpre-c2y-compat -std=c2y %s
3+
// RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -std=c23 %s
4+
// RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -std=c17 %s
5+
// good-no-diagnostics
6+
7+
/* WG14 N3622: Clang 22
8+
* Allow calling static inline within extern inline
9+
*
10+
* This verifies that a constraint from previous standards is no longer
11+
* triggered in C2y mode. The constraint is with calling a static function
12+
* or using a static variable from an inline function with external linkage.
13+
*/
14+
15+
static void static_func(void) {} // expected-note {{declared here}}
16+
static int static_var; // expected-note {{declared here}}
17+
18+
extern inline void test(void) {
19+
static_func(); /* ped-warning {{using static function 'static_func' in an inline function with external linkage is a C2y extension}}
20+
compat-warning {{using static function 'static_func' in an inline function with external linkage is incompatible with standards before C2y}}
21+
*/
22+
static_var = 12; /* ped-warning {{using static variable 'static_var' in an inline function with external linkage is a C2y extension}}
23+
compat-warning {{using static variable 'static_var' in an inline function with external linkage is incompatible with standards before C2y}}
24+
*/
25+
}

clang/test/Sema/inline.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ static int staticFunction(void); // expected-note + {{'staticFunction' declared
1111
static struct { int x; } staticStruct; // expected-note + {{'staticStruct' declared here}}
1212

1313
inline int useStatic (void) { // expected-note 3 {{use 'static' to give inline function 'useStatic' internal linkage}}
14-
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
15-
(void)staticStruct.x; // expected-warning{{static variable 'staticStruct' is used in an inline function with external linkage}}
16-
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
14+
staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}
15+
(void)staticStruct.x; // expected-warning{{using static variable 'staticStruct' in an inline function with external linkage is a C2y extension}}
16+
return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}
1717
}
1818

1919
extern inline int useStaticFromExtern (void) { // no suggestions
20-
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
21-
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
20+
staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}
21+
return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}
2222
}
2323

2424
static inline int useStaticFromStatic (void) {
@@ -67,8 +67,8 @@ inline int useStaticMainFile (void) {
6767
#pragma clang diagnostic warning "-Wstatic-in-inline"
6868

6969
inline int useStaticAgain (void) { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' internal linkage}}
70-
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
71-
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
70+
staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}
71+
return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}
7272
}
7373

7474
#pragma clang diagnostic pop
@@ -86,8 +86,8 @@ extern inline void defineStaticVarInExtern(void) {
8686
// Check behavior of line markers.
8787
# 1 "XXX.h" 1
8888
inline int useStaticMainFileInLineMarker(void) { // expected-note 2 {{use 'static' to give inline function 'useStaticMainFileInLineMarker' internal linkage}}
89-
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
90-
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
89+
staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}
90+
return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}
9191
}
9292
# 100 "inline.c" 2
9393

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ <h2 id="c2y">C2y implementation status</h2>
349349
<tr>
350350
<td>Allow calling static inline within extern inline</td>
351351
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3622.txt">N3622</a></td>
352-
<td class="unknown" align="center">Unknown</td>
352+
<td class="unreleased" align="center">Clang 22</td>
353353
</tr>
354354
<tr>
355355
<td>Generic replacement (v. 2 of quasi-literals)</td>

0 commit comments

Comments
 (0)