-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[C2y] Allow static local variables in inline functions with external … #167086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8111,6 +8111,11 @@ NamedDecl *Sema::ActOnVariableDeclarator( | |
| // An inline definition of a function with external linkage shall | ||
| // not contain a definition of a modifiable object with static or | ||
| // thread storage duration... | ||
| // | ||
| // WG14 N3622 which removed the constraint entirely in C2y. It is left | ||
| // enabled in earlier language modes because this is a constraint in those | ||
| // language modes. But in C2y mode, we still want to issue the "incompatible | ||
| // with previous standards" diagnostic, too. | ||
|
Comment on lines
+8116
to
+8118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary comment? We should issue "incompatible" diagnostic for all features.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do see your point. However, I wanted to ensure it was as consistent as possible with the linked commit. |
||
| // We only apply this when the function is required to be defined | ||
| // elsewhere, i.e. when the function is not 'extern inline'. Note | ||
| // that a local variable with thread storage duration still has to | ||
|
|
@@ -8120,8 +8125,15 @@ NamedDecl *Sema::ActOnVariableDeclarator( | |
| !NewVD->getType().isConstQualified()) { | ||
| FunctionDecl *CurFD = getCurFunctionDecl(); | ||
| if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { | ||
| Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||
| diag::warn_static_local_in_extern_inline); | ||
| unsigned DiagID; | ||
| if (getLangOpts().C2y) | ||
| DiagID = diag::warn_c2y_compat_static_local_in_extern_inline; | ||
| else if (getSourceManager().isInMainFile(D.getBeginLoc())) | ||
| DiagID = diag::ext_static_local_in_extern_inline_quiet; | ||
Pranjal095 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else | ||
| DiagID = diag::ext_static_local_in_extern_inline; | ||
|
|
||
| Diag(D.getDeclSpec().getStorageClassSpecLoc(), DiagID); | ||
| MaybeSuggestAddingStaticToDecl(CurFD); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // RUN: %clang_cc1 -verify=good -pedantic -Wall -std=c2y %s | ||
| // RUN: %clang_cc1 -verify=compat,expected -pedantic -Wall -Wpre-c2y-compat -std=c2y %s | ||
| // RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -std=c23 %s | ||
| // RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -std=c17 %s | ||
| // good-no-diagnostics | ||
|
|
||
| /* WG14 N3622: Clang 22 | ||
| * Allow static local variables in extern inline functions | ||
| * | ||
| * This verifies that a constraint from previous standards is no longer | ||
| * triggered in C2y mode. The constraint is regarding static local | ||
| * variables in inline functions with external linkage. | ||
| */ | ||
|
|
||
| inline void func1(void) { // expected-note {{use 'static' to give inline function 'func1' internal linkage}} | ||
| static int x = 0; /* ped-warning {{non-constant static local variable in an inline function with external linkage is a C2y extension}} | ||
| compat-warning {{non-constant static local variable in an inline function with external linkage is incompatible with standards before C2y}} | ||
| */ | ||
| (void)x; | ||
| } | ||
|
|
||
| inline void func2(void) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this test used for?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first test is to ensure that the new C2y feature works with the specified diagnostics. The second test is to ensure that no false positives have popped up during the process. |
||
| static const int x = 0; | ||
| (void)x; | ||
| } | ||
Pranjal095 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.