[clang] [clang][OpenACC] Fix crash on null associated stmt in atomic template instantiation (PR #249371)
Kavya Bhanvadia via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 5 23:55:03 PST 2026
https://github.com/DeviceDidWhat created https://github.com/llvm/llvm-project/pull/249371
When the statement associated with an `acc atomic` directive inside a template is invalid (e.g. it uses an undeclared identifier), the construct is created with a null associated statement after the error is diagnosed. Instantiating the template then called `isa<DeclStmt>` on that null pointer in `TransformOpenACCAtomicConstruct`, hitting an assertion.
Check for a null associated statement before the `isa<DeclStmt>` check. The error was already diagnosed when the template was first parsed, so nothing more needs to be checked.
Adds a regression test to `SemaOpenACC/atomic-construct.cpp`.
assisted by claude sonnet 5.5 and reviewed by me.
Fixes #249313
>From 8857069f203b0728db5fcd7d21c08cd454372f2a Mon Sep 17 00:00:00 2001
From: Kavya <kavyabhanvadia at gmail.com>
Date: Sun, 6 Dec 2026 13:23:39 +0530
Subject: [PATCH] [clang][OpenACC] Fix crash on null associated stmt in atomic
template instantiation
---
clang/lib/Sema/TreeTransform.h | 6 ++++--
clang/test/SemaOpenACC/atomic-construct.cpp | 10 ++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 797984fa54b5ea..b0ef0b64e7659a 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13665,9 +13665,11 @@ StmtResult TreeTransform<Derived>::TransformOpenACCAtomicConstruct(
StmtResult AssocStmt = getDerived().TransformStmt(C->getAssociatedStmt());
- // DeclStmt is never valid here. Our pass through this while dependent already
+ // The associated statement can be null if it was invalid when we first
+ // processed this construct (the error was already diagnosed). DeclStmt is
+ // never valid here either, but our pass through this while dependent already
// diagnosed it, so there is no reason to check it again.
- if (!isa<DeclStmt>(C->getAssociatedStmt()))
+ if (C->getAssociatedStmt() && !isa<DeclStmt>(C->getAssociatedStmt()))
AssocStmt = getSema().OpenACC().ActOnAssociatedStmt(
C->getBeginLoc(), C->getDirectiveKind(), C->getAtomicKind(), {},
AssocStmt);
diff --git a/clang/test/SemaOpenACC/atomic-construct.cpp b/clang/test/SemaOpenACC/atomic-construct.cpp
index 5c75a95420baa0..98375407149751 100644
--- a/clang/test/SemaOpenACC/atomic-construct.cpp
+++ b/clang/test/SemaOpenACC/atomic-construct.cpp
@@ -1901,3 +1901,13 @@ template <typename T> void DeclAssocStmt() {
}
void DeclAssocStmtUse() { DeclAssocStmt<int>(); }
+
+namespace gh249313 {
+template <typename T> void UndeclaredVar(T x) {
+ // expected-error at +2 {{use of undeclared identifier 'bar'}}
+#pragma acc atomic
+ bar = 42;
+}
+
+void Use() { UndeclaredVar<int>(2); }
+} // namespace gh249313
More information about the cfe-commits
mailing list