Author: David Spickett
Date: 2025-12-17T21:06:10+01:00
New Revision: 8d3fb12b5b2fb6e78afc2844f491136286d162a4

URL: 
https://github.com/llvm/llvm-project/commit/8d3fb12b5b2fb6e78afc2844f491136286d162a4
DIFF: 
https://github.com/llvm/llvm-project/commit/8d3fb12b5b2fb6e78afc2844f491136286d162a4.diff

LOG: [clang-format] Fix comparison warning in 32-bit builds (#172627)

In our 32-bit Arm builds we see:
```
UnwrappedLineFormatter.cpp:645:31: warning: comparison of integers of different 
signs: 'typename iterator_traits<AnnotatedLine *const *>::difference_type' (aka 
'int') and 'const unsigned int' [-Wsign-compare]
  645 |       if (std::distance(I, E) <= N)
      |           ~~~~~~~~~~~~~~~~~~~ ^  ~
```

The existing comparison seems to assume that the distance will not be
negative. So to fix this warning I've cast the distance to the unsigned
type.

I think this warning does not occur in 64-bit builds because there it is
safe to extend the unsigned 32-bit integer to the 64-bit signed distance
type.

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineFormatter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 913789afd9919..8589aa83f6c55 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -642,8 +642,10 @@ class LineJoiner {
         return 0;
       const auto N = MergedLines + LinesToBeMerged;
       // Check if there is even a line after the inner result.
-      if (std::distance(I, E) <= N)
+      if (auto Distance = std::distance(I, E);
+          static_cast<decltype(N)>(Distance) <= N) {
         return 0;
+      }
       // Check that the line after the inner result starts with a closing brace
       // which we are permitted to merge into one line.
       if (I[N]->First->is(TT_NamespaceRBrace) &&


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to