[clang] [Clang] Handle real and imaginary parts of complex lvalue in `APValue::printPretty` (PR #69252)

Yingwei Zheng via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 14:24:20 PDT 2023


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/69252

This patch handles formatting of real and imaginary parts of complex lvalue.
Fixes #69218.


>From 8f0ebe5b5cfed069c8274c0761559d6595d4dea8 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 17 Oct 2023 05:17:17 +0800
Subject: [PATCH 1/2] [Clang] Add pre-commit tests for PR69218. NFC.

---
 clang/test/CodeGen/complex.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/test/CodeGen/complex.c b/clang/test/CodeGen/complex.c
index 6233529a18f8b8d..b50d607d00c0167 100644
--- a/clang/test/CodeGen/complex.c
+++ b/clang/test/CodeGen/complex.c
@@ -113,3 +113,8 @@ void t92(void) {
   (0 ? (_Complex double) 2.0f : 2.0f);
 }
 
+// PR69218
+int t10(void) {
+  float _Complex a;
+  return (0 < &__real__ a) && (0 < &__imag__ a);
+}

>From c76a511cf1ad72eff3725bf24700f00a2f6fc014 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 17 Oct 2023 05:19:11 +0800
Subject: [PATCH 2/2] [Clang] Handle real and imaginary part of complex lvalue
 in `APValue::printPretty`

---
 clang/lib/AST/APValue.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index ef424215182280b..d08c2936b56dd45 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -841,6 +841,10 @@ void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
           Out << *VD;
           ElemTy = VD->getType();
         }
+      } else if (ElemTy->isAnyComplexType()) {
+        // The lvalue refers to a complex type
+        Out << (Path[I].getAsArrayIndex() == 0 ? ".real" : ".imag");
+        ElemTy = ElemTy->castAs<ComplexType>()->getElementType();
       } else {
         // The lvalue must refer to an array.
         Out << '[' << Path[I].getAsArrayIndex() << ']';



More information about the cfe-commits mailing list