- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[clang] Reject 'auto' storage class with type specifier in C++ #166004
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 | ||||
|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -4095,11 +4095,23 @@ void Parser::ParseDeclarationSpecifiers( | |||||
| case tok::kw_auto: | ||||||
| if (getLangOpts().CPlusPlus11 || getLangOpts().C23) { | ||||||
| if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { | ||||||
| isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, | ||||||
| PrevSpec, DiagID, Policy); | ||||||
| if (!isInvalid && !getLangOpts().C23) | ||||||
| Diag(Tok, diag::ext_auto_storage_class) | ||||||
| << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); | ||||||
| // In C++ (not C23), 'auto' cannot be combined with a type specifier. | ||||||
| // OpenCL is excluded here because Sema emits a more specific error | ||||||
| // message for OpenCL (e.g., "does not support the 'auto' storage | ||||||
| // class specifier"). | ||||||
| if (!getLangOpts().C23 && !getLangOpts().OpenCLCPlusPlus) { | ||||||
| // In C++11+, 'auto' cannot be combined with a type specifier. | ||||||
| // Don't set the storage class specifier to avoid Sema emitting a | ||||||
| // redundant error (GCC only emits one error for this case: "two or | ||||||
| // more data types in declaration"). | ||||||
| isInvalid = true; | ||||||
| PrevSpec = "auto"; | ||||||
| 
         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. 
        Suggested change
       
    
  | 
||||||
| DiagID = diag::err_auto_type_specifier; | ||||||
| } else { | ||||||
| // C23 allows 'auto' as storage class with type specifier. | ||||||
| isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, | ||||||
| PrevSpec, DiagID, Policy); | ||||||
| } | ||||||
| } else | ||||||
| isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, | ||||||
| DiagID, Policy); | ||||||
| 
          
            
          
           | 
    ||||||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -5,14 +5,18 @@ | |
| // The auto or register specifiers can be applied only to names of objects | ||
| // declared in a block (6.3) or to function parameters (8.4). | ||
| 
     | 
||
| auto int ao; // expected-error {{illegal storage class on file-scoped variable}} | ||
| auto int ao; | ||
| #if __cplusplus >= 201103L // C++11 or later | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #else | ||
| // expected-error@-4 {{illegal storage class on file-scoped variable}} | ||
| 
         
      Comment on lines
    
      +10
     to 
      +12
    
   
  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'd be slightly easier to review this file if we used   | 
||
| #endif | ||
| 
     | 
||
| auto void af(); // expected-error {{illegal storage class on function}} | ||
| auto void af(); | ||
| #if __cplusplus >= 201103L // C++11 or later | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #else | ||
| // expected-error@-4 {{illegal storage class on function}} | ||
| #endif | ||
| 
     | 
||
| register int ro; // expected-error {{illegal storage class on file-scoped variable}} | ||
| 
        
          
        
         | 
    @@ -25,13 +29,17 @@ register int ro; // expected-error {{illegal storage class on file-scoped variab | |
| register void rf(); // expected-error {{illegal storage class on function}} | ||
| 
     | 
||
| struct S { | ||
| auto int ao; // expected-error {{storage class specified for a member declaration}} | ||
| auto int ao; | ||
| #if __cplusplus >= 201103L // C++11 or later | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #else | ||
| // expected-error@-4 {{storage class specified for a member declaration}} | ||
| #endif | ||
| auto void af(); // expected-error {{storage class specified for a member declaration}} | ||
| auto void af(); | ||
| #if __cplusplus >= 201103L // C++11 or later | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #else | ||
| // expected-error@-4 {{storage class specified for a member declaration}} | ||
| #endif | ||
| 
     | 
||
| register int ro; // expected-error {{storage class specified for a member declaration}} | ||
| 
        
          
        
         | 
    @@ -40,19 +48,21 @@ struct S { | |
| 
     | 
||
| void foo(auto int ap, register int rp) { | ||
| #if __cplusplus >= 201703L | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| // expected-error@-3 {{ISO C++17 does not allow 'register' storage class specifier}} | ||
| #elif __cplusplus >= 201103L | ||
| // expected-warning@-5 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-5 {{'auto' cannot be combined with a type specifier}} | ||
| // expected-warning@-6 {{'register' storage class specifier is deprecated}} | ||
| #endif | ||
| auto int abo; | ||
| #if __cplusplus >= 201103L // C++11 or later | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #endif | ||
| auto void abf(); // expected-error {{illegal storage class on function}} | ||
| auto void abf(); | ||
| #if __cplusplus >= 201103L // C++11 or later | ||
| // expected-warning@-2 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}} | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #else | ||
| // expected-error@-4 {{illegal storage class on function}} | ||
| #endif | ||
| 
     | 
||
| register int rbo; | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -42,7 +42,12 @@ void p3example() { | |
| static auto y = 0.0; | ||
| 
         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 line should also be diagnosed in C++98 mode... this seems to be a preexisting bug: https://godbolt.org/z/3cza618cx (we have a similar bug with  GitHub is too useless to let me put this comment on the correct line, but line 40 has a similar C++98 bug: https://godbolt.org/z/xEozMhe6z because there's no type specifier there, and line 41 does as well for the same reason. Hmmm, it seems we're treating type deduction as an extension in C++98 mode, but we don't list it in the language extensions page as such and we don't issue pedantic diagnostics for it. @zygoloid do you recall if this was an intentional extension or not? Regardless, because this is existing behavior, nothing needs to change as part of this PR. 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 see a diagnostic for it appearing in all of your test cases: ... and  If we want that table to be complete, it's probably worth going through the diagnostics in the  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. 
 Ugh, yeah, I am seeing that now, I think I was looking at the wrong output before. :-D Yeah, I think the table just needs to be updated, that doesn't need to be a part of this PR though. Thanks!  | 
||
| // In C++98: 'auto' storage class specifier is redundant and incompatible with C++0x | ||
| // In C++0x: 'auto' storage class specifier is not permitted in C++0x, and will not be supported in future releases | ||
| auto int r; // expected-warning {{'auto' storage class specifier}} | ||
| auto int r; | ||
| #if __cplusplus >= 201103L | ||
| // expected-error@-2 {{'auto' cannot be combined with a type specifier}} | ||
| #else | ||
| // expected-warning@-4 {{'auto' storage class specifier}} | ||
| #endif | ||
| 
     | 
||
| same<__typeof(x), int> xHasTypeInt; | ||
| same<__typeof(v), const int*> vHasTypeConstIntPtr; | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s | ||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s | ||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s | ||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s | ||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 %s | ||
| 
     | 
||
| // Test that 'auto' cannot be combined with a type specifier in C++. | ||
| void f() { | ||
| auto int x = 1; // expected-error {{'auto' cannot be combined with a type specifier}} | ||
| 
         
      Comment on lines
    
      +8
     to 
      +9
    
   
  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. One more test to add:  | 
||
| auto char c = 'a'; // expected-error {{'auto' cannot be combined with a type specifier}} | ||
| auto float f = 1.0f; // expected-error {{'auto' cannot be combined with a type specifier}} | ||
| auto double d = 1.0; // expected-error {{'auto' cannot be combined with a type specifier}} | ||
| auto long l = 1L; // expected-error {{'auto' cannot be combined with a type specifier}} | ||
| } | ||
| 
     | 
||
| // Test that regular 'auto' (type deduction) still works in C++. | ||
| void h() { | ||
| auto x = 1; | ||
| auto y = 2.0; | ||
| auto z = 'c'; | ||
| } | ||
| 
     | 
||
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 pr makes
ext_auto_storage_classunused, so we should remove it