Skip to content

Commit cf637b7

Browse files
authored
[flang][OpenMP] Fix goto within SECTION (#144502)
Previously we didn't push any context for SECTION and they are not modelled with differing scopes and so goto detection couldn't tell that GOTOs between two SECTIONs were between constructs rather than just staying inside of the parent SECTIONS construct. Fixes #143231
1 parent cd4e384 commit cf637b7

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
384384
bool Pre(const parser::OpenMPSectionsConstruct &);
385385
void Post(const parser::OpenMPSectionsConstruct &) { PopContext(); }
386386

387+
bool Pre(const parser::OpenMPSectionConstruct &);
388+
void Post(const parser::OpenMPSectionConstruct &) { PopContext(); }
389+
387390
bool Pre(const parser::OpenMPCriticalConstruct &critical);
388391
void Post(const parser::OpenMPCriticalConstruct &) { PopContext(); }
389392

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

2009+
bool OmpAttributeVisitor::Pre(const parser::OpenMPSectionConstruct &x) {
2010+
PushContext(x.source, llvm::omp::Directive::OMPD_section);
2011+
GetContext().withinConstruct = true;
2012+
return true;
2013+
}
2014+
20062015
bool OmpAttributeVisitor::Pre(const parser::OpenMPCriticalConstruct &x) {
20072016
const auto &beginCriticalDir{std::get<parser::OmpCriticalDirective>(x.t)};
20082017
const auto &endCriticalDir{std::get<parser::OmpEndCriticalDirective>(x.t)};
@@ -3024,8 +3033,13 @@ void OmpAttributeVisitor::CheckLabelContext(const parser::CharBlock source,
30243033
const parser::CharBlock target, std::optional<DirContext> sourceContext,
30253034
std::optional<DirContext> targetContext) {
30263035
auto dirContextsSame = [](DirContext &lhs, DirContext &rhs) -> bool {
3027-
// Sometimes nested constructs share a scope but are different contexts
3028-
return (lhs.scope == rhs.scope) && (lhs.directive == rhs.directive);
3036+
// Sometimes nested constructs share a scope but are different contexts.
3037+
// The directiveSource comparison is for OmpSection. Sections do not have
3038+
// their own scopes and two different sections both have the same directive.
3039+
// Their source however is different. This string comparison is unfortunate
3040+
// but should only happen for GOTOs inside of SECTION.
3041+
return (lhs.scope == rhs.scope) && (lhs.directive == rhs.directive) &&
3042+
(lhs.directiveSource == rhs.directiveSource);
30293043
};
30303044
unsigned version{context_.langOptions().OpenMPVersion};
30313045
if (targetContext &&

flang/test/Semantics/OpenMP/parallel-sections01.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ program OmpConstructSections01
3535
!$omp section
3636
print *, "This is a single statement structured block"
3737
!$omp section
38+
!ERROR: invalid branch into an OpenMP structured block
39+
!ERROR: invalid branch leaving an OpenMP structured block
3840
open (10, file="random-file-name.txt", err=30)
3941
!ERROR: invalid branch into an OpenMP structured block
4042
!ERROR: invalid branch leaving an OpenMP structured block
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
2+
! Regression test for #143231
3+
4+
!$omp sections
5+
! ERROR: invalid branch into an OpenMP structured block
6+
! ERROR: invalid branch leaving an OpenMP structured block
7+
goto 10
8+
!$omp section
9+
10 print *, "Invalid jump"
10+
!$omp end sections
11+
end

flang/test/Semantics/OpenMP/sections02.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ program OmpConstructSections01
1919
!$omp section
2020
print *, "This is a single statement structured block"
2121
!$omp section
22+
!ERROR: invalid branch into an OpenMP structured block
23+
!ERROR: invalid branch leaving an OpenMP structured block
2224
open (10, file="random-file-name.txt", err=30)
2325
!ERROR: invalid branch into an OpenMP structured block
2426
!ERROR: invalid branch leaving an OpenMP structured block

0 commit comments

Comments
 (0)