Skip to content

[flang][OpenMP] Fix goto within SECTION #144502

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 17, 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
18 changes: 16 additions & 2 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
bool Pre(const parser::OpenMPSectionsConstruct &);
void Post(const parser::OpenMPSectionsConstruct &) { PopContext(); }

bool Pre(const parser::OpenMPSectionConstruct &);
void Post(const parser::OpenMPSectionConstruct &) { PopContext(); }

bool Pre(const parser::OpenMPCriticalConstruct &critical);
void Post(const parser::OpenMPCriticalConstruct &) { PopContext(); }

Expand Down Expand Up @@ -2003,6 +2006,12 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPSectionsConstruct &x) {
return true;
}

bool OmpAttributeVisitor::Pre(const parser::OpenMPSectionConstruct &x) {
PushContext(x.source, llvm::omp::Directive::OMPD_section);
GetContext().withinConstruct = true;
return true;
}

bool OmpAttributeVisitor::Pre(const parser::OpenMPCriticalConstruct &x) {
const auto &beginCriticalDir{std::get<parser::OmpCriticalDirective>(x.t)};
const auto &endCriticalDir{std::get<parser::OmpEndCriticalDirective>(x.t)};
Expand Down Expand Up @@ -3024,8 +3033,13 @@ void OmpAttributeVisitor::CheckLabelContext(const parser::CharBlock source,
const parser::CharBlock target, std::optional<DirContext> sourceContext,
std::optional<DirContext> targetContext) {
auto dirContextsSame = [](DirContext &lhs, DirContext &rhs) -> bool {
// Sometimes nested constructs share a scope but are different contexts
return (lhs.scope == rhs.scope) && (lhs.directive == rhs.directive);
// Sometimes nested constructs share a scope but are different contexts.
// The directiveSource comparison is for OmpSection. Sections do not have
// their own scopes and two different sections both have the same directive.
// Their source however is different. This string comparison is unfortunate
// but should only happen for GOTOs inside of SECTION.
return (lhs.scope == rhs.scope) && (lhs.directive == rhs.directive) &&
(lhs.directiveSource == rhs.directiveSource);
};
unsigned version{context_.langOptions().OpenMPVersion};
if (targetContext &&
Expand Down
2 changes: 2 additions & 0 deletions flang/test/Semantics/OpenMP/parallel-sections01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ program OmpConstructSections01
!$omp section
print *, "This is a single statement structured block"
!$omp section
!ERROR: invalid branch into an OpenMP structured block
!ERROR: invalid branch leaving an OpenMP structured block
open (10, file="random-file-name.txt", err=30)
!ERROR: invalid branch into an OpenMP structured block
!ERROR: invalid branch leaving an OpenMP structured block
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Semantics/OpenMP/sections-goto.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
! Regression test for #143231

!$omp sections
! ERROR: invalid branch into an OpenMP structured block
! ERROR: invalid branch leaving an OpenMP structured block
goto 10
!$omp section
10 print *, "Invalid jump"
!$omp end sections
end
2 changes: 2 additions & 0 deletions flang/test/Semantics/OpenMP/sections02.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ program OmpConstructSections01
!$omp section
print *, "This is a single statement structured block"
!$omp section
!ERROR: invalid branch into an OpenMP structured block
!ERROR: invalid branch leaving an OpenMP structured block
open (10, file="random-file-name.txt", err=30)
!ERROR: invalid branch into an OpenMP structured block
!ERROR: invalid branch leaving an OpenMP structured block
Expand Down
Loading