Skip to content

[flang][NFC] Clean up code in two new functions #142037

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
Jun 10, 2025
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
3 changes: 3 additions & 0 deletions flang/include/flang/Parser/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@ template <typename A> std::optional<CharBlock> GetLastSource(A &x) {
return GetSourceHelper<false>::GetSource(const_cast<const A &>(x));
}

// Checks whether the assignment statement has a single variable on the RHS.
bool CheckForSingleVariableOnRHS(const AssignmentStmt &);

} // namespace Fortran::parser
#endif // FORTRAN_PARSER_TOOLS_H_
26 changes: 2 additions & 24 deletions flang/include/flang/Semantics/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,29 +753,7 @@ std::string GetCommonBlockObjectName(const Symbol &, bool underscoring);
// Check for ambiguous USE associations
bool HadUseError(SemanticsContext &, SourceName at, const Symbol *);

/// Checks if the assignment statement has a single variable on the RHS.
inline bool checkForSingleVariableOnRHS(
const Fortran::parser::AssignmentStmt &assignmentStmt) {
const Fortran::parser::Expr &expr{
std::get<Fortran::parser::Expr>(assignmentStmt.t)};
const Fortran::common::Indirection<Fortran::parser::Designator> *designator =
std::get_if<Fortran::common::Indirection<Fortran::parser::Designator>>(
&expr.u);
return designator != nullptr;
}

/// Checks if the symbol on the LHS is present in the RHS expression.
inline bool checkForSymbolMatch(const Fortran::semantics::SomeExpr *lhs,
const Fortran::semantics::SomeExpr *rhs) {
auto lhsSyms{Fortran::evaluate::GetSymbolVector(*lhs)};
const Fortran::semantics::Symbol &lhsSymbol{*lhsSyms.front()};
for (const Fortran::semantics::Symbol &symbol :
Fortran::evaluate::GetSymbolVector(*rhs)) {
if (lhsSymbol == symbol) {
return true;
}
}
return false;
}
// Checks whether the symbol on the LHS is present in the RHS expression.
bool CheckForSymbolMatch(const SomeExpr *lhs, const SomeExpr *rhs);
} // namespace Fortran::semantics
#endif // FORTRAN_SEMANTICS_TOOLS_H_
4 changes: 2 additions & 2 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ void genAtomicCapture(Fortran::lower::AbstractConverter &converter,
firOpBuilder.createBlock(&(atomicCaptureOp->getRegion(0)));
mlir::Block &block = atomicCaptureOp->getRegion(0).back();
firOpBuilder.setInsertionPointToStart(&block);
if (Fortran::semantics::checkForSingleVariableOnRHS(stmt1)) {
if (Fortran::semantics::checkForSymbolMatch(
if (Fortran::parser::CheckForSingleVariableOnRHS(stmt1)) {
if (Fortran::semantics::CheckForSymbolMatch(
Fortran::semantics::GetExpr(stmt2Var),
Fortran::semantics::GetExpr(stmt2Expr))) {
// Atomic capture construct is of the form [capture-stmt, update-stmt]
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3199,8 +3199,8 @@ static void genAtomicCapture(lower::AbstractConverter &converter,
firOpBuilder.createBlock(&(atomicCaptureOp->getRegion(0)));
mlir::Block &block = atomicCaptureOp->getRegion(0).back();
firOpBuilder.setInsertionPointToStart(&block);
if (semantics::checkForSingleVariableOnRHS(stmt1)) {
if (semantics::checkForSymbolMatch(semantics::GetExpr(stmt2Var),
if (parser::CheckForSingleVariableOnRHS(stmt1)) {
if (semantics::CheckForSymbolMatch(semantics::GetExpr(stmt2Var),
semantics::GetExpr(stmt2Expr))) {
// Atomic capture construct is of the form [capture-stmt, update-stmt]
const semantics::SomeExpr &fromExpr = *semantics::GetExpr(stmt1Expr);
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Parser/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,9 @@ const CoindexedNamedObject *GetCoindexedNamedObject(
},
allocateObject.u);
}

bool CheckForSingleVariableOnRHS(const AssignmentStmt &assignmentStmt) {
return Unwrap<Designator>(std::get<Expr>(assignmentStmt.t)) != nullptr;
}

} // namespace Fortran::parser
8 changes: 4 additions & 4 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2922,9 +2922,9 @@ void OmpStructureChecker::CheckAtomicCaptureConstruct(
const auto *e2 = GetExpr(context_, stmt2Expr);

if (e1 && v1 && e2 && v2) {
if (semantics::checkForSingleVariableOnRHS(stmt1)) {
if (parser::CheckForSingleVariableOnRHS(stmt1)) {
CheckAtomicCaptureStmt(stmt1);
if (semantics::checkForSymbolMatch(v2, e2)) {
if (CheckForSymbolMatch(v2, e2)) {
// ATOMIC CAPTURE construct is of the form [capture-stmt, update-stmt]
CheckAtomicUpdateStmt(stmt2);
} else {
Expand All @@ -2936,8 +2936,8 @@ void OmpStructureChecker::CheckAtomicCaptureConstruct(
"Captured variable/array element/derived-type component %s expected to be assigned in the second statement of ATOMIC CAPTURE construct"_err_en_US,
stmt1Expr.source);
}
} else if (semantics::checkForSymbolMatch(v1, e1) &&
semantics::checkForSingleVariableOnRHS(stmt2)) {
} else if (CheckForSymbolMatch(v1, e1) &&
parser::CheckForSingleVariableOnRHS(stmt2)) {
// ATOMIC CAPTURE construct is of the form [update-stmt, capture-stmt]
CheckAtomicUpdateStmt(stmt1);
CheckAtomicCaptureStmt(stmt2);
Expand Down
14 changes: 14 additions & 0 deletions flang/lib/Semantics/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1756,4 +1756,18 @@ bool HadUseError(
}
}

bool CheckForSymbolMatch(const SomeExpr *lhs, const SomeExpr *rhs) {
if (lhs && rhs) {
if (SymbolVector lhsSymbols{evaluate::GetSymbolVector(*lhs)};
!lhsSymbols.empty()) {
const Symbol &first{*lhsSymbols.front()};
for (const Symbol &symbol : evaluate::GetSymbolVector(*rhs)) {
if (first == symbol) {
return true;
}
}
}
}
return false;
}
} // namespace Fortran::semantics