[flang-commits] [flang] 9fdde69 - [flang] Avoid crash in error recovery

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jun 27 13:24:06 PDT 2023


Author: Peter Klausler
Date: 2023-06-27T13:19:45-07:00
New Revision: 9fdde69f72f6145d220645d1b218b4c6f2be2c13

URL: https://github.com/llvm/llvm-project/commit/9fdde69f72f6145d220645d1b218b4c6f2be2c13
DIFF: https://github.com/llvm/llvm-project/commit/9fdde69f72f6145d220645d1b218b4c6f2be2c13.diff

LOG: [flang] Avoid crash in error recovery

When a PASS() clause names a nonexistent dummy argument, don't crash
later in expression semantics.
Fixes https://github.com/llvm/llvm-project/issues/63487.

Differential Revision: https://reviews.llvm.org/D153792

Added: 
    

Modified: 
    flang/lib/Semantics/expression.cpp
    flang/test/Semantics/resolve52.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index a6b54dd11d21b..f1087c7d42337 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -2116,7 +2116,7 @@ static std::optional<parser::CharBlock> GetPassName(
       proc.details());
 }
 
-static int GetPassIndex(const Symbol &proc) {
+static std::optional<int> GetPassIndex(const Symbol &proc) {
   CHECK(!proc.attrs().test(semantics::Attr::NOPASS));
   std::optional<parser::CharBlock> passName{GetPassName(proc)};
   const auto *interface {
@@ -2133,7 +2133,7 @@ static int GetPassIndex(const Symbol &proc) {
     }
     ++index;
   }
-  DIE("PASS argument name not in dummy argument list");
+  return std::nullopt;
 }
 
 // Injects an expression into an actual argument list as the "passed object"
@@ -2146,10 +2146,13 @@ static void AddPassArg(ActualArguments &actuals, const Expr<SomeDerived> &expr,
   if (component.attrs().test(semantics::Attr::NOPASS)) {
     return;
   }
-  int passIndex{GetPassIndex(component)};
+  std::optional<int> passIndex{GetPassIndex(component)};
+  if (!passIndex) {
+    return; // error recovery
+  }
   auto iter{actuals.begin()};
   int at{0};
-  while (iter < actuals.end() && at < passIndex) {
+  while (iter < actuals.end() && at < *passIndex) {
     if (*iter && (*iter)->keyword()) {
       iter = actuals.end();
       break;
@@ -4296,7 +4299,7 @@ const Symbol *ArgumentAnalyzer::FindBoundOp(parser::CharBlock oprName,
   if (generic) {
     ExpressionAnalyzer::AdjustActuals adjustment{
         [&](const Symbol &proc, ActualArguments &) {
-          return passIndex == GetPassIndex(proc);
+          return passIndex == GetPassIndex(proc).value_or(-1);
         }};
     auto pair{
         context_.ResolveGeneric(*generic, actuals_, adjustment, isSubroutine)};

diff  --git a/flang/test/Semantics/resolve52.f90 b/flang/test/Semantics/resolve52.f90
index c9ee0b8620899..9f89510652b2e 100644
--- a/flang/test/Semantics/resolve52.f90
+++ b/flang/test/Semantics/resolve52.f90
@@ -50,6 +50,11 @@ module m3
   subroutine s(x)
     class(t) :: x
   end
+  subroutine test
+    type(t) x
+    !ERROR: Dummy argument 'x=' (#1) is not OPTIONAL and is not associated with an actual argument in this procedure reference
+    call x%p
+  end
 end
 
 module m4


        


More information about the flang-commits mailing list