Skip to content

Commit eebe9a3

Browse files
authored
[flang][CUDA] Fix crash in name resolution for CUDA function (llvm#108616)
When a function result type appears on a FUNCTION statement after some CUDA attributes, there wasn't always valid program source location information attached to the function result variable information stack. Ensure that some relevant source information is always available.
1 parent 34a4eef commit eebe9a3

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,15 @@ class FuncResultStack {
474474
~FuncResultStack();
475475

476476
struct FuncInfo {
477-
explicit FuncInfo(const Scope &s) : scope{s} {}
477+
FuncInfo(const Scope &s, SourceName at) : scope{s}, source{at} {}
478478
const Scope &scope;
479+
SourceName source;
479480
// Parse tree of the type specification in the FUNCTION prefix
480481
const parser::DeclarationTypeSpec *parsedType{nullptr};
481482
// Name of the function RESULT in the FUNCTION suffix, if any
482483
const parser::Name *resultName{nullptr};
483484
// Result symbol
484485
Symbol *resultSymbol{nullptr};
485-
std::optional<SourceName> source;
486486
bool inFunctionStmt{false}; // true between Pre/Post of FunctionStmt
487487
};
488488

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

494494
FuncInfo *Top() { return stack_.empty() ? nullptr : &stack_.back(); }
495-
FuncInfo &Push(const Scope &scope) { return stack_.emplace_back(scope); }
495+
FuncInfo &Push(const Scope &scope, SourceName at) {
496+
return stack_.emplace_back(scope, at);
497+
}
496498
void Pop();
497499

498500
private:
@@ -3799,11 +3801,13 @@ bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) {
37993801
if (const auto *parsedType{std::get_if<parser::DeclarationTypeSpec>(&x.u)}) {
38003802
if (FuncResultStack::FuncInfo * info{funcResultStack().Top()}) {
38013803
if (info->parsedType) { // C1543
3802-
Say(currStmtSource().value(),
3804+
Say(currStmtSource().value_or(info->source),
38033805
"FUNCTION prefix cannot specify the type more than once"_err_en_US);
38043806
} else {
38053807
info->parsedType = parsedType;
3806-
info->source = currStmtSource();
3808+
if (auto at{currStmtSource()}) {
3809+
info->source = *at;
3810+
}
38073811
}
38083812
} else {
38093813
Say(currStmtSource().value(),
@@ -3978,6 +3982,9 @@ bool SubprogramVisitor::Pre(const parser::FunctionStmt &) {
39783982
FuncResultStack::FuncInfo &info{DEREF(funcResultStack().Top())};
39793983
CHECK(!info.inFunctionStmt);
39803984
info.inFunctionStmt = true;
3985+
if (auto at{currStmtSource()}) {
3986+
info.source = *at;
3987+
}
39813988
return BeginAttrs();
39823989
}
39833990
bool SubprogramVisitor::Pre(const parser::EntryStmt &) { return BeginAttrs(); }
@@ -4507,7 +4514,7 @@ Symbol &SubprogramVisitor::PushSubprogramScope(const parser::Name &name,
45074514
symbol->set(subpFlag);
45084515
PushScope(Scope::Kind::Subprogram, symbol);
45094516
if (subpFlag == Symbol::Flag::Function) {
4510-
funcResultStack().Push(currScope());
4517+
funcResultStack().Push(currScope(), name.source);
45114518
}
45124519
if (inInterfaceBlock()) {
45134520
auto &details{symbol->get<SubprogramDetails>()};

0 commit comments

Comments
 (0)