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

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 17 02:19:53 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: David Spickett (DavidSpickett)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/172627.diff


1 Files Affected:

- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+3-1) 


``````````diff
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.

``````````

</details>


https://github.com/llvm/llvm-project/pull/172627


More information about the cfe-commits mailing list