llvm-project/flang/lib/Evaluate
Peter Steinfeld 95540f9dbf [flang] Detect circularly defined interfaces of procedures
It's possible to define a procedure whose interface depends on a procedure
which has an interface that depends on the original procedure.  Such a circular
definition was causing the compiler to fall into an infinite loop when
resolving the name of the second procedure.  It's also possible to create
circular dependency chains of more than two procedures.

I fixed this by adding the function HasCycle() to the class DeclarationVisitor
and calling it from DeclareProcEntity() to detect procedures with such
circularly defined interfaces.  I marked the associated symbols of such
procedures by calling SetError() on them.  When processing subsequent
procedures, I called HasError() before attempting to analyze their interfaces.
Unfortunately, this did not work.

With help from Tim, we determined that the SymbolSet used to track the
erroneous symbols was instantiated using a "<" operator which was defined using
the location of the name of the procedure.  But the location of the procedure
name was being changed by a call to ReplaceName() between the times that the
calls to SetError() and HasError() were made.  This caused HasError() to
incorrectly report that a symbol was not in the set of erroneous symbols.

I fixed this by changing SymbolSet to be an unordered set that uses the
contents of the name of the symbol as the basis for its hash function.  This
works because the contents of the name of the symbol is preserved by
ReplaceName() even though its location changes.

I also fixed the error message used when reporting recursively defined
dummy procedure arguments by removing extra apostrophes and sorting the
list of symbols.

I also added tests that will crash the compiler without this change.

Note that the "<" operator is used in other contexts, for example, in the map
of characterized procedures, maps of items in equivalence sets, maps of
structure constructor values, ...  All of these situations happen after name
resolution has been completed and all calls to ReplaceName() have already
happened and thus are not subject to the problem I ran into when ReplaceName()
was called when processing procedure entities.

Note also that the implementation of the "<" operator uses the relative
location in the cooked character stream as the basis of its implementation.
This is potentially problematic when symbols from diffent compilation units
(for example symbols originating in .mod files) are put into the same map since
their names will appear in two different source streams which may not be
allocated in the same relative positions in memory.  But I was unable to create
a test that caused a problem.  Using a direct comparison of the content of the
name of the symbol in the "<" operator has problems.  Symbols in enclosing or
parallel scopes can have the same name.  Also using the location of the symbol
in the cooked character stream has the advantage that it preserves the the
order of the symbols in a structure constructor constant, which makes matching
the values with the symbols relatively easy.

This patch supersedes D97749.

Differential Revision: https://reviews.llvm.org/D97774
2021-03-02 10:53:44 -08:00
..
CMakeLists.txt [flang] Fix build with BUILD_SHARED_LIBS=ON and FLANG_BUILD_NEW_DRIVER=ON 2020-10-15 13:03:55 +02:00
call.cpp [flang] Fix bogus error message with binding 2021-01-21 16:59:51 -08:00
character.h [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
characteristics.cpp [flang] Detect circularly defined interfaces of procedures 2021-03-02 10:53:44 -08:00
check-expression.cpp [flang] Fix problems with constant arrays with lower bounds that are not 1 2021-01-29 08:05:10 -08:00
common.cpp [flang] Add isnan() extension intrinsic function, with folding 2020-11-11 11:40:57 -08:00
complex.cpp [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
constant.cpp [flang] Fixes for RESHAPE() 2020-07-30 19:05:06 -07:00
expression.cpp [flang] Fix assert on constant folding of extended types 2020-09-10 14:34:03 -07:00
fold-character.cpp [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
fold-complex.cpp [flang] Rework host runtime folding and enable REAL(2) folding with it. 2020-10-14 16:40:44 +02:00
fold-designator.cpp [flang] Implement STORAGE_SIZE(), SIZEOF(), C_SIZEOF() 2020-12-15 17:26:20 -08:00
fold-implementation.h [flang] Remove some needless operations in expr rewriting 2021-01-22 10:08:51 -08:00
fold-integer.cpp [flang] Fix calls to LBOUND() intrinsic for arrays with lower bounds not 1 2021-02-03 10:53:32 -08:00
fold-logical.cpp [flang] Implement IEEE_SUPPORT_xxx inquiry functions 2021-02-02 11:38:19 -08:00
fold-real.cpp [flang] Rework host runtime folding and enable REAL(2) folding with it. 2020-10-14 16:40:44 +02:00
fold.cpp [flang] Fix classification of shape inquiries in specification exprs 2021-01-13 10:05:14 -08:00
formatting.cpp [flang] Fix bogus message on internal subprogram with alternate return 2021-01-08 10:14:21 -08:00
host.cpp [flang] Fix setting mxcsr on MSVC 2020-07-08 07:25:25 -05:00
host.h [flang] Rework host runtime folding and enable REAL(2) folding with it. 2020-10-14 16:40:44 +02:00
initial-image.cpp [flang] Implement STORAGE_SIZE(), SIZEOF(), C_SIZEOF() 2020-12-15 17:26:20 -08:00
int-power.h [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
integer.cpp [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
intrinsics-library.cpp [flang] Rework host runtime folding and enable REAL(2) folding with it. 2020-10-14 16:40:44 +02:00
intrinsics.cpp [flang] Implement IEEE_SUPPORT_xxx inquiry functions 2021-02-02 11:38:19 -08:00
logical.cpp [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
real.cpp [flang][msvc] Avoid a reinterpret_cast 2020-10-15 10:38:48 -07:00
shape.cpp [flang] Add TypeAndShape::MeasureElementSizeInBytes() 2021-02-02 15:15:20 -08:00
static-data.cpp [flang] Reformat with latest clang-format and .clang-format 2020-03-28 21:00:16 -07:00
tools.cpp [flang] Infrastructure improvements in utility routines 2021-01-20 12:40:25 -08:00
type.cpp [flang] Implement STORAGE_SIZE(), SIZEOF(), C_SIZEOF() 2020-12-15 17:26:20 -08:00
variable.cpp [flang] Improve shape & length characterization 2021-01-30 16:14:27 -08:00