[flang-commits] [flang] 0061e68 - [flang] Better error recovery for missing THEN in ELSE IF

peter klausler via flang-commits flang-commits at lists.llvm.org
Mon Oct 4 09:38:25 PDT 2021


Author: peter klausler
Date: 2021-10-04T09:37:53-07:00
New Revision: 0061e681a3296ac9016891729e03e38ebb235bdc

URL: https://github.com/llvm/llvm-project/commit/0061e681a3296ac9016891729e03e38ebb235bdc
DIFF: https://github.com/llvm/llvm-project/commit/0061e681a3296ac9016891729e03e38ebb235bdc.diff

LOG: [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

Added: 
    flang/test/Parser/elseif-then.f90

Modified: 
    flang/lib/Evaluate/check-expression.cpp
    flang/lib/Parser/executable-parsers.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/check-expression.cpp b/flang/lib/Evaluate/check-expression.cpp
index cb910caeecf01..2e0cc8087b8a0 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<SpecificIntrinsic>(&call.proc().u)}) {
     if (intrinsic->name == "kind" ||

diff  --git a/flang/lib/Parser/executable-parsers.cpp b/flang/lib/Parser/executable-parsers.cpp
index a0b5cf232abf7..66b217a698fdd 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<IfConstruct::ElseIfBlock>(
             unambiguousStatement(construct<ElseIfStmt>(
                 "ELSE IF" >> parenthesized(scalarLogicalExpr),
-                "THEN" >> maybe(name))),
+                recovery("THEN"_tok, ok) >> maybe(name))),
             block)),
         maybe(construct<IfConstruct::ElseBlock>(
             statement(construct<ElseStmt>("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 0000000000000..29ec164d548be
--- /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


        


More information about the flang-commits mailing list