Skip to content

[Clang] Handle real and imaginary parts of complex lvalue in APValue::printPretty #69252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2023

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Oct 16, 2023

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

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

llvmbot commented Oct 16, 2023

@llvm/pr-subscribers-clang

Author: Yingwei Zheng (dtcxzyw)

Changes

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


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

2 Files Affected:

  • (modified) clang/lib/AST/APValue.cpp (+4)
  • (modified) clang/test/CodeGen/complex.c (+5)
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() << ']';
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);
+}

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! You should also add a release note to clang/docs/ReleaseNotes.rst so users know about the fix.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM

@dtcxzyw dtcxzyw merged commit 48508c1 into llvm:main Oct 23, 2023
@dtcxzyw dtcxzyw deleted the complex-print branch October 23, 2023 13:40
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.

clang: trunk: crashes at clang::APValue::printPretty, Assertion `isa<ArrayType>(CanonicalType)' failed.
3 participants