Skip to content

[flang][CUDA] Fix crash in name resolution for CUDA function #108616

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
Sep 16, 2024
Merged
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
19 changes: 13 additions & 6 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,15 @@ class FuncResultStack {
~FuncResultStack();

struct FuncInfo {
explicit FuncInfo(const Scope &s) : scope{s} {}
FuncInfo(const Scope &s, SourceName at) : scope{s}, source{at} {}
const Scope &scope;
SourceName source;
// Parse tree of the type specification in the FUNCTION prefix
const parser::DeclarationTypeSpec *parsedType{nullptr};
// Name of the function RESULT in the FUNCTION suffix, if any
const parser::Name *resultName{nullptr};
// Result symbol
Symbol *resultSymbol{nullptr};
std::optional<SourceName> source;
bool inFunctionStmt{false}; // true between Pre/Post of FunctionStmt
};

Expand All @@ -492,7 +492,9 @@ class FuncResultStack {
void CompleteTypeIfFunctionResult(Symbol &);

FuncInfo *Top() { return stack_.empty() ? nullptr : &stack_.back(); }
FuncInfo &Push(const Scope &scope) { return stack_.emplace_back(scope); }
FuncInfo &Push(const Scope &scope, SourceName at) {
return stack_.emplace_back(scope, at);
}
void Pop();

private:
Expand Down Expand Up @@ -3799,11 +3801,13 @@ bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) {
if (const auto *parsedType{std::get_if<parser::DeclarationTypeSpec>(&x.u)}) {
if (FuncResultStack::FuncInfo * info{funcResultStack().Top()}) {
if (info->parsedType) { // C1543
Say(currStmtSource().value(),
Say(currStmtSource().value_or(info->source),
"FUNCTION prefix cannot specify the type more than once"_err_en_US);
} else {
info->parsedType = parsedType;
info->source = currStmtSource();
if (auto at{currStmtSource()}) {
info->source = *at;
}
}
} else {
Say(currStmtSource().value(),
Expand Down Expand Up @@ -3978,6 +3982,9 @@ bool SubprogramVisitor::Pre(const parser::FunctionStmt &) {
FuncResultStack::FuncInfo &info{DEREF(funcResultStack().Top())};
CHECK(!info.inFunctionStmt);
info.inFunctionStmt = true;
if (auto at{currStmtSource()}) {
info.source = *at;
}
return BeginAttrs();
}
bool SubprogramVisitor::Pre(const parser::EntryStmt &) { return BeginAttrs(); }
Expand Down Expand Up @@ -4507,7 +4514,7 @@ Symbol &SubprogramVisitor::PushSubprogramScope(const parser::Name &name,
symbol->set(subpFlag);
PushScope(Scope::Kind::Subprogram, symbol);
if (subpFlag == Symbol::Flag::Function) {
funcResultStack().Push(currScope());
funcResultStack().Push(currScope(), name.source);
}
if (inInterfaceBlock()) {
auto &details{symbol->get<SubprogramDetails>()};
Expand Down
Loading