From: Fariborz Jahanian Date: Tue, 22 Feb 2011 23:17:49 +0000 (+0000) Subject: Provide Fixit warning when 'auto' is intended as storage X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=12e3ecec906f65580059a9d8555849a272c2db81;p=clang Provide Fixit warning when 'auto' is intended as storage specifier in legacy code. Patch is reviewed offline by Doug. // rdar://9036633. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126261 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index 7e20d20cd2..9a68af9c45 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -28,6 +28,10 @@ def ext_extra_struct_semi : Extension< def ext_extra_ivar_semi : Extension< "extra ';' inside instance variable list">; +def auto_storage_class : ExtWarn< + "'auto' storage class specifier is redundant and will be " + "removed in future releases">; + def ext_duplicate_declspec : Extension<"duplicate '%0' declaration specifier">; def ext_plain_complex : ExtWarn< "plain '_Complex' requires a type specifier; assuming '_Complex double'">; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 2999fdf5d4..5828bfa114 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -1246,9 +1246,18 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, DiagID, getLang()); break; case tok::kw_auto: - if (getLang().CPlusPlus0x || getLang().ObjC1) - isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, - DiagID); + if (getLang().CPlusPlus0x || getLang().ObjC2) { + if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { + isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, + DiagID, getLang()); + if (!isInvalid) + Diag(Tok, diag::auto_storage_class) + << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); + } + else + isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, + DiagID); + } else isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, DiagID, getLang()); @@ -1461,6 +1470,7 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, else Diag(Tok, DiagID) << PrevSpec; } + DS.SetRangeEnd(Tok.getLocation()); ConsumeToken(); } diff --git a/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp b/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp index 81f1a9f96e..b675fb8013 100644 --- a/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp +++ b/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp @@ -39,7 +39,7 @@ void p3example() { auto x = 5; const auto *v = &x, u = 6; static auto y = 0.0; - auto int r; // expected-error{{cannot combine with previous}} expected-error{{requires an initializer}} + auto int r; // expected-warning {{'auto' storage class specifier is redundant and will be removed in future releases}} same xHasTypeInt; same vHasTypeConstIntPtr; diff --git a/test/FixIt/auto-fixit.m b/test/FixIt/auto-fixit.m new file mode 100644 index 0000000000..d09f04c2b7 --- /dev/null +++ b/test/FixIt/auto-fixit.m @@ -0,0 +1,11 @@ +/* RUN: cp %s %t + RUN: %clang_cc1 -x objective-c -fixit %t + RUN: %clang_cc1 -x objective-c -Werror %t + */ + +// rdar://9036633 + +int main() { + auto int i = 0; + return i; +} diff --git a/test/SemaCXX/auto-cxx0x.cpp b/test/SemaCXX/auto-cxx0x.cpp index 654acb5ad2..f9246beff9 100644 --- a/test/SemaCXX/auto-cxx0x.cpp +++ b/test/SemaCXX/auto-cxx0x.cpp @@ -1,5 +1,5 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++0x void f() { - auto int a; // expected-error{{cannot combine with previous 'auto' declaration specifier}} // expected-error{{declaration of variable 'a' with type 'auto' requires an initializer}} + auto int a; // expected-warning {{'auto' storage class specifier is redundant and will be removed in future releases}} int auto b; // expected-error{{cannot combine with previous 'int' declaration specifier}} } diff --git a/test/SemaObjC/auto-objective-c.m b/test/SemaObjC/auto-objective-c.m index e5d0179aa6..5467e24a79 100644 --- a/test/SemaObjC/auto-objective-c.m +++ b/test/SemaObjC/auto-objective-c.m @@ -25,3 +25,9 @@ typedef int (^P) (int x); return my_block; } @end + + +// rdar://9036633 +int main() { + auto int auto_i = 7; // expected-warning {{'auto' storage class specifier is redundant and will be removed in future releases}} +}