-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[C] Disallow declarations where a statement is required #92908
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
Changes from all commits
219dae0
12c89e0
c4b0d42
9a21719
c67502d
6e2e9c0
f59942f
0639525
3f00614
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 |
---|---|---|
|
@@ -239,7 +239,15 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes( | |
auto IsStmtAttr = [](ParsedAttr &Attr) { return Attr.isStmtAttr(); }; | ||
bool AllAttrsAreStmtAttrs = llvm::all_of(CXX11Attrs, IsStmtAttr) && | ||
llvm::all_of(GNUAttrs, IsStmtAttr); | ||
if (((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) || | ||
// In C, the grammar production for statement (C23 6.8.1p1) does not allow | ||
// for declarations, which is different from C++ (C++23 [stmt.pre]p1). So | ||
// in C++, we always allow a declaration, but in C we need to check whether | ||
// we're in a statement context that allows declarations. e.g., in C, the | ||
// following is invalid: if (1) int x; | ||
if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt || | ||
(StmtCtx & ParsedStmtContext::AllowDeclarationsInC) != | ||
ParsedStmtContext()) && | ||
((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) || | ||
Comment on lines
+247
to
+250
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. Can you add a standard quote? 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've added one, let me know if you're happy with it. 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. Thanks (somehow i did not get notified) |
||
isDeclarationStatement())) { | ||
SourceLocation DeclStart = Tok.getLocation(), DeclEnd; | ||
DeclGroupPtrTy Decl; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic | ||
|
||
// Test that we can parse declarations at global scope. | ||
int v; | ||
|
||
void func(void) { | ||
// Test that we can parse declarations within a compound statement. | ||
int a; | ||
{ | ||
int b; | ||
} | ||
|
||
int z = ({ // expected-warning {{use of GNU statement expression extension}} | ||
// Test that we can parse declarations within a GNU statement expression. | ||
int w = 12; | ||
w; | ||
}); | ||
|
||
// Test that we diagnose declarations where a statement is required. | ||
// See GH92775. | ||
if (1) | ||
int x; // expected-error {{expected expression}} | ||
for (;;) | ||
int c; // expected-error {{expected expression}} | ||
|
||
label: | ||
int y; // expected-warning {{label followed by a declaration is a C23 extension}} | ||
|
||
// Test that lookup works as expected. | ||
(void)a; | ||
(void)v; | ||
(void)z; | ||
(void)b; // expected-error {{use of undeclared identifier 'b'}} | ||
(void)w; // expected-error {{use of undeclared identifier 'w'}} | ||
(void)x; // expected-error {{use of undeclared identifier 'x'}} | ||
(void)c; // expected-error {{use of undeclared identifier 'c'}} | ||
(void)y; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a little mysterious, I only see that we check it but it is not obvious how it gets set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shafik L481 below