-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[C23] Complete support for WG14 N2508 #71398
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d54a5c
[C23] Complete support for WG14 N2508
AaronBallman 683ef4e
Merge remote-tracking branch 'origin/main' into aballman-c23-n2508
AaronBallman 532b1ea
Add test coverage for pedantic and compat diagnostics
AaronBallman b2c9965
Remove FIXME comment based on review feedback
AaronBallman 8981b9e
Update based on review feedback; NFC
AaronBallman 2a3f06a
Merge branch 'main' into aballman-c23-n2508
AaronBallman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,10 +235,7 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes( | |
auto IsStmtAttr = [](ParsedAttr &Attr) { return Attr.isStmtAttr(); }; | ||
bool AllAttrsAreStmtAttrs = llvm::all_of(CXX11Attrs, IsStmtAttr) && | ||
llvm::all_of(GNUAttrs, IsStmtAttr); | ||
if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt || | ||
(StmtCtx & ParsedStmtContext::AllowDeclarationsInC) != | ||
ParsedStmtContext()) && | ||
((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) || | ||
if (((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) || | ||
isDeclarationStatement())) { | ||
SourceLocation DeclStart = Tok.getLocation(), DeclEnd; | ||
DeclGroupPtrTy Decl; | ||
|
@@ -701,6 +698,18 @@ StmtResult Parser::ParseSEHLeaveStatement() { | |
return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope()); | ||
} | ||
|
||
static void DiagnoseLabelFollowedByDecl(Parser &P, const Stmt *SubStmt) { | ||
// When in C mode (but not Microsoft extensions mode), diagnose use of a | ||
// label that is followed by a declaration rather than a statement. | ||
if (!P.getLangOpts().CPlusPlus && !P.getLangOpts().MicrosoftExt && | ||
isa<DeclStmt>(SubStmt)) { | ||
P.Diag(SubStmt->getBeginLoc(), | ||
P.getLangOpts().C23 | ||
? diag::warn_c23_compat_label_followed_by_declaration | ||
: diag::ext_c_label_followed_by_declaration); | ||
} | ||
} | ||
|
||
/// ParseLabeledStatement - We have an identifier and a ':' after it. | ||
/// | ||
/// label: | ||
|
@@ -715,9 +724,10 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs, | |
assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && | ||
"Not an identifier!"); | ||
|
||
// The substatement is always a 'statement', not a 'declaration', but is | ||
// otherwise in the same context as the labeled-statement. | ||
StmtCtx &= ~ParsedStmtContext::AllowDeclarationsInC; | ||
// [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a | ||
// substatement in a selection statement, in place of the loop body in an | ||
// iteration statement, or in place of the statement that follows a label. | ||
StmtCtx &= ~ParsedStmtContext::AllowStandaloneOpenMPDirectives; | ||
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 think this deserves a comment. 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. Done |
||
|
||
Token IdentTok = Tok; // Save the whole token. | ||
ConsumeToken(); // eat the identifier. | ||
|
@@ -766,6 +776,8 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs, | |
if (SubStmt.isInvalid()) | ||
SubStmt = Actions.ActOnNullStmt(ColonLoc); | ||
|
||
DiagnoseLabelFollowedByDecl(*this, SubStmt.get()); | ||
|
||
LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(), | ||
IdentTok.getLocation()); | ||
Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs); | ||
|
@@ -784,9 +796,10 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, | |
bool MissingCase, ExprResult Expr) { | ||
assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!"); | ||
|
||
// The substatement is always a 'statement', not a 'declaration', but is | ||
// otherwise in the same context as the labeled-statement. | ||
StmtCtx &= ~ParsedStmtContext::AllowDeclarationsInC; | ||
// [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a | ||
// substatement in a selection statement, in place of the loop body in an | ||
// iteration statement, or in place of the statement that follows a label. | ||
StmtCtx &= ~ParsedStmtContext::AllowStandaloneOpenMPDirectives; | ||
|
||
// It is very common for code to contain many case statements recursively | ||
// nested, as in (but usually without indentation): | ||
|
@@ -912,6 +925,7 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, | |
// Broken sub-stmt shouldn't prevent forming the case statement properly. | ||
if (SubStmt.isInvalid()) | ||
SubStmt = Actions.ActOnNullStmt(SourceLocation()); | ||
DiagnoseLabelFollowedByDecl(*this, SubStmt.get()); | ||
Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get()); | ||
} | ||
|
||
|
@@ -927,9 +941,10 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, | |
StmtResult Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx) { | ||
assert(Tok.is(tok::kw_default) && "Not a default stmt!"); | ||
|
||
// The substatement is always a 'statement', not a 'declaration', but is | ||
// otherwise in the same context as the labeled-statement. | ||
StmtCtx &= ~ParsedStmtContext::AllowDeclarationsInC; | ||
// [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a | ||
// substatement in a selection statement, in place of the loop body in an | ||
// iteration statement, or in place of the statement that follows a label. | ||
StmtCtx &= ~ParsedStmtContext::AllowStandaloneOpenMPDirectives; | ||
|
||
SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. | ||
|
||
|
@@ -963,6 +978,7 @@ StmtResult Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx) { | |
if (SubStmt.isInvalid()) | ||
SubStmt = Actions.ActOnNullStmt(ColonLoc); | ||
|
||
DiagnoseLabelFollowedByDecl(*this, SubStmt.get()); | ||
return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, | ||
SubStmt.get(), getCurScope()); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
// RUN: %clang_cc1 -verify -std=c2x %s | ||
// RUN: %clang_cc1 -verify -std=c23 %s | ||
// RUN: %clang_cc1 -verify=pedantic -std=c11 -pedantic %s | ||
// RUN: %clang_cc1 -verify=compat -std=c23 -Wpre-c23-compat %s | ||
|
||
// expected-no-diagnostics | ||
|
||
/* WG14 N2508: yes | ||
* Free positioning of labels inside compound statements | ||
*/ | ||
void test() { | ||
void test(void) { | ||
{ | ||
inner: | ||
} | ||
} /* pedantic-warning {{label at end of compound statement is a C23 extension}} | ||
compat-warning {{label at end of compound statement is incompatible with C standards before C23}} | ||
*/ | ||
|
||
switch (1) { | ||
case 1: | ||
} | ||
} /* pedantic-warning {{label at end of compound statement is a C23 extension}} | ||
compat-warning {{label at end of compound statement is incompatible with C standards before C23}} | ||
*/ | ||
|
||
{ | ||
multiple: labels: on: a: line: | ||
} | ||
} /* pedantic-warning {{label at end of compound statement is a C23 extension}} | ||
compat-warning {{label at end of compound statement is incompatible with C standards before C23}} | ||
*/ | ||
|
||
final: | ||
} | ||
} /* pedantic-warning {{label at end of compound statement is a C23 extension}} | ||
compat-warning {{label at end of compound statement is incompatible with C standards before C23}} | ||
*/ | ||
|
||
void test_labels(void) { | ||
label: | ||
int i = 0; /* pedantic-warning {{label followed by a declaration is a C23 extension}} | ||
compat-warning {{label followed by a declaration is incompatible with C standards before C23}} | ||
*/ | ||
|
||
switch (i) { | ||
case 1: | ||
_Static_assert(1, ""); /* pedantic-warning {{label followed by a declaration is a C23 extension}} | ||
compat-warning {{label followed by a declaration is incompatible with C standards before C23}} | ||
*/ | ||
default: | ||
_Static_assert(1, ""); /* pedantic-warning {{label followed by a declaration is a C23 extension}} | ||
compat-warning {{label followed by a declaration is incompatible with C standards before C23}} | ||
*/ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.