[clang] 6e63f96 - [Parse] Improve diagnostic and recovery when there is an extra override in the outline method definition.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 18 01:00:36 PDT 2021
Author: Haojian Wu
Date: 2021-10-18T10:00:21+02:00
New Revision: 6e63f96e11ee9af300b166c994980d3b80cea0c7
URL: https://github.com/llvm/llvm-project/commit/6e63f96e11ee9af300b166c994980d3b80cea0c7
DIFF: https://github.com/llvm/llvm-project/commit/6e63f96e11ee9af300b166c994980d3b80cea0c7.diff
LOG: [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
Added:
clang/test/Parser/cxx-extra-virtual-specifiers.cpp
Modified:
clang/include/clang/Basic/DiagnosticParseKinds.td
clang/lib/Parse/ParseDecl.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index e82eafba99546..367da91afbae1 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 9f660e466aae0..a0871062395e6 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 0000000000000..f09883424dc8a
--- /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}}
+}
More information about the cfe-commits
mailing list