[clang] [OpenACC] Fix template instantiation of atomic decl-stmt. (PR #245673)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 08:50:17 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Erich Keane (erichkeane)

<details>
<summary>Changes</summary>

We originally replaced all 'failed' expressions to an atomic with a RecoveryExpr. However, when we do this with a DeclStmt, this results in it not properly being able to be instantiated/looked up later.

This patch just adds it to the AST unwrapped.  Additionally, we suppress it from being checked again during template instantiation.

Fixes: #<!-- -->245108

---
Full diff: https://github.com/llvm/llvm-project/pull/245673.diff


3 Files Affected:

- (modified) clang/lib/Sema/SemaOpenACCAtomic.cpp (+7) 
- (modified) clang/lib/Sema/TreeTransform.h (+7-3) 
- (modified) clang/test/SemaOpenACC/atomic-construct.cpp (+10) 


``````````diff
diff --git a/clang/lib/Sema/SemaOpenACCAtomic.cpp b/clang/lib/Sema/SemaOpenACCAtomic.cpp
index 66b84ed20217c2..0256c6267045b8 100644
--- a/clang/lib/Sema/SemaOpenACCAtomic.cpp
+++ b/clang/lib/Sema/SemaOpenACCAtomic.cpp
@@ -45,6 +45,13 @@ class AtomicOperandChecker {
       return StmtError();
 
     Expr *E = dyn_cast<Expr>(AssocStmt.get());
+
+    // DeclStmt can't be wrapped in a RecoveryExpr, and dropping it causes
+    // template instantiation to not be able to find the declaration, so we have
+    // to continue representing it as itself.
+    if (isa<DeclStmt>(AssocStmt.get()))
+      return AssocStmt;
+
     QualType T = E ? E->getType() : SemaRef.getASTContext().DependentTy;
 
     return RecoveryExpr::Create(SemaRef.getASTContext(), T,
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 87a0f40584384e..84a0e52fecfa53 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13629,9 +13629,13 @@ StmtResult TreeTransform<Derived>::TransformOpenACCAtomicConstruct(
       getSema().OpenACC(), C->getDirectiveKind(), C->getDirectiveLoc(), {}, {});
 
   StmtResult AssocStmt = getDerived().TransformStmt(C->getAssociatedStmt());
-  AssocStmt = getSema().OpenACC().ActOnAssociatedStmt(
-      C->getBeginLoc(), C->getDirectiveKind(), C->getAtomicKind(), {},
-      AssocStmt);
+
+  // DeclStmt is never valid here. Our pass through this while dependent already
+  // diagnosed it, so there is no reason to check it again.
+  if (!isa<DeclStmt>(C->getAssociatedStmt()))
+    AssocStmt = getSema().OpenACC().ActOnAssociatedStmt(
+        C->getBeginLoc(), C->getDirectiveKind(), C->getAtomicKind(), {},
+        AssocStmt);
 
   return getDerived().RebuildOpenACCAtomicConstruct(
       C->getBeginLoc(), C->getDirectiveLoc(), C->getAtomicKind(),
diff --git a/clang/test/SemaOpenACC/atomic-construct.cpp b/clang/test/SemaOpenACC/atomic-construct.cpp
index 056154e5dad3df..5c75a95420baa0 100644
--- a/clang/test/SemaOpenACC/atomic-construct.cpp
+++ b/clang/test/SemaOpenACC/atomic-construct.cpp
@@ -1891,3 +1891,13 @@ template<class T> void bar(T baz) {
   baz = baz << foo;
 }
 }
+
+template <typename T> void DeclAssocStmt() {
+  // expected-error at +2 {{statement associated with OpenACC 'atomic' directive is invalid}}
+  // expected-note at +2 {{expected assignment, compound assignment, increment, or decrement expression}}
+#pragma acc atomic
+  int i;
+  i = 42;
+}
+
+void DeclAssocStmtUse() { DeclAssocStmt<int>(); }

``````````

</details>


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


More information about the cfe-commits mailing list