[flang-commits] [PATCH] D103567: [flang] Fix crashes due to failure to find a subprogram

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Jun 2 16:57:53 PDT 2021


klausler created this revision.
klausler added reviewers: PeteSteinfeld, awarzynski.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.

In error recovery situations, the mappings from source locations
to scopes were failing in a way that tripped some asserts.
Specifically, FindPureProcedureContaining() wasn't coping well
when starting at the global scope.  (And since the global scope
no longer has a source range, clean up the Semantics constructor
to avoid confusion.)


https://reviews.llvm.org/D103567

Files:
  flang/include/flang/Semantics/semantics.h
  flang/lib/Frontend/FrontendActions.cpp
  flang/lib/Semantics/check-io.cpp
  flang/lib/Semantics/scope.cpp
  flang/lib/Semantics/tools.cpp
  flang/tools/f18/f18.cpp


Index: flang/tools/f18/f18.cpp
===================================================================
--- flang/tools/f18/f18.cpp
+++ flang/tools/f18/f18.cpp
@@ -253,8 +253,8 @@
   if (!driver.debugNoSemantics || driver.dumpSymbols ||
       driver.dumpUnparseWithSymbols || driver.getDefinition ||
       driver.getSymbolsSources) {
-    Fortran::semantics::Semantics semantics{semanticsContext, parseTree,
-        parsing.cooked().AsCharBlock(), driver.debugModuleWriter};
+    Fortran::semantics::Semantics semantics{
+        semanticsContext, parseTree, driver.debugModuleWriter};
     semantics.Perform();
     Fortran::semantics::RuntimeDerivedTypeTables tables;
     if (!semantics.AnyFatalError()) {
Index: flang/lib/Semantics/tools.cpp
===================================================================
--- flang/lib/Semantics/tools.cpp
+++ flang/lib/Semantics/tools.cpp
@@ -80,8 +80,12 @@
   // N.B. We only need to examine the innermost containing program unit
   // because an internal subprogram of a pure subprogram must also
   // be pure (C1592).
-  const Scope &scope{GetProgramUnitContaining(start)};
-  return IsPureProcedure(scope) ? &scope : nullptr;
+  if (start.IsGlobal()) {
+    return nullptr;
+  } else {
+    const Scope &scope{GetProgramUnitContaining(start)};
+    return IsPureProcedure(scope) ? &scope : nullptr;
+  }
 }
 
 static bool MightHaveCompatibleDerivedtypes(
Index: flang/lib/Semantics/scope.cpp
===================================================================
--- flang/lib/Semantics/scope.cpp
+++ flang/lib/Semantics/scope.cpp
@@ -318,7 +318,7 @@
 }
 
 void Scope::AddSourceRange(const parser::CharBlock &source) {
-  for (auto *scope = this; !scope->IsGlobal(); scope = &scope->parent()) {
+  for (auto *scope{this}; !scope->IsGlobal(); scope = &scope->parent()) {
     scope->sourceRange_.ExtendToCover(source);
   }
 }
Index: flang/lib/Semantics/check-io.cpp
===================================================================
--- flang/lib/Semantics/check-io.cpp
+++ flang/lib/Semantics/check-io.cpp
@@ -953,8 +953,12 @@
 
 void IoChecker::CheckForPureSubprogram() const { // C1597
   CHECK(context_.location());
-  if (FindPureProcedureContaining(context_.FindScope(*context_.location()))) {
-    context_.Say("External I/O is not allowed in a pure subprogram"_err_en_US);
+  if (const Scope *
+      scope{context_.globalScope().FindScope(*context_.location())}) {
+    if (FindPureProcedureContaining(*scope)) {
+      context_.Say(
+          "External I/O is not allowed in a pure subprogram"_err_en_US);
+    }
   }
 }
 
Index: flang/lib/Frontend/FrontendActions.cpp
===================================================================
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -158,7 +158,7 @@
   // Prepare semantics
   setSemantics(std::make_unique<Fortran::semantics::Semantics>(
       ci.invocation().semanticsContext(), parseTree,
-      ci.parsing().cooked().AsCharBlock(), ci.invocation().debugModuleDir()));
+      ci.invocation().debugModuleDir()));
   auto &semantics = this->semantics();
 
   // Run semantic checks
Index: flang/include/flang/Semantics/semantics.h
===================================================================
--- flang/include/flang/Semantics/semantics.h
+++ flang/include/flang/Semantics/semantics.h
@@ -207,10 +207,9 @@
 class Semantics {
 public:
   explicit Semantics(SemanticsContext &context, parser::Program &program,
-      parser::CharBlock charBlock, bool debugModuleWriter = false)
+      bool debugModuleWriter = false)
       : context_{context}, program_{program} {
     context.set_debugModuleWriter(debugModuleWriter);
-    context.globalScope().AddSourceRange(charBlock);
   }
 
   SemanticsContext &context() const { return context_; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103567.349415.patch
Type: text/x-patch
Size: 3794 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210602/16c52baf/attachment.bin>


More information about the flang-commits mailing list