Skip to content

Commit 3bbdbb2

Browse files
authored
[flang] Fix parsing time explosion (llvm#76533)
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 llvm#76477.
1 parent 120ad25 commit 3bbdbb2

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

flang/lib/Parser/Fortran-parsers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,9 @@ TYPE_PARSER(construct<PartRef>(name,
11511151
11521152
// R913 structure-component -> data-ref
11531153
// The final part-ref in the data-ref is not allowed to have subscripts.
1154-
TYPE_PARSER(construct<StructureComponent>(
1155-
construct<DataRef>(some(Parser<PartRef>{} / percentOrDot)), name))
1154+
TYPE_CONTEXT_PARSER("component"_en_US,
1155+
construct<StructureComponent>(
1156+
construct<DataRef>(some(Parser<PartRef>{} / percentOrDot)), name))
11561157
11571158
// R919 subscript -> scalar-int-expr
11581159
constexpr auto subscript{scalarIntExpr};

0 commit comments

Comments
 (0)