[clang] [clang][Interp] Implement IntegralAP::truncate() (PR #69912)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 23 03:25:15 PDT 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/69912

None

>From a9e872ec17d31baad095d12a1fdca15a2a5965ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 23 Oct 2023 12:23:45 +0200
Subject: [PATCH] [clang][Interp] Implement IntegralAP::truncate()

---
 clang/lib/AST/Interp/IntegralAP.h |  5 ++---
 clang/test/AST/Interp/intap.cpp   | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index 45e5b49546270aa..f8d47ca183f3a67 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -137,9 +137,8 @@ template <bool Signed> class IntegralAP final {
     return NameStr;
   }
 
-  IntegralAP truncate(unsigned bitWidth) const {
-    assert(false);
-    return V;
+  IntegralAP truncate(unsigned BitWidth) const {
+    return IntegralAP(V.trunc(BitWidth));
   }
 
   IntegralAP<false> toUnsigned() const {
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index 27fae1b904351ce..c19e8c1367e9f4f 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -118,4 +118,20 @@ namespace AddSubOffset {
   static_assert(*P2 == 1,"");
 }
 
+namespace Bitfields {
+  struct S1 {
+    unsigned _BitInt(128) a : 2;
+  };
+  constexpr S1 s1{100}; // ref-warning {{changes value from 100 to 0}} \
+                        // expected-warning {{changes value from 100 to 0}}
+  constexpr S1 s12{3};
+  static_assert(s12.a == 3, "");
+
+  struct S2 {
+    unsigned __int128 a : 2;
+  };
+  constexpr S2 s2{100}; // ref-warning {{changes value from 100 to 0}} \
+                        // expected-warning {{changes value from 100 to 0}}
+}
+
 #endif



More information about the cfe-commits mailing list