From 4867347e82648d3baf09524b98b09c297a5a198f Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 28 Jan 2011 06:07:34 +0000 Subject: [PATCH] PR9037: Allow override, final, and new as an extension on inline members. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124477 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticParseKinds.td | 2 ++ include/clang/Parse/Parser.h | 3 ++- include/clang/Sema/DeclSpec.h | 2 ++ lib/Parse/ParseCXXInlineMethods.cpp | 16 +++++++++--- lib/Parse/ParseDeclCXX.cpp | 7 ++++-- test/CXX/class/class.mem/p8-0x-pedantic.cpp | 14 +++++++++++ test/CXX/class/class.mem/p8-0x.cpp | 28 +++++++++++++++++++++ 7 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 test/CXX/class/class.mem/p8-0x-pedantic.cpp diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index 7ce5017e18..4142fcca65 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -394,6 +394,8 @@ def warn_deleted_function_accepted_as_extension: ExtWarn< // C++0x override control def ext_override_control_keyword : Extension< "'%0' keyword accepted as a C++0x extension">, InGroup; +def ext_override_inline: Extension< + "'%0' keyword only allowed in declarations, allowed as an extension">; def err_duplicate_virt_specifier : Error< "class member already marked '%0'">; diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index 7f22a49533..e3ed3e8c8b 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -910,7 +910,8 @@ private: void PopParsingClass(); Decl *ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D, - const ParsedTemplateInfo &TemplateInfo); + const ParsedTemplateInfo &TemplateInfo, + const VirtSpecifiers& VS); void ParseLexedMethodDeclarations(ParsingClass &Class); void ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM); void ParseLexedMethodDefs(ParsingClass &Class); diff --git a/include/clang/Sema/DeclSpec.h b/include/clang/Sema/DeclSpec.h index 59962ae3b7..742cfe96af 100644 --- a/include/clang/Sema/DeclSpec.h +++ b/include/clang/Sema/DeclSpec.h @@ -1513,6 +1513,8 @@ public: bool isNewSpecified() const { return Specifiers & VS_New; } SourceLocation getNewLoc() const { return VS_newLoc; } + void clear() { Specifiers = 0; } + static const char *getSpecifierName(Specifier VS); private: diff --git a/lib/Parse/ParseCXXInlineMethods.cpp b/lib/Parse/ParseCXXInlineMethods.cpp index 0a5a586f73..d62e71836f 100644 --- a/lib/Parse/ParseCXXInlineMethods.cpp +++ b/lib/Parse/ParseCXXInlineMethods.cpp @@ -21,7 +21,8 @@ using namespace clang; /// Declarator is a well formed C++ inline method definition. Now lex its body /// and store its tokens for parsing after the C++ class is complete. Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D, - const ParsedTemplateInfo &TemplateInfo) { + const ParsedTemplateInfo &TemplateInfo, + const VirtSpecifiers& VS) { assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) && "Current token not a '{', ':' or 'try'!"); @@ -35,11 +36,18 @@ Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D, // FIXME: Friend templates FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true, move(TemplateParams)); - else // FIXME: pass template information through + else { // FIXME: pass template information through + if (VS.isOverrideSpecified()) + Diag(VS.getOverrideLoc(), diag::ext_override_inline) << "override"; + if (VS.isFinalSpecified()) + Diag(VS.getFinalLoc(), diag::ext_override_inline) << "final"; + if (VS.isNewSpecified()) + Diag(VS.getNewLoc(), diag::ext_override_inline) << "new"; + FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, move(TemplateParams), 0, - VirtSpecifiers(), 0, - /*IsDefinition*/true); + VS, 0, /*IsDefinition*/true); + } HandleMemberFunctionDefaultArgs(D, FnD); diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index b077f73baa..9466ebc844 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -1531,6 +1531,7 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, } ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); + VirtSpecifiers VS; if (Tok.isNot(tok::colon)) { // Don't parse FOO:BAR as if it were a typo for FOO::BAR. @@ -1547,6 +1548,8 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, return; } + ParseOptionalCXX0XVirtSpecifierSeq(VS); + // If attributes exist after the declarator, but before an '{', parse them. MaybeParseGNUAttributes(DeclaratorInfo); @@ -1579,7 +1582,7 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, return; } - ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo); + ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS); // Consume the optional ';' if (Tok.is(tok::semi)) ConsumeToken(); @@ -1609,7 +1612,6 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, SkipUntil(tok::comma, true, true); } - VirtSpecifiers VS; ParseOptionalCXX0XVirtSpecifierSeq(VS); // pure-specifier: @@ -1689,6 +1691,7 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, // Parse the next declarator. DeclaratorInfo.clear(); + VS.clear(); BitfieldSize = 0; Init = 0; Deleted = false; diff --git a/test/CXX/class/class.mem/p8-0x-pedantic.cpp b/test/CXX/class/class.mem/p8-0x-pedantic.cpp new file mode 100644 index 0000000000..a4b775c191 --- /dev/null +++ b/test/CXX/class/class.mem/p8-0x-pedantic.cpp @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++0x -pedantic -verify %s + +namespace inline_extension { + struct Base1 { + virtual void f() {} + }; + + struct B : Base1 { + virtual void f() override {} // expected-warning {{'override' keyword only allowed in declarations, allowed as an extension}} + virtual void g() final {} // expected-warning {{'final' keyword only allowed in declarations, allowed as an extension}} + virtual void h() new {} // expected-warning {{'new' keyword only allowed in declarations, allowed as an extension}} + }; +} + diff --git a/test/CXX/class/class.mem/p8-0x.cpp b/test/CXX/class/class.mem/p8-0x.cpp index ce676ae6ff..bf1b4c177b 100644 --- a/test/CXX/class/class.mem/p8-0x.cpp +++ b/test/CXX/class/class.mem/p8-0x.cpp @@ -11,10 +11,12 @@ struct A : Base1 { }; struct Base2 { + virtual void e1(), e2(); virtual void f(); }; struct B : Base2 { + virtual void e1() override, e2(int); // No error. virtual void f() override; void g() override; // expected-error {{only virtual member functions can be marked 'override'}} int h override; // expected-error {{only virtual member functions can be marked 'override'}} @@ -25,3 +27,29 @@ struct C { void g() final; // expected-error {{only virtual member functions can be marked 'final'}} int h final; // expected-error {{only virtual member functions can be marked 'final'}} }; + +namespace inline_extension { + struct Base1 { + virtual void g() {} + }; + + struct A : Base1 { + virtual void f() new new {} // expected-error {{class member already marked 'new'}} + virtual void g() override override {} // expected-error {{class member already marked 'override'}} + virtual void h() final final {} // expected-error {{class member already marked 'final'}} + }; + + struct Base2 { + virtual void f(); + }; + + struct B : Base2 { + virtual void f() override {} + void g() override {} // expected-error {{only virtual member functions can be marked 'override'}} + }; + + struct C { + virtual void f() final {} + void g() final {} // expected-error {{only virtual member functions can be marked 'final'}} + }; +} -- 2.40.0