[flang-commits] [flang] 1dff863 - [flang] Fix crashes due to failure to find a subprogram

peter klausler via flang-commits flang-commits at lists.llvm.org
Thu Jun 3 12:46:38 PDT 2021


Author: peter klausler
Date: 2021-06-03T12:45:43-07:00
New Revision: 1dff8637b11232a7e6e7994c2ed3526262564786

URL: https://github.com/llvm/llvm-project/commit/1dff8637b11232a7e6e7994c2ed3526262564786
DIFF: https://github.com/llvm/llvm-project/commit/1dff8637b11232a7e6e7994c2ed3526262564786.diff

LOG: [flang] Fix crashes due to failure to find a subprogram

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.)

Differential Revision: https://reviews.llvm.org/D103567

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Semantics/semantics.h b/flang/include/flang/Semantics/semantics.h
index 3ef0cafa872aa..c49672ea03667 100644
--- a/flang/include/flang/Semantics/semantics.h
+++ b/flang/include/flang/Semantics/semantics.h
@@ -207,10 +207,9 @@ class SemanticsContext {
 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_; }

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 96aa91a2fa92f..de8a02753391b 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -158,7 +158,7 @@ bool PrescanAndSemaAction::BeginSourceFileAction(CompilerInstance &c1) {
   // 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

diff  --git a/flang/lib/Semantics/check-io.cpp b/flang/lib/Semantics/check-io.cpp
index 0f93deea0e9e9..9ce7f82936147 100644
--- a/flang/lib/Semantics/check-io.cpp
+++ b/flang/lib/Semantics/check-io.cpp
@@ -953,8 +953,12 @@ void IoChecker::CheckForDefinableVariable(
 
 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);
+    }
   }
 }
 

diff  --git a/flang/lib/Semantics/scope.cpp b/flang/lib/Semantics/scope.cpp
index c6f73e583d6de..548b55fd8f0df 100644
--- a/flang/lib/Semantics/scope.cpp
+++ b/flang/lib/Semantics/scope.cpp
@@ -318,7 +318,7 @@ Scope *Scope::FindScope(parser::CharBlock source) {
 }
 
 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);
   }
 }

diff  --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index a633ecbe1898d..6440175c502d1 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -80,8 +80,12 @@ const Scope *FindPureProcedureContaining(const Scope &start) {
   // 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(

diff  --git a/flang/tools/f18/f18.cpp b/flang/tools/f18/f18.cpp
index f77dd7e9cbf3d..9e414958ee783 100644
--- a/flang/tools/f18/f18.cpp
+++ b/flang/tools/f18/f18.cpp
@@ -253,8 +253,8 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
   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()) {


        


More information about the flang-commits mailing list