[llvm] 134ffa8 - NFC: Fix -Wsign-compare warnings on 32-bit builds

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 20 17:52:20 PDT 2020


Author: Hubert Tong
Date: 2020-10-20T20:52:10-04:00
New Revision: 134ffa8138c31444685013e10f592cd7c88d675b

URL: https://github.com/llvm/llvm-project/commit/134ffa8138c31444685013e10f592cd7c88d675b
DIFF: https://github.com/llvm/llvm-project/commit/134ffa8138c31444685013e10f592cd7c88d675b.diff

LOG: NFC: Fix -Wsign-compare warnings on 32-bit builds

Comparing 32-bit `ptrdiff_t` against 32-bit `unsigned` results in
`-Wsign-compare` warnings for both GCC and Clang.

The warning for the cases in question appear to identify an issue
where the `ptrdiff_t` value would be mutated via conversion to an
unsigned type.

The warning is resolved by using the usual arithmetic conversions to
safely preserve the value of the `unsigned` operand while trying to
convert to a signed type. Host platforms where `unsigned` has the same
width as `unsigned long long` will need to make a different change, but
using an explicit cast has disadvantages that can be avoided for now.

Reviewed By: dantrushin

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

Added: 
    

Modified: 
    clang/lib/Serialization/ASTReaderStmt.cpp
    llvm/lib/CodeGen/StackMaps.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp
index c154c146727e..363527f884b3 100644
--- a/clang/lib/Serialization/ASTReaderStmt.cpp
+++ b/clang/lib/Serialization/ASTReaderStmt.cpp
@@ -2186,9 +2186,9 @@ void ASTStmtReader::VisitRecoveryExpr(RecoveryExpr *E) {
   unsigned NumArgs = Record.readInt();
   E->BeginLoc = readSourceLocation();
   E->EndLoc = readSourceLocation();
-  assert(
-      (NumArgs == std::distance(E->children().begin(), E->children().end())) &&
-      "Wrong NumArgs!");
+  assert((NumArgs + 0LL ==
+          std::distance(E->children().begin(), E->children().end())) &&
+         "Wrong NumArgs!");
   (void)NumArgs;
   for (Stmt *&Child : E->children())
     Child = Record.readSubStmt();

diff  --git a/llvm/lib/CodeGen/StackMaps.cpp b/llvm/lib/CodeGen/StackMaps.cpp
index f7fb2e824bb9..ee1a4a47b4cc 100644
--- a/llvm/lib/CodeGen/StackMaps.cpp
+++ b/llvm/lib/CodeGen/StackMaps.cpp
@@ -401,7 +401,7 @@ void StackMaps::parseStatepointOpers(const MachineInstr &MI,
     SmallVector<unsigned, 8> GCPtrIndices;
     unsigned GCPtrIdx = (unsigned)SO.getFirstGCPtrIdx();
     assert((int)GCPtrIdx != -1);
-    assert(MOI - MI.operands_begin() == GCPtrIdx);
+    assert(MOI - MI.operands_begin() == GCPtrIdx + 0LL);
     while (NumGCPointers--) {
       GCPtrIndices.push_back(GCPtrIdx);
       GCPtrIdx = StackMaps::getNextMetaArgIdx(&MI, GCPtrIdx);


        


More information about the llvm-commits mailing list