[clang] [Clang] [OpenMP] Use raw AST statement as a gallback when transformed statement isn't available for jump check (PR #234881)

Soham Karandikar via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 23 02:19:15 PDT 2026


https://github.com/skadewdl3 created https://github.com/llvm/llvm-project/pull/234881

Fixes #230923.

Taking a look at the segfault in a debugger and playing around with the reproducer, the error happened only when:
- There's a non-empty statement before the nested for-loop
- There's an interchange directive present

The segfault was happening in `JumpScopeChecker::BuildScopeInformation` when processing the statement as an `OMPExecutableDirective`. In case of an interchange directive being present, `getStructuredBlock` returned `nullptr` causing `getBeginLoc` to segfault.

This PR adds a fallback so if the transformed statement returned by `getStructuredBlock()` isn't available, the raw AST statement is used instead.

Assisted-by: GPT Sol 6.1
I used Sol to decide upon where the fix should be placed.

>From 1df7e90e7dcac1a6345f6a4b06328af97f65bb80 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Fri, 23 Oct 2026 03:53:33 -0500
Subject: [PATCH 1/3] Raw source statement is used as fallback if transformed
 structured block isn't available

---
 clang/lib/Sema/JumpDiagnostics.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/JumpDiagnostics.cpp b/clang/lib/Sema/JumpDiagnostics.cpp
index bbe6f644609bf8..6489c9ede77592 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -655,12 +655,19 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
   default:
     if (auto *ED = dyn_cast<OMPExecutableDirective>(S)) {
       if (!ED->isStandaloneDirective()) {
+        // Sometimes, the transformed structured block may not be available
+        // when transformation is deferred (e.g., when using `omp interchange`).
+        // In such cases, use the existing source statement
+        Stmt *Block = ED->getStructuredBlock();
+        if (!Block)
+          Block = ED->getRawStmt();
+
         unsigned NewParentScope = Scopes.size();
         Scopes.emplace_back(ParentScope,
                             diag::note_omp_protected_structured_block,
                             diag::note_omp_exits_structured_block,
-                            ED->getStructuredBlock()->getBeginLoc());
-        BuildScopeInformation(ED->getStructuredBlock(), NewParentScope);
+                            Block->getBeginLoc());
+        BuildScopeInformation(Block, NewParentScope);
         return;
       }
     }

>From 1177fcc3a337c6720776f67514800b0a1c7b3fb5 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Fri, 23 Oct 2026 04:01:45 -0500
Subject: [PATCH 2/3] Added test

---
 clang/test/OpenMP/interchange_messages.cpp | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/clang/test/OpenMP/interchange_messages.cpp b/clang/test/OpenMP/interchange_messages.cpp
index 7b9a4963445e4f..d714267722a9b4 100644
--- a/clang/test/OpenMP/interchange_messages.cpp
+++ b/clang/test/OpenMP/interchange_messages.cpp
@@ -220,6 +220,19 @@ static void templated_func_type_dependent() {
 }
 
 
+// Deferred interchange must not crash jump-scope checking
+template <typename T>
+static void interchange_in_switch() {
+  switch (T(0)) {
+    int a = 10;
+#pragma omp for collapse(2)
+#pragma omp interchange
+    for (int j = 0; j < 2; j++)
+      for (int k = 0; k < 2; k++)
+        ;
+  }
+}
+
 void template_inst() {
   // expected-note at +1 {{in instantiation of function template specialization 'templated_func<int>' requested here}}
   templated_func<int>();

>From 97d74a8cb73dd9e0f4e61abef7715c5187b341af Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Fri, 23 Oct 2026 04:14:27 -0500
Subject: [PATCH 3/3] Ran git clang-format

---
 clang/lib/Sema/JumpDiagnostics.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/JumpDiagnostics.cpp b/clang/lib/Sema/JumpDiagnostics.cpp
index 6489c9ede77592..96e65b4ca766c6 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -663,10 +663,9 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
           Block = ED->getRawStmt();
 
         unsigned NewParentScope = Scopes.size();
-        Scopes.emplace_back(ParentScope,
-                            diag::note_omp_protected_structured_block,
-                            diag::note_omp_exits_structured_block,
-                            Block->getBeginLoc());
+        Scopes.emplace_back(
+            ParentScope, diag::note_omp_protected_structured_block,
+            diag::note_omp_exits_structured_block, Block->getBeginLoc());
         BuildScopeInformation(Block, NewParentScope);
         return;
       }



More information about the cfe-commits mailing list