Author: Timm Baeder
Date: 2026-01-07T14:49:23+01:00
New Revision: 4fdbe05d6e0c5ab8e244e14635ed6f04e9d2a1cc

URL: 
https://github.com/llvm/llvm-project/commit/4fdbe05d6e0c5ab8e244e14635ed6f04e9d2a1cc
DIFF: 
https://github.com/llvm/llvm-project/commit/4fdbe05d6e0c5ab8e244e14635ed6f04e9d2a1cc.diff

LOG: [clang][bytecode] Fix some imag/real corner cases (#174764)

Fix real/imag when taking a primitive parameter _and_ being discarded,
and fix the case where their subexpression can't be classified.

Fixes https://github.com/llvm/llvm-project/issues/174668

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/AST/ByteCode/complex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp 
b/clang/lib/AST/ByteCode/Compiler.cpp
index b4449def1c6f0..a539f31db0ca6 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6792,13 +6792,17 @@ bool Compiler<Emitter>::VisitUnaryOperator(const 
UnaryOperator *E) {
       return false;
     return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
   case UO_Real: // __real x
-    assert(T);
+    if (!T)
+      return false;
     return this->delegate(SubExpr);
   case UO_Imag: { // __imag x
-    assert(T);
+    if (!T)
+      return false;
     if (!this->discard(SubExpr))
       return false;
-    return this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
+    return DiscardResult
+               ? true
+               : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
   }
   case UO_Extension:
     return this->delegate(SubExpr);

diff  --git a/clang/test/AST/ByteCode/complex.cpp 
b/clang/test/AST/ByteCode/complex.cpp
index be10b3cfa53da..182162d251ece 100644
--- a/clang/test/AST/ByteCode/complex.cpp
+++ b/clang/test/AST/ByteCode/complex.cpp
@@ -410,3 +410,29 @@ namespace ComplexConstexpr {
   static_assert(__imag test6 == 6, "");
   static_assert(&__imag test6 == &__real test6 + 1, "");
 }
+
+namespace Discard {
+  constexpr int test1() {
+    __imag(0);
+    __imag(0.0);
+    __real(0);
+    __real(0.0);
+
+    return 10;
+  }
+  static_assert(test1() == 10, "");
+
+  constexpr int test2() {
+    __imag(bar()); // both-error {{use of undeclared identifier}}
+    return 10;
+  }
+  static_assert(test2() == 10, ""); // both-error {{not an integral constant 
expression}}
+
+  constexpr int test3() {
+    __real(barz()); // both-error {{use of undeclared identifier}}
+    return 10;
+  }
+  static_assert(test3() == 10, ""); // both-error {{not an integral constant 
expression}}
+
+
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to