Skip to content

Conversation

@DavidSpickett
Copy link
Collaborator

@DavidSpickett DavidSpickett commented Dec 17, 2025

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-clang-format

Author: David Spickett (DavidSpickett)

Changes

In our 32-bit Arm builds we see:

UnwrappedLineFormatter.cpp:645:31: warning: comparison of integers of different signs: 'typename iterator_traits&lt;AnnotatedLine *const *&gt;::difference_type' (aka 'int') and 'const unsigned int' [-Wsign-compare]
  645 |       if (std::distance(I, E) &lt;= 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 --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.

Co-authored-by: Björn Schäpers <github@hazardy.de>
@github-actions
Copy link

github-actions bot commented Dec 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@HazardyKnusperkeks HazardyKnusperkeks merged commit 8d3fb12 into llvm:main Dec 17, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants