diff options
author | Haojian Wu <hokein.wu@gmail.com> | 2021-10-12 16:13:09 +0200 |
---|---|---|
committer | Haojian Wu <hokein.wu@gmail.com> | 2021-10-18 10:00:21 +0200 |
commit | 6e63f96e11ee9af300b166c994980d3b80cea0c7 (patch) | |
tree | ad2efe4e7bcb6983c6e7e75da9e43d651c94b903 | |
parent | 3f0b178de21ee82791a6ebe198314f14c0287a44 (diff) | |
download | llvm-6e63f96e11ee9af300b166c994980d3b80cea0c7.tar.gz |
[Parse] Improve diagnostic and recovery when there is an extra override in the outline method definition.
The clang behavior was poor before this patch:
```
void B::foo() override {}
// Before: clang emited "expcted function body after function
// declarator", and skiped all contents until it hits a ";", the
// following function f() is discarded.
// VS
// Now "override is not allowed" with a remove fixit, and following f()
// is retained.
void f();
```
Differential Revision: https://reviews.llvm.org/D111883
-rw-r--r-- | clang/include/clang/Basic/DiagnosticParseKinds.td | 3 | ||||
-rw-r--r-- | clang/lib/Parse/ParseDecl.cpp | 12 | ||||
-rw-r--r-- | clang/test/Parser/cxx-extra-virtual-specifiers.cpp | 19 |
3 files changed, 34 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td index e82eafba9954..367da91afbae 100644 --- a/clang/include/clang/Basic/DiagnosticParseKinds.td +++ b/clang/include/clang/Basic/DiagnosticParseKinds.td @@ -958,6 +958,9 @@ def err_duplicate_class_virt_specifier : Error< def err_duplicate_virt_specifier : Error< "class member already marked '%0'">; +def err_virt_specifier_outside_class : Error< + "'%0' specifier is not allowed outside a class definition">; + def err_expected_parameter_pack : Error< "expected the name of a parameter pack">; def err_paren_sizeof_parameter_pack : Error< diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 9f660e466aae..a0871062395e 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -2021,6 +2021,18 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, Actions.CodeCompleteAfterFunctionEquals(D); return nullptr; } + // We're at the point where the parsing of function declarator is finished. + // + // A common error is that users accidently add a virtual specifier + // (e.g. override) in an out-line method definition. + // We attempt to recover by stripping all these specifiers coming after + // the declarator. + while (auto Specifier = isCXX11VirtSpecifier()) { + Diag(Tok, diag::err_virt_specifier_outside_class) + << VirtSpecifiers::getSpecifierName(Specifier) + << FixItHint::CreateRemoval(Tok.getLocation()); + ConsumeToken(); + } // Look at the next token to make sure that this isn't a function // declaration. We have to check this because __attribute__ might be the // start of a function definition in GCC-extended K&R C. diff --git a/clang/test/Parser/cxx-extra-virtual-specifiers.cpp b/clang/test/Parser/cxx-extra-virtual-specifiers.cpp new file mode 100644 index 000000000000..f09883424dc8 --- /dev/null +++ b/clang/test/Parser/cxx-extra-virtual-specifiers.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -fdiagnostics-parseable-fixits %s + +class A { + virtual void foo(); +}; +class B : public A { + void foo() override; +}; + +void B::foo() override {} // expected-error {{'override' specifier is not allowed outside a class definition}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:15-[[@LINE-1]]:24}:"" + +void f1() override; // expected-error {{'override' specifier is not allowed}} + +void f2() override {} // expected-error {{'override' specifier is not allowed}} + +void test() { + void f() override; // expected-error {{'override' specifier is not allowed}} +} |