Skip to content

[flang] Fix references to destroyed objects #111582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef FORTRAN_SEMANTICS_PROGRAM_TREE_H_
#define FORTRAN_SEMANTICS_PROGRAM_TREE_H_

#include "symbol.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Semantics/symbol.h"
#include <list>
#include <variant>

Expand All @@ -35,7 +35,7 @@ class ProgramTree {
std::list<common::Reference<const parser::GenericSpec>>;

// Build the ProgramTree rooted at one of these program units.
static ProgramTree Build(const parser::ProgramUnit &, SemanticsContext &);
static ProgramTree &Build(const parser::ProgramUnit &, SemanticsContext &);
static std::optional<ProgramTree> Build(
const parser::MainProgram &, SemanticsContext &);
static std::optional<ProgramTree> Build(
Expand Down
7 changes: 6 additions & 1 deletion flang/include/flang/Semantics/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef FORTRAN_SEMANTICS_SEMANTICS_H_
#define FORTRAN_SEMANTICS_SEMANTICS_H_

#include "module-dependences.h"
#include "program-tree.h"
#include "scope.h"
#include "symbol.h"
#include "flang/Common/Fortran-features.h"
Expand All @@ -17,7 +19,6 @@
#include "flang/Evaluate/intrinsics.h"
#include "flang/Evaluate/target.h"
#include "flang/Parser/message.h"
#include "flang/Semantics/module-dependences.h"
#include <iosfwd>
#include <set>
#include <string>
Expand Down Expand Up @@ -280,6 +281,9 @@ class SemanticsContext {

void DumpSymbols(llvm::raw_ostream &);

// Top-level ProgramTrees are owned by the SemanticsContext for persistence.
ProgramTree &SaveProgramTree(ProgramTree &&);

private:
struct ScopeIndexComparator {
bool operator()(parser::CharBlock, parser::CharBlock) const;
Expand Down Expand Up @@ -331,6 +335,7 @@ class SemanticsContext {
ModuleDependences moduleDependences_;
std::map<const Symbol *, SourceName> moduleFileOutputRenamings_;
UnorderedSymbolSet isDefined_;
std::list<ProgramTree> programTrees_;
};

class Semantics {
Expand Down
8 changes: 4 additions & 4 deletions flang/lib/Semantics/program-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "program-tree.h"
#include "flang/Semantics/program-tree.h"
#include "flang/Common/idioms.h"
#include "flang/Parser/char-block.h"
#include "flang/Semantics/scope.h"
Expand Down Expand Up @@ -130,13 +130,13 @@ static ProgramTree BuildModuleTree(
return node;
}

ProgramTree ProgramTree::Build(
ProgramTree &ProgramTree::Build(
const parser::ProgramUnit &x, SemanticsContext &context) {
return common::visit(
[&](const auto &y) {
[&](const auto &y) -> ProgramTree & {
auto node{Build(y.value(), context)};
CHECK(node.has_value());
return std::move(*node);
return context.SaveProgramTree(std::move(*node));
},
x.u);
}
Expand Down
5 changes: 3 additions & 2 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "definable.h"
#include "mod-file.h"
#include "pointer-assignment.h"
#include "program-tree.h"
#include "resolve-directives.h"
#include "resolve-names-utils.h"
#include "rewrite-parse-tree.h"
Expand All @@ -32,6 +31,7 @@
#include "flang/Parser/tools.h"
#include "flang/Semantics/attr.h"
#include "flang/Semantics/expression.h"
#include "flang/Semantics/program-tree.h"
#include "flang/Semantics/scope.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/symbol.h"
Expand Down Expand Up @@ -2490,6 +2490,7 @@ Symbol &ScopeHandler::CopySymbol(const SourceName &name, const Symbol &symbol) {
}

// Look for name only in scope, not in enclosing scopes.

Symbol *ScopeHandler::FindInScope(
const Scope &scope, const parser::Name &name) {
return Resolve(name, FindInScope(scope, name.source));
Expand Down Expand Up @@ -9120,7 +9121,7 @@ bool ResolveNamesVisitor::Pre(const parser::ProgramUnit &x) {
ResolveAccParts(context(), x, &topScope_);
return false;
}
auto root{ProgramTree::Build(x, context())};
ProgramTree &root{ProgramTree::Build(x, context())};
SetScope(topScope_);
ResolveSpecificationParts(root);
FinishSpecificationParts(root);
Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Semantics/semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ void SemanticsContext::DumpSymbols(llvm::raw_ostream &os) {
DoDumpSymbols(os, globalScope());
}

ProgramTree &SemanticsContext::SaveProgramTree(ProgramTree &&tree) {
return programTrees_.emplace_back(std::move(tree));
}

void Semantics::DumpSymbols(llvm::raw_ostream &os) { context_.DumpSymbols(os); }

void Semantics::DumpSymbolsSources(llvm::raw_ostream &os) const {
Expand Down
Loading