[clang] 9cf8f81 - Fix _BitInt suffix width calculation

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 07:00:57 PDT 2022


Author: Aaron Ballman
Date: 2022-03-22T10:00:05-04:00
New Revision: 9cf8f81ca45de198013f29442a7de6600b226d70

URL: https://github.com/llvm/llvm-project/commit/9cf8f81ca45de198013f29442a7de6600b226d70
DIFF: https://github.com/llvm/llvm-project/commit/9cf8f81ca45de198013f29442a7de6600b226d70.diff

LOG: Fix _BitInt suffix width calculation

@mgehre-amd pointed out the following post-commit review feedback on
the changes in 8cba72177dcd8de5d37177dbaf2347e5c1f0f1e8:

As an example, the paper says 3wb /* Yields an _BitInt(3); two value
bits, one sign bit */.
So I would expect that 0xFwb gives _BitInt(5); four value bits, one
sign bit, but with this implementation I get _BitInt(2).
This is because ResultVal as 4 bits, and getMinSignedBits() inteprets
it as negative and thus says that 1 bit is enough to represent -1.

This corrects the behavior for calculating the bit-width and adds some
test coverage.

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/Lexer/bitint-constants.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index be90c58e066d8..7fdeb7a8e30be 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -3979,8 +3979,8 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
       if (Literal.isBitInt) {
         // The signed version has one more bit for the sign value. There are no
         // zero-width bit-precise integers, even if the literal value is 0.
-        Width = Literal.isUnsigned ? std::max(ResultVal.getActiveBits(), 1u)
-                                   : std::max(ResultVal.getMinSignedBits(), 2u);
+        Width = std::max(ResultVal.getActiveBits(), 1u) +
+                (Literal.isUnsigned ? 0u : 1u);
 
         // Diagnose if the width of the constant is larger than BITINT_MAXWIDTH,
         // and reset the type to the largest supported width.

diff  --git a/clang/test/Lexer/bitint-constants.c b/clang/test/Lexer/bitint-constants.c
index 243f8c0377c27..2ff35e8207786 100644
--- a/clang/test/Lexer/bitint-constants.c
+++ b/clang/test/Lexer/bitint-constants.c
@@ -142,3 +142,18 @@ void ValidSuffixInvalidValue(void) {
   0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1wb; // expected-error {{integer literal is too large to be represented in any signed integer type}}
   0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1uwb; // expected-error {{integer literal is too large to be represented in any integer type}}
 }
+
+void TestTypes(void) {
+  // 2 value bits, one sign bit
+  _Static_assert(__builtin_types_compatible_p(__typeof__(3wb), _BitInt(3)));
+  // 2 value bits, one sign bit
+  _Static_assert(__builtin_types_compatible_p(__typeof__(-3wb), _BitInt(3)));
+  // 2 value bits, no sign bit
+  _Static_assert(__builtin_types_compatible_p(__typeof__(3uwb), unsigned _BitInt(2)));
+  // 4 value bits, one sign bit
+  _Static_assert(__builtin_types_compatible_p(__typeof__(0xFwb), _BitInt(5)));
+  // 4 value bits, one sign bit
+  _Static_assert(__builtin_types_compatible_p(__typeof__(-0xFwb), _BitInt(5)));
+  // 4 value bits, no sign bit
+  _Static_assert(__builtin_types_compatible_p(__typeof__(0xFuwb), unsigned _BitInt(4)));
+}


        


More information about the cfe-commits mailing list