Skip to content

Conversation

@devajithvs
Copy link
Contributor

#121943 rewrote __atomic_test_and_set and __atomic_clear to be lowered through AtomicExpr

StmtPrinter::VisitAtomicExpr still treated them like other atomic builtins with a Val1 operand. This led to incorrect pretty-printing when dumping the AST.

Skip Val1 for these two builtins like atomic loads.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 19, 2025

@llvm/pr-subscribers-clang

Author: Devajith (devajithvs)

Changes

#121943 rewrote __atomic_test_and_set and __atomic_clear to be lowered through AtomicExpr

StmtPrinter::VisitAtomicExpr still treated them like other atomic builtins with a Val1 operand. This led to incorrect pretty-printing when dumping the AST.

Skip Val1 for these two builtins like atomic loads.


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

2 Files Affected:

  • (modified) clang/lib/AST/StmtPrinter.cpp (+3-1)
  • (modified) clang/test/SemaCXX/ast-print.cpp (+11)
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index 0030300521128..8f348f6e5bc73 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -2028,7 +2028,9 @@ void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
       Node->getOp() != AtomicExpr::AO__atomic_load_n &&
       Node->getOp() != AtomicExpr::AO__scoped_atomic_load_n &&
       Node->getOp() != AtomicExpr::AO__opencl_atomic_load &&
-      Node->getOp() != AtomicExpr::AO__hip_atomic_load) {
+      Node->getOp() != AtomicExpr::AO__hip_atomic_load &&
+      Node->getOp() != AtomicExpr::AO__atomic_test_and_set &&
+      Node->getOp() != AtomicExpr::AO__atomic_clear) {
     OS << ", ";
     PrintExpr(Node->getVal1());
   }
diff --git a/clang/test/SemaCXX/ast-print.cpp b/clang/test/SemaCXX/ast-print.cpp
index 2cb1ec440b6bb..2dc32c20b7296 100644
--- a/clang/test/SemaCXX/ast-print.cpp
+++ b/clang/test/SemaCXX/ast-print.cpp
@@ -176,6 +176,17 @@ float test15() {
   return __builtin_asinf(1.0F);
 }
 
+// CHECK: void test16() {
+// CHECK:   char *ptr;
+// CHECK:   __atomic_test_and_set(ptr, 0);
+// CHECK:   __atomic_clear(ptr, 0);
+// CHECK: }
+void test16() {
+  char *ptr;
+  __atomic_test_and_set(ptr, 0);
+  __atomic_clear(ptr, 0);
+}
+
 namespace PR18776 {
 struct A {
   operator void *();

@devajithvs
Copy link
Contributor Author

devajithvs commented Sep 19, 2025

We currently print:

$ echo 'char *a; void b() { __atomic_test_and_set(a, 0);}' | clang -cc1 -ast-print
char *a;
void b() {
    __atomic_test_and_set(a, <null expr>, 0);
}

This was probably missed in #121943

Discovered when debugging issues upgrading ROOT/Cling to LLVM20: root-project/root#19242

CC: @ostannard, @shafik, @jyknight

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

Comment on lines 184 to 270
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests for the other atomic load builtins? It might be worth checking them all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other places we do a similar check? Maybe we need that to me a member function of AtomicExpr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we do a similar check here, but it is part of a bigger switch case.

case AtomicExpr::AO__atomic_load_n:
case AtomicExpr::AO__scoped_atomic_load_n:
case AtomicExpr::AO__c11_atomic_load:
case AtomicExpr::AO__opencl_atomic_load:
case AtomicExpr::AO__hip_atomic_load:
case AtomicExpr::AO__atomic_test_and_set:
case AtomicExpr::AO__atomic_clear:
break;

llvm#121943 rewrote
`__atomic_test_and_set` and `__atomic_clear` to be lowered through AtomicExpr

StmtPrinter::VisitAtomicExpr still treated them like other atomic builtins
with a Val1 operand. This led to incorrect pretty-printing when dumping
the AST.

Skip Val1 for these two builtins like atomic loads.
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@devajithvs
Copy link
Contributor Author

Thanks for reviewing @cor3ntin. Would you mind merging this? (I don't have commit access yet)

@cor3ntin cor3ntin merged commit ba49062 into llvm:main Sep 19, 2025
10 checks passed
@devajithvs devajithvs deleted the fix_dump_atomic branch September 19, 2025 13:18
devajithvs added a commit to devajithvs/llvm-project that referenced this pull request Sep 19, 2025
…omic_clear` (llvm#159712)

llvm#121943 rewrote
`__atomic_test_and_set` and `__atomic_clear` to be lowered through
AtomicExpr

StmtPrinter::VisitAtomicExpr still treated them like other atomic
builtins with a Val1 operand. This led to incorrect pretty-printing when
dumping the AST.

Skip Val1 for these two builtins like atomic loads.

(cherry picked from commit ba49062)
devajithvs added a commit to devajithvs/llvm-project that referenced this pull request Sep 25, 2025
…omic_clear` (llvm#159712)

llvm#121943 rewrote
`__atomic_test_and_set` and `__atomic_clear` to be lowered through
AtomicExpr

StmtPrinter::VisitAtomicExpr still treated them like other atomic
builtins with a Val1 operand. This led to incorrect pretty-printing when
dumping the AST.

Skip Val1 for these two builtins like atomic loads.

(cherry picked from commit ba49062)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants