[clang] [clang][Interp] Consider bit width in IntegralAP::toAPSInt() (PR #71646)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 8 07:46:26 PST 2023


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/71646

>From 8761ed0d3f0630dd53ed7ff8b6ce30c19dbd9e5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 8 Nov 2023 06:08:04 +0100
Subject: [PATCH] [clang][Interp] Consider bit width in toAPSInt()

---
 clang/lib/AST/Interp/IntegralAP.h | 12 ++++++++++--
 clang/test/AST/Interp/intap.cpp   |  9 +++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index b674082d9ea5812..88de1f1392e6813 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -119,8 +119,16 @@ template <bool Signed> class IntegralAP final {
 
   constexpr unsigned bitWidth() const { return V.getBitWidth(); }
 
-  APSInt toAPSInt(unsigned Bits = 0) const { return APSInt(V, !Signed); }
-  APValue toAPValue() const { return APValue(APSInt(V, !Signed)); }
+  APSInt toAPSInt(unsigned Bits = 0) const {
+    if (Bits == 0)
+      Bits = bitWidth();
+
+    if constexpr (Signed)
+      return APSInt(V.sext(Bits), !Signed);
+    else
+      return APSInt(V.zext(Bits), !Signed);
+  }
+  APValue toAPValue() const { return APValue(toAPSInt()); }
 
   bool isZero() const { return V.isZero(); }
   bool isPositive() const { return V.isNonNegative(); }
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index 5f08b76a565c25a..34c8d0565082994 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -63,11 +63,16 @@ namespace i128 {
 
   static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
   static_assert(INT128_MAX != 0, "");
+  static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \
+                                      // expected-note {{evaluates to '170141183460469231731687303715884105727 == 0'}} \
+                                      // ref-error {{failed}} \
+                                      // ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
+
   static const __int128_t INT128_MIN = -INT128_MAX - 1;
   constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \
-                                         // expected-note {{outside the range}} \
+                                         // expected-note {{value 170141183460469231731687303715884105728 is outside the range}} \
                                          // ref-error {{must be initialized by a constant expression}} \
-                                         // ref-note {{outside the range}}
+                                         // ref-note {{value 170141183460469231731687303715884105728 is outside the range}}
   constexpr int128_t Two = (int128_t)1 << 1ul;
   static_assert(Two == 2, "");
   static_assert(Two, "");



More information about the cfe-commits mailing list