[flang-commits] [flang] [flang][OpenMP] Move branching verification to semantic checks (PR #193324)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Thu Apr 23 09:52:28 PDT 2026


================
@@ -428,6 +436,119 @@ void OmpStructureChecker::AnalyzeObjects(const parser::OmpObjectList &objects) {
   }
 }
 
+const parser::OpenMPConstruct *
+OmpStructureChecker::GetCurrentConstruct() const {
+  for (const LoopOrConstruct &c : llvm::reverse(constructStack_)) {
+    if (auto *omp{std::get_if<const parser::OpenMPConstruct *>(&c)}) {
+      return *omp;
+    }
+  }
+  return nullptr;
+}
+
+void OmpStructureChecker::CheckSourceLabel(const parser::Label &label) {
+  // Get the context to check if the statement causing a jump to the 'label' is
+  // in an enclosing OpenMP construct
+  const parser::OpenMPConstruct *thisConstruct{GetCurrentConstruct()};
+  sourceLabels_.emplace(
+      label, std::make_pair(currentStatementSource_, thisConstruct));
+  // Check if the statement with 'label' to which a jump is being introduced
+  // has already been encountered
+  auto it{targetLabels_.find(label)};
+  if (it != targetLabels_.end()) {
+    // Check if both the statement with 'label' and the statement that causes a
+    // jump to the 'label' are in the same block.
+    CheckLabelContext(currentStatementSource_, it->second.first, thisConstruct,
+        it->second.second);
+  }
+}
+
+// Check for invalid branch into or out of OpenMP structured blocks
+void OmpStructureChecker::CheckLabelContext(const parser::CharBlock source,
+    const parser::CharBlock target, const parser::OpenMPConstruct *srcOmp,
+    const parser::OpenMPConstruct *tgtOmp) {
+  auto isSameOrIncludes = [&](const parser::OpenMPConstruct *lhs,
+                              const parser::OpenMPConstruct *rhs) -> bool {
+    assert(lhs && "Expecting first argument");
+    if (!rhs) {
+      return false;
+    }
+    if (lhs == rhs) {
+      return true;
+    }
+
+    auto getSource{[](const parser::OpenMPConstruct &c) -> parser::CharBlock {
+      if (auto src{parser::GetSource(c)}) {
+        return *src;
+      }
+      return {};
+    }};
+
+    return getSource(*lhs).Contains(getSource(*rhs));
----------------
kparzysz wrote:

Actually, it should never return {}.  Every instance of `OpenMPConstruct` should have a valid source associated with it.  I have changed this code to assert on missing source to make that clear.

https://github.com/llvm/llvm-project/pull/193324


More information about the flang-commits mailing list