[llvm] 76f5c5a - [ADT][Support] Fix C4146 error from MSVC

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 15 14:34:24 PST 2021


Author: Vladislav Vinogradov
Date: 2021-01-15T14:34:14-08:00
New Revision: 76f5c5a7b059929f0e0352ad4ff5ec1b78511868

URL: https://github.com/llvm/llvm-project/commit/76f5c5a7b059929f0e0352ad4ff5ec1b78511868
DIFF: https://github.com/llvm/llvm-project/commit/76f5c5a7b059929f0e0352ad4ff5ec1b78511868.diff

LOG: [ADT][Support] Fix C4146 error from MSVC

Unary minus operator applied to unsigned type, result still unsigned.

Use `~0U` instead of `-1U` and `1 + ~VAL` instead of `-VAL`.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D94417

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringExtras.h
    llvm/include/llvm/Support/MathExtras.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index caa1ed547bb2..fe5c5967a2cf 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -71,7 +71,7 @@ inline unsigned hexDigitValue(char C) {
     constexpr HexTable() {
       // Default initialize everything to invalid.
       for (int i = 0; i < 255; ++i)
-        LUT[i] = -1U;
+        LUT[i] = ~0U;
       // Initialize `0`-`9`.
       for (int i = 0; i < 10; ++i)
         LUT['0' + i] = i;
@@ -88,7 +88,7 @@ inline unsigned hexDigitValue(char C) {
 inline bool isDigit(char C) { return C >= '0' && C <= '9'; }
 
 /// Checks if character \p C is a hexadecimal numeric character.
-inline bool isHexDigit(char C) { return hexDigitValue(C) != -1U; }
+inline bool isHexDigit(char C) { return hexDigitValue(C) != ~0U; }
 
 /// Checks if character \p C is a valid letter as classified by "C" locale.
 inline bool isAlpha(char C) {
@@ -184,7 +184,7 @@ inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
 inline bool tryGetHexFromNibbles(char MSB, char LSB, uint8_t &Hex) {
   unsigned U1 = hexDigitValue(MSB);
   unsigned U2 = hexDigitValue(LSB);
-  if (U1 == -1U || U2 == -1U)
+  if (U1 == ~0U || U2 == ~0U)
     return false;
 
   Hex = static_cast<uint8_t>((U1 << 4) | U2);
@@ -291,7 +291,7 @@ inline std::string utostr(uint64_t X, bool isNeg = false) {
 
 inline std::string itostr(int64_t X) {
   if (X < 0)
-    return utostr(-static_cast<uint64_t>(X), true);
+    return utostr(static_cast<uint64_t>(1) + ~static_cast<uint64_t>(X), true);
   else
     return utostr(static_cast<uint64_t>(X));
 }

diff  --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 16da3046c8ce..33b9065261e8 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -440,7 +440,7 @@ inline uint64_t maxUIntN(uint64_t N) {
 inline int64_t minIntN(int64_t N) {
   assert(N > 0 && N <= 64 && "integer width out of range");
 
-  return -(UINT64_C(1)<<(N-1));
+  return UINT64_C(1) + ~(UINT64_C(1) << (N - 1));
 }
 
 /// Gets the maximum value for a N-bit signed integer.


        


More information about the llvm-commits mailing list