-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] support Wold-style-declaration #78837
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 all commits
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 |
---|---|---|
|
@@ -3348,6 +3348,7 @@ void Parser::ParseDeclarationSpecifiers( | |
while (true) { | ||
bool isInvalid = false; | ||
bool isStorageClass = false; | ||
bool isFunctionSpecifier = false; | ||
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. Hmmm, what's obsoleted are storage class specifiers that are not at the start of the declaration. e.g.,
and function specifiers are not storage class specifiers. So technically, this is not obsolete:
GCC does warn about this, and I think that's reasonable to also mimic. I'll check with WG14 whether function specifiers should also be obsoleted similar to storage class specifiers. |
||
const char *PrevSpec = nullptr; | ||
unsigned DiagID = 0; | ||
|
||
|
@@ -4092,6 +4093,7 @@ void Parser::ParseDeclarationSpecifiers( | |
// function-specifier | ||
case tok::kw_inline: | ||
isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); | ||
isFunctionSpecifier = true; | ||
break; | ||
case tok::kw_virtual: | ||
// C++ for OpenCL does not allow virtual function qualifier, to avoid | ||
|
@@ -4104,6 +4106,7 @@ void Parser::ParseDeclarationSpecifiers( | |
isInvalid = true; | ||
} else { | ||
isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); | ||
isFunctionSpecifier = true; | ||
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. C doesn't have virtual functions, so no need for this change. |
||
} | ||
break; | ||
case tok::kw_explicit: { | ||
|
@@ -4140,12 +4143,14 @@ void Parser::ParseDeclarationSpecifiers( | |
} | ||
isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID, | ||
ExplicitSpec, CloseParenLoc); | ||
isFunctionSpecifier = true; | ||
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. C also doesn't have |
||
break; | ||
} | ||
case tok::kw__Noreturn: | ||
if (!getLangOpts().C11) | ||
Diag(Tok, diag::ext_c11_feature) << Tok.getName(); | ||
isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); | ||
isFunctionSpecifier = true; | ||
break; | ||
|
||
// alignment-specifier | ||
|
@@ -4552,6 +4557,12 @@ void Parser::ParseDeclarationSpecifiers( | |
continue; | ||
} | ||
|
||
unsigned Specs = DS.getParsedSpecifiers(); | ||
if (!getLangOpts().CPlusPlus && (isFunctionSpecifier || isStorageClass)) { | ||
if (Specs & DeclSpec::PQ_TypeQualifier || DS.hasTypeSpecifier()) | ||
Diag(Tok, diag::warn_old_style_declaration) << Tok.getName(); | ||
} | ||
|
||
DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation()); | ||
|
||
// If the specifier wasn't legal, issue a diagnostic. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||||||
// RUN: %clang_cc1 -fsyntax-only -verify -Wold-style-declaration %s | ||||||||||||
// RUN: %clang_cc1 -fsyntax-only -verify -Wextra %s | ||||||||||||
|
||||||||||||
Comment on lines
+2
to
+3
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. It would be good to add a RUN line along these lines:
Suggested change
and group the code that's invalid in C++ under |
||||||||||||
static int x0; | ||||||||||||
int __attribute__ ((aligned (16))) static x1; // expected-warning {{'static' is not at beginning of declaration}} | ||||||||||||
|
||||||||||||
extern int x2; | ||||||||||||
int extern x3; // expected-warning {{'extern' is not at beginning of declaration}} | ||||||||||||
|
||||||||||||
typedef int x4; | ||||||||||||
int typedef x5; // expected-warning {{'typedef' is not at beginning of declaration}} | ||||||||||||
|
||||||||||||
void g (int); | ||||||||||||
|
||||||||||||
void | ||||||||||||
f (void) | ||||||||||||
{ | ||||||||||||
auto int x6 = 0; | ||||||||||||
int auto x7 = 0; // expected-warning {{'auto' is not at beginning of declaration}} | ||||||||||||
Comment on lines
+18
to
+19
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. This is going to cause problems when we update to |
||||||||||||
register int x8 = 0; | ||||||||||||
int register x9 = 0; // expected-warning {{'register' is not at beginning of declaration}} | ||||||||||||
g (x6 + x7 + x8 + x9); | ||||||||||||
} | ||||||||||||
|
||||||||||||
const static int x10; // expected-warning {{'static' is not at beginning of declaration}} | ||||||||||||
|
||||||||||||
/* Attributes are OK before storage class specifiers, since some | ||||||||||||
attributes are like such specifiers themselves. */ | ||||||||||||
|
||||||||||||
__attribute__((format(printf, 1, 2))) static void h (const char *, ...); | ||||||||||||
__attribute__((format(printf, 1, 2))) void static i (const char *, ...); // expected-warning {{'static' is not at beginning of declaration}} | ||||||||||||
|
||||||||||||
static __thread int var = 5; // not-expected-warning {{'__thread' is not at beginning of declaration}} | ||||||||||||
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. You should add tests for: I think it also makes sense to add a test for Also, please add a newline to the end of the file. |
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.
We don't typically add new diagnostics that are off by default (though this one is in
-Wextra
so it has a chance of being enabled by users). I actually wonder if we want to enable this by default; the standard makes this an obsolescent feature (see C23 6.11.5p1) and so having an on-by-default warning actually helps alert users to potential future code breakage.My intuition is that this would not be an overly chatty diagnostic to enable by default; do you have access to a large corpus of C code you could try to compile with your patch?