diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -826,7 +826,7 @@ const ProgramTree::EntryStmtList * = nullptr); bool BeginMpSubprogram(const parser::Name &); void PushBlockDataScope(const parser::Name &); - void EndSubprogram( + void EndSubprogram(std::optional stmtSource = std::nullopt, const std::optional * = nullptr); protected: @@ -3237,8 +3237,9 @@ return BeginSubprogram(name, Symbol::Flag::Subroutine); } void SubprogramVisitor::Post(const parser::InterfaceBody::Subroutine &x) { - EndSubprogram(&std::get>( - std::get>(x.t).statement.t)); + const auto &stmt{std::get>(x.t)}; + EndSubprogram(stmt.source, + &std::get>(stmt.statement.t)); } bool SubprogramVisitor::Pre(const parser::InterfaceBody::Function &x) { const auto &name{std::get( @@ -3246,9 +3247,10 @@ return BeginSubprogram(name, Symbol::Flag::Function); } void SubprogramVisitor::Post(const parser::InterfaceBody::Function &x) { - const auto &maybeSuffix{std::get>( - std::get>(x.t).statement.t)}; - EndSubprogram(maybeSuffix ? &maybeSuffix->binding : nullptr); + const auto &stmt{std::get>(x.t)}; + const auto &maybeSuffix{ + std::get>(stmt.statement.t)}; + EndSubprogram(stmt.source, maybeSuffix ? &maybeSuffix->binding : nullptr); } bool SubprogramVisitor::Pre(const parser::SubroutineStmt &stmt) { @@ -3598,15 +3600,19 @@ } void SubprogramVisitor::EndSubprogram( + std::optional stmtSource, const std::optional *binding) { if (binding && *binding && currScope().symbol()) { // Finally process the BIND(C,NAME=name) now that symbols in the name // expression will resolve local names. auto flagRestorer{common::ScopedSet(inSpecificationPart_, false)}; + auto originalStmtSource{messageHandler().currStmtSource()}; + messageHandler().set_currStmtSource(stmtSource); BeginAttrs(); Walk(**binding); SetBindNameOn(*currScope().symbol()); currScope().symbol()->attrs() |= EndAttrs(); + messageHandler().set_currStmtSource(originalStmtSource); } PopScope(); } @@ -7438,27 +7444,31 @@ } void ResolveNamesVisitor::EndScopeForNode(const ProgramTree &node) { - using BindingPtr = const std::optional *; - EndSubprogram(common::visit( + std::optional stmtSource; + const std::optional *binding{nullptr}; + common::visit( common::visitors{ - [](const parser::Statement *stmt) { + [&](const parser::Statement *stmt) { if (stmt) { + stmtSource = stmt->source; if (const auto &maybeSuffix{ std::get>( stmt->statement.t)}) { - return &maybeSuffix->binding; + binding = &maybeSuffix->binding; } } - return BindingPtr{}; }, - [](const parser::Statement *stmt) { - return stmt ? &std::get>( - stmt->statement.t) - : BindingPtr{}; + [&](const parser::Statement *stmt) { + if (stmt) { + stmtSource = stmt->source; + binding = &std::get>( + stmt->statement.t); + } }, - [](const auto *) { return BindingPtr{}; }, + [](const auto *) {}, }, - node.stmt())); + node.stmt()); + EndSubprogram(stmtSource, binding); } // Some analyses and checks, such as the processing of initializers of