[flang-commits] [flang] [flang] Fix parsing time explosion (PR #76533)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jan 2 07:54:45 PST 2024


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/76533

>From fe39a04650a6982631726219e9f37fbbf34d4621 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Thu, 28 Dec 2023 12:58:56 -0800
Subject: [PATCH] [flang] Fix parsing time explosion

When parsing a deeply-nested expression like
  A1(A2(A3(A4(A5(A6(...A99(i)...))))))
the parser can get into an exponential state due to the need
to consider the possibility that each "An(...)" might be the
beginning of a reference to a procedure component ("An(...)%PROC(...)")
so that alternative has to be attempted first before proceeding
to try parsing "An(...)" as a function reference or as an array
element designator.  The parser for a structure component, which
is used by the procedure designator parser, was not protected with
the usual failure memoization technique, leading to exponentially
bad behavior parsing a deeply-nested expression.  Fix by exploiting
the instrumented() parser combinator so that failed structure component
parsers aren't repeated.

Fixes https://github.com/llvm/llvm-project/issues/76477.
---
 flang/lib/Parser/Fortran-parsers.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index c070bc1de37352f..0dd95d69d3c6628 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -1151,8 +1151,9 @@ TYPE_PARSER(construct<PartRef>(name,
 
 // R913 structure-component -> data-ref
 // The final part-ref in the data-ref is not allowed to have subscripts.
-TYPE_PARSER(construct<StructureComponent>(
-    construct<DataRef>(some(Parser<PartRef>{} / percentOrDot)), name))
+TYPE_CONTEXT_PARSER("component"_en_US,
+    construct<StructureComponent>(
+        construct<DataRef>(some(Parser<PartRef>{} / percentOrDot)), name))
 
 // R919 subscript -> scalar-int-expr
 constexpr auto subscript{scalarIntExpr};



More information about the flang-commits mailing list