diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp --- a/flang/lib/Semantics/check-omp-structure.cpp +++ b/flang/lib/Semantics/check-omp-structure.cpp @@ -2634,24 +2634,28 @@ bool OmpStructureChecker::CheckTargetBlockOnlyTeams( const parser::Block &block) { bool nestedTeams{false}; - auto it{block.begin()}; - - if (const auto *ompConstruct{parser::Unwrap(*it)}) { - if (const auto *ompBlockConstruct{ - std::get_if(&ompConstruct->u)}) { - const auto &beginBlockDir{ - std::get(ompBlockConstruct->t)}; - const auto &beginDir{ - std::get(beginBlockDir.t)}; - if (beginDir.v == llvm::omp::Directive::OMPD_teams) { - nestedTeams = true; + + if (!block.empty()) { + auto it{block.begin()}; + if (const auto *ompConstruct{ + parser::Unwrap(*it)}) { + if (const auto *ompBlockConstruct{ + std::get_if(&ompConstruct->u)}) { + const auto &beginBlockDir{ + std::get(ompBlockConstruct->t)}; + const auto &beginDir{ + std::get(beginBlockDir.t)}; + if (beginDir.v == llvm::omp::Directive::OMPD_teams) { + nestedTeams = true; + } } } - } - if (nestedTeams && ++it == block.end()) { - return true; + if (nestedTeams && ++it == block.end()) { + return true; + } } + return false; } diff --git a/flang/test/Semantics/OpenMP/omp-target.f90 b/flang/test/Semantics/OpenMP/omp-target.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/omp-target.f90 @@ -0,0 +1,11 @@ +! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp -Werror + +! Corner cases in OpenMP target directives + +subroutine empty_target() + integer :: i + + !$omp target map(i) + !$omp end target + +end subroutine