[flang-commits] [flang] [Flang][OpenMP] Prevent ICE for certain constructs in unnamed programs (PR #73938)

Sergio Afonso via flang-commits flang-commits at lists.llvm.org
Wed Dec 6 03:43:13 PST 2023


================
@@ -25,7 +25,7 @@
 template <typename T>
 static Fortran::semantics::Scope *GetScope(
     Fortran::semantics::SemanticsContext &context, const T &x) {
-  std::optional<Fortran::parser::CharBlock> source{GetSource(x)};
+  std::optional<Fortran::parser::CharBlock> source{GetLastSource(x)};
----------------
skatrak wrote:

This function is only called for `MainProgram`, `FunctionSubprogram`, `SubroutineSubprogram` and `BlockData` (see `ResolveOmpTopLevelParts` in the same file). It tries to get the scope associated to these nodes and crashes while doing so in certain situations when it is a nameless `MainProgram`. As far as I can tell, the issue is that, when the optional `ProgramStmt` is not present (nameless program) the call to `GetSource` will return the source object of the first sourced PFT node that appears in the program, which may not have a scope associated.

In my opinion, it is incorrect to return the scope associated to any inner constructs when querying the program itself. This change makes it so the END statement of the program/function/subroutine/block, which is always present, is picked up every time, preventing this problem and stopping the crash from happening.

Maybe it'd be good to possibly restrict the `GetScope` function so it fails to compile if we pass any unexpected PFT node type, or just add a comment to describe its intended use. I'll gladly do it if that helps, or if there are any other suggestions as to how to deal with this case.

https://github.com/llvm/llvm-project/pull/73938


More information about the flang-commits mailing list