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

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 3 01:45:26 PDT 2023


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

>From def4af21f38857311d3f6209d3b731f21c4d3a12 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 9aefea6d0c47ed9..1f535d420bcd54b 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -140,9 +140,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 02a860eb0986c15..db9f516131af474 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -121,4 +121,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