From 0061e681a3296ac9016891729e03e38ebb235bdc Mon Sep 17 00:00:00 2001 From: peter klausler Date: Thu, 30 Sep 2021 15:58:38 -0700 Subject: [PATCH] [flang] Better error recovery for missing THEN in ELSE IF The THEN keyword in the "ELSE IF (test) THEN" statement is useless syntactically, and to omit it is a common error (at least for me!) that has poor error recovery. This patch changes the parser to cough up a simple "expected 'THEN'" and still recognize the rest of the IF construct. Differential Revision: https://reviews.llvm.org/D110952 --- flang/lib/Evaluate/check-expression.cpp | 2 +- flang/lib/Parser/executable-parsers.cpp | 2 +- flang/test/Parser/elseif-then.f90 | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 flang/test/Parser/elseif-then.f90 diff --git a/flang/lib/Evaluate/check-expression.cpp b/flang/lib/Evaluate/check-expression.cpp index cb910ca..2e0cc80 100644 --- a/flang/lib/Evaluate/check-expression.cpp +++ b/flang/lib/Evaluate/check-expression.cpp @@ -93,7 +93,7 @@ bool IsConstantExprHelper::IsConstantStructureConstructorComponent( } bool IsConstantExprHelper::operator()(const ProcedureRef &call) const { - // LBOUND, UBOUND, and SIZE with DIM= arguments will have been reritten + // LBOUND, UBOUND, and SIZE with DIM= arguments will have been rewritten // into DescriptorInquiry operations. if (const auto *intrinsic{std::get_if(&call.proc().u)}) { if (intrinsic->name == "kind" || diff --git a/flang/lib/Parser/executable-parsers.cpp b/flang/lib/Parser/executable-parsers.cpp index a0b5cf2..66b217a 100644 --- a/flang/lib/Parser/executable-parsers.cpp +++ b/flang/lib/Parser/executable-parsers.cpp @@ -310,7 +310,7 @@ TYPE_CONTEXT_PARSER("IF construct"_en_US, many(construct( unambiguousStatement(construct( "ELSE IF" >> parenthesized(scalarLogicalExpr), - "THEN" >> maybe(name))), + recovery("THEN"_tok, ok) >> maybe(name))), block)), maybe(construct( statement(construct("ELSE" >> maybe(name))), block)), diff --git a/flang/test/Parser/elseif-then.f90 b/flang/test/Parser/elseif-then.f90 new file mode 100644 index 0000000..29ec164 --- /dev/null +++ b/flang/test/Parser/elseif-then.f90 @@ -0,0 +1,8 @@ +! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s +! CHECK-NOT: expected '=>' +! CHECK: error: expected 'THEN' +if (.false.) then +else if (.false.) +else +end if +end -- 2.7.4