Skip to content

[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp #143236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jun 7, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

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

1 Files Affected:

  • (modified) clang/tools/clang-format/ClangFormat.cpp (+22-21)
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index b22d3aaf3183b..441ed442a30a9 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -244,17 +244,17 @@ static bool fillRanges(MemoryBuffer *Code,
   DiagnosticsEngine Diagnostics(
       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), DiagOpts);
   SourceManager Sources(Diagnostics, Files);
-  FileID ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
-                                 InMemoryFileSystem.get());
+  const auto ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
+                                     InMemoryFileSystem.get());
   if (!LineRanges.empty()) {
     if (!Offsets.empty() || !Lengths.empty()) {
       errs() << "error: cannot use -lines with -offset/-length\n";
       return true;
     }
 
-    for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
+    for (const auto &LineRange : LineRanges) {
       unsigned FromLine, ToLine;
-      if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
+      if (parseLineRange(LineRange, FromLine, ToLine)) {
         errs() << "error: invalid <start line>:<end line> pair\n";
         return true;
       }
@@ -266,12 +266,12 @@ static bool fillRanges(MemoryBuffer *Code,
         errs() << "error: start line should not exceed end line\n";
         return true;
       }
-      SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
-      SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
+      const auto Start = Sources.translateLineCol(ID, FromLine, 1);
+      const auto End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
       if (Start.isInvalid() || End.isInvalid())
         return true;
-      unsigned Offset = Sources.getFileOffset(Start);
-      unsigned Length = Sources.getFileOffset(End) - Offset;
+      const auto Offset = Sources.getFileOffset(Start);
+      const auto Length = Sources.getFileOffset(End) - Offset;
       Ranges.push_back(tooling::Range(Offset, Length));
     }
     return false;
@@ -284,27 +284,28 @@ static bool fillRanges(MemoryBuffer *Code,
     errs() << "error: number of -offset and -length arguments must match.\n";
     return true;
   }
-  for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
-    if (Offsets[i] >= Code->getBufferSize()) {
-      errs() << "error: offset " << Offsets[i] << " is outside the file\n";
+  for (unsigned I = 0, E = Offsets.size(), Size = Lengths.size(); I < E; ++I) {
+    auto Offset = Offsets[I];
+    if (Offset >= Code->getBufferSize()) {
+      errs() << "error: offset " << Offset << " is outside the file\n";
       return true;
     }
-    SourceLocation Start =
-        Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
+    const auto Start =
+        Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
     SourceLocation End;
-    if (i < Lengths.size()) {
-      if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
-        errs() << "error: invalid length " << Lengths[i]
-               << ", offset + length (" << Offsets[i] + Lengths[i]
-               << ") is outside the file.\n";
+    if (I < Size) {
+      const auto L = Lengths[I];
+      if (Offset + L > Code->getBufferSize()) {
+        errs() << "error: invalid length " << L << ", offset + length ("
+               << Offset + L << ") is outside the file.\n";
         return true;
       }
-      End = Start.getLocWithOffset(Lengths[i]);
+      End = Start.getLocWithOffset(L);
     } else {
       End = Sources.getLocForEndOfFile(ID);
     }
-    unsigned Offset = Sources.getFileOffset(Start);
-    unsigned Length = Sources.getFileOffset(End) - Offset;
+    Offset = Sources.getFileOffset(Start);
+    const auto Length = Sources.getFileOffset(End) - Offset;
     Ranges.push_back(tooling::Range(Offset, Length));
   }
   return false;

- Don't recalculate Offset for -offset.
- Calculate Length only if -length is unspecified.
@owenca owenca requested a review from HazardyKnusperkeks June 7, 2025 07:40
@owenca owenca merged commit 897ccdd into llvm:main Jun 8, 2025
6 of 7 checks passed
@owenca owenca deleted the nfc branch June 8, 2025 03:59
@slackito
Copy link
Collaborator

slackito commented Jun 9, 2025

It seems this change has introduced a bug when formatting multiple files in one go. Passing a shorter file after a longer one results in a stale length being used. It can be reproduced with trivially short files:

$ cat ~/a.cc

$ cat ~/b.cc
// a
$ bin/clang-format ~/b.cc ~/a.cc
// a
error: invalid length 5, offset + length (5) is outside the file.

This was supposed to be an NFC change so I'm going to revert the patch while the problem gets investigated/fixed.

slackito added a commit that referenced this pull request Jun 9, 2025
…143236)"

This reverts commit 897ccdd. It
introduced a bug when formatting multiple files in one go. When a
shorter file is passed after a longer one, a stale length from the
previous file seems to be used, triggering an "invalid length (...) is
outside the file" error.
owenca added a commit to owenca/llvm-project that referenced this pull request Jun 10, 2025
owenca added a commit to owenca/llvm-project that referenced this pull request Jun 10, 2025
owenca added a commit to owenca/llvm-project that referenced this pull request Jun 10, 2025
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…lvm#143236)"

This reverts commit 897ccdd. It
introduced a bug when formatting multiple files in one go. When a
shorter file is passed after a longer one, a stale length from the
previous file seems to be used, triggering an "invalid length (...) is
outside the file" error.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…lvm#143236)"

This reverts commit 897ccdd. It
introduced a bug when formatting multiple files in one go. When a
shorter file is passed after a longer one, a stale length from the
previous file seems to be used, triggering an "invalid length (...) is
outside the file" error.
owenca added a commit that referenced this pull request Jun 12, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 12, 2025
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…lvm#143236)"

This reverts commit 897ccdd. It
introduced a bug when formatting multiple files in one go. When a
shorter file is passed after a longer one, a stale length from the
previous file seems to be used, triggering an "invalid length (...) is
outside the file" error.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
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.

4 participants