https://github.com/DavidSpickett created 
https://github.com/llvm/llvm-project/pull/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 asserted that that 
is the case, then cast the distance to the unsigned type.

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

>From b4f217e3db511708c3f2402c8b9fea31053b85f2 Mon Sep 17 00:00:00 2001
From: David Spickett <[email protected]>
Date: Wed, 17 Dec 2025 10:15:44 +0000
Subject: [PATCH] [clang-format] Fix comparison warning in 32-bit builds

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 asserted that that is the case, then cast the
distance to the unsigned type.

I think this warning does not occur in 64-bit builds
becuase there it is safe to extend the unsigned 32-bit
integer to the 64-bit signed distance type.
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 913789afd9919..3a6ab8de7c476 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -642,7 +642,9 @@ 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)
+      auto Distance = std::distance(I, E);
+      assert(Distance >= 0);
+      if (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.

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

Reply via email to