Skip to content

Commit 6f4d460

Browse files
sameeran joshiSameeranjoshi
authored andcommitted
[Flang][openmp][openacc] Extend CheckNoBranching to handle branching provided by LabelEnforce.
`CheckNoBranching` is currently handling only illegal branching out for constructs with `Parser::Name` in them. Extend the same for handling illegal branching out caused by `Parser::Label` based statements. This patch could possibly solve one of the issues(typically branching out) mentioned in D92735. Reviewed By: kiranchandramohan Differential Revision: https://reviews.llvm.org/D93447
1 parent 0529946 commit 6f4d460

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

flang/lib/Semantics/check-directive-structure.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "flang/Common/enum-set.h"
1616
#include "flang/Semantics/semantics.h"
1717
#include "flang/Semantics/tools.h"
18-
1918
#include <unordered_map>
2019

2120
namespace Fortran::semantics {
@@ -43,6 +42,9 @@ template <typename D> class NoBranchingEnforce {
4342

4443
template <typename T> bool Pre(const parser::Statement<T> &statement) {
4544
currentStatementSourcePosition_ = statement.source;
45+
if (statement.label.has_value()) {
46+
labels_.insert(*statement.label);
47+
}
4648
return true;
4749
}
4850

@@ -54,6 +56,8 @@ template <typename D> class NoBranchingEnforce {
5456
}
5557
void Post(const parser::StopStmt &) { EmitBranchOutError("STOP"); }
5658

59+
std::set<parser::Label> labels() { return labels_; }
60+
5761
private:
5862
parser::MessageFormattedText GetEnclosingMsg() const {
5963
return {"Enclosing %s construct"_en_US, upperCaseDirName_};
@@ -103,6 +107,7 @@ template <typename D> class NoBranchingEnforce {
103107
parser::CharBlock sourcePosition_;
104108
std::string upperCaseDirName_;
105109
D currentDirective_;
110+
std::set<parser::Label> labels_;
106111
};
107112

108113
// Generic structure checker for directives/clauses language such as OpenMP
@@ -226,6 +231,9 @@ class DirectiveStructureChecker : public virtual BaseChecker {
226231
SayNotMatching(beginDir.source, endDir.source);
227232
}
228233
}
234+
// Check illegal branching out of `Parser::Block` for `Parser::Name` based
235+
// nodes (examples `Parser::ExitStmt`) along with `Parser::Label`
236+
// based nodes (example `Parser::GotoStmt`).
229237
void CheckNoBranching(const parser::Block &block, D directive,
230238
const parser::CharBlock &directiveSource);
231239

@@ -271,6 +279,11 @@ void DirectiveStructureChecker<D, C, PC, ClauseEnumSize>::CheckNoBranching(
271279
NoBranchingEnforce<D> noBranchingEnforce{
272280
context_, directiveSource, directive, ContextDirectiveAsFortran()};
273281
parser::Walk(block, noBranchingEnforce);
282+
283+
LabelEnforce directiveLabelEnforce{context_, noBranchingEnforce.labels(),
284+
directiveSource,
285+
parser::ToUpperCaseLetters(getDirectiveName(directive).str()).c_str()};
286+
parser::Walk(block, directiveLabelEnforce);
274287
}
275288

276289
// Check that only clauses included in the given set are present after the given

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,7 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
125125
CheckMatching<parser::OmpBlockDirective>(beginDir, endDir);
126126

127127
PushContextAndClauseSets(beginDir.source, beginDir.v);
128-
129-
switch (beginDir.v) {
130-
case llvm::omp::OMPD_parallel:
131-
CheckNoBranching(block, llvm::omp::OMPD_parallel, beginDir.source);
132-
break;
133-
default:
134-
break;
135-
}
128+
CheckNoBranching(block, beginDir.v, beginDir.source);
136129
}
137130

138131
void OmpStructureChecker::Leave(const parser::OpenMPBlockConstruct &) {

flang/test/Semantics/omp-parallell01.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
! RUN: %S/test_errors.sh %s %t %f18 -fopenmp
2-
! XFAIL: *
32

43
! OpenMP Version 4.5
54
! 2.5 parallel construct.
@@ -13,7 +12,7 @@ program omp_parallel
1312
do i = 1, 10
1413
do j = 1, 10
1514
print *, "Hello"
16-
!ERROR: invalid branch to/from OpenMP structured block
15+
!ERROR: Control flow escapes from PARALLEL
1716
goto 10
1817
end do
1918
end do

0 commit comments

Comments
 (0)