[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
This commit is contained in:
Haojian Wu 2021-10-12 16:13:09 +02:00
parent 3f0b178de2
commit 6e63f96e11
3 changed files with 34 additions and 0 deletions

View File

@ -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<

View File

@ -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.

View File

@ -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}}
}