[clang] [clang][Interp] Fix creating APSInt from IntegralAP (PR #71410)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 08:01:26 PST 2023


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

The boolean argument in the APSInt constructor is IsUnsigned, not IsSigned.

This marks the 10th time I've run into this issue. Happy anniversary.

>From 81836da057f2703f49f30d31955306577a0e1170 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 6 Nov 2023 16:59:33 +0100
Subject: [PATCH] [clang][Interp] Fix creating APSInt from IntegralAP

The boolean argument in the APSInt constructor is IsUnsigned, not
IsSigned.

This marks the 10th time I've run into this issue. Happy anniversary.
---
 clang/lib/AST/Interp/IntegralAP.h | 4 ++--
 clang/test/AST/Interp/intap.cpp   | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index 1f535d420bcd54b..134b602d1bb61ca 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -119,8 +119,8 @@ 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 { return APSInt(V, !Signed); }
+  APValue toAPValue() const {return APValue(APSInt(V, !Signed)); }
 
   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 45961e6fc74b7a7..5f08b76a565c25a 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -56,6 +56,10 @@ namespace i128 {
 
   static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
   static_assert(UINT128_MAX == -1, "");
+  static_assert(UINT128_MAX == 1, ""); // expected-error {{static assertion failed}} \
+                                       // expected-note {{'340282366920938463463374607431768211455 == 1'}} \
+                                       // ref-error {{static assertion failed}} \
+                                       // ref-note {{'340282366920938463463374607431768211455 == 1'}}
 
   static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
   static_assert(INT128_MAX != 0, "");



More information about the cfe-commits mailing list