Skip to content

Commit 9d95242

Browse files
[Flang][OpenMP] Issue an error for loop directive without a loop (#118039)
Fixes #107500
1 parent 148fdc5 commit 9d95242

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,22 +1895,29 @@ void OmpAttributeVisitor::PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
18951895
}
18961896

18971897
const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
1898-
for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
1899-
// go through all the nested do-loops and resolve index variables
1900-
const parser::Name *iv{GetLoopIndex(*loop)};
1901-
if (iv) {
1902-
if (auto *symbol{ResolveOmp(*iv, ivDSA, currScope())}) {
1903-
symbol->set(Symbol::Flag::OmpPreDetermined);
1904-
iv->symbol = symbol; // adjust the symbol within region
1905-
AddToContextObjectWithDSA(*symbol, ivDSA);
1906-
}
1898+
if (outer.has_value()) {
1899+
for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
1900+
// go through all the nested do-loops and resolve index variables
1901+
const parser::Name *iv{GetLoopIndex(*loop)};
1902+
if (iv) {
1903+
if (auto *symbol{ResolveOmp(*iv, ivDSA, currScope())}) {
1904+
symbol->set(Symbol::Flag::OmpPreDetermined);
1905+
iv->symbol = symbol; // adjust the symbol within region
1906+
AddToContextObjectWithDSA(*symbol, ivDSA);
1907+
}
19071908

1908-
const auto &block{std::get<parser::Block>(loop->t)};
1909-
const auto it{block.begin()};
1910-
loop = it != block.end() ? GetDoConstructIf(*it) : nullptr;
1909+
const auto &block{std::get<parser::Block>(loop->t)};
1910+
const auto it{block.begin()};
1911+
loop = it != block.end() ? GetDoConstructIf(*it) : nullptr;
1912+
}
19111913
}
1914+
CheckAssocLoopLevel(level, GetAssociatedClause());
1915+
} else {
1916+
context_.Say(GetContext().directiveSource,
1917+
"A DO loop must follow the %s directive"_err_en_US,
1918+
parser::ToUpperCaseLetters(
1919+
llvm::omp::getOpenMPDirectiveName(GetContext().directive).str()));
19121920
}
1913-
CheckAssocLoopLevel(level, GetAssociatedClause());
19141921
}
19151922
void OmpAttributeVisitor::CheckAssocLoopLevel(
19161923
std::int64_t level, const parser::OmpClause *clause) {

flang/test/Semantics/OpenMP/do21.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
2+
! Check for existence of loop following a DO directive
3+
4+
subroutine do1
5+
!ERROR: A DO loop must follow the DO directive
6+
!$omp do
7+
end subroutine
8+
9+
subroutine do2
10+
!ERROR: A DO loop must follow the PARALLEL DO directive
11+
!$omp parallel do
12+
end subroutine
13+
14+
subroutine do3
15+
!ERROR: A DO loop must follow the SIMD directive
16+
!$omp simd
17+
end subroutine
18+
19+
subroutine do4
20+
!ERROR: A DO loop must follow the DO SIMD directive
21+
!$omp do simd
22+
end subroutine
23+
24+
subroutine do5
25+
!ERROR: A DO loop must follow the LOOP directive
26+
!$omp loop
27+
end subroutine

0 commit comments

Comments
 (0)