[clang] Reapply "[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp" (PR #143477)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 10 08:33:59 PDT 2025
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/143477
>From 7f745330975e0e360e9a42b9d57c3e3afa3680f8 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 7 Jun 2025 20:59:53 -0700
Subject: [PATCH 1/2] [clang-format][NFC] Clean up fillRanges() in
ClangFormat.cpp (#143236)
---
clang/tools/clang-format/ClangFormat.cpp | 49 ++++++++++--------------
1 file changed, 21 insertions(+), 28 deletions(-)
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index b22d3aaf3183b..ad12672fa89c1 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;
@@ -279,32 +279,25 @@ static bool fillRanges(MemoryBuffer *Code,
if (Offsets.empty())
Offsets.push_back(0);
- if (Offsets.size() != Lengths.size() &&
- !(Offsets.size() == 1 && Lengths.empty())) {
+ if (Offsets.size() == 1 && Lengths.empty()) {
+ Lengths.push_back(Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) -
+ Offsets[0]);
+ } else if (Offsets.size() != Lengths.size()) {
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(); I < E; ++I) {
+ const 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]);
- 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";
- return true;
- }
- End = Start.getLocWithOffset(Lengths[i]);
- } else {
- End = Sources.getLocForEndOfFile(ID);
+ const auto Length = Lengths[I];
+ if (Offset + Length > Code->getBufferSize()) {
+ errs() << "error: invalid length " << Length << ", offset + length ("
+ << Offset + Length << ") is outside the file.\n";
+ return true;
}
- unsigned Offset = Sources.getFileOffset(Start);
- unsigned Length = Sources.getFileOffset(End) - Offset;
Ranges.push_back(tooling::Range(Offset, Length));
}
return false;
>From a4bfdc8e0400e61bf17776c91d9550fe205a6f4e Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 10 Jun 2025 08:33:13 -0700
Subject: [PATCH 2/2] Reland "[clang-format][NFC] Clean up fillRanges() in
ClangFormat.cpp"
Reapply #143236 and fix the bug reported in https://github.com/llvm/llvm-project/pull/143236#issuecomment-2957102180.
---
clang/tools/clang-format/ClangFormat.cpp | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index ad12672fa89c1..24ad3cb42254d 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -279,21 +279,24 @@ static bool fillRanges(MemoryBuffer *Code,
if (Offsets.empty())
Offsets.push_back(0);
- if (Offsets.size() == 1 && Lengths.empty()) {
- Lengths.push_back(Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) -
- Offsets[0]);
+ const bool EmptyLengths = Lengths.empty();
+ unsigned Length = 0;
+ if (Offsets.size() == 1 && EmptyLengths) {
+ Length = Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) - Offsets[0];
} else if (Offsets.size() != Lengths.size()) {
errs() << "error: number of -offset and -length arguments must match.\n";
return true;
}
- for (unsigned I = 0, E = Offsets.size(); I < E; ++I) {
+ for (unsigned I = 0, E = Offsets.size(), CodeSize = Code->getBufferSize();
+ I < E; ++I) {
const auto Offset = Offsets[I];
- if (Offset >= Code->getBufferSize()) {
+ if (Offset >= CodeSize) {
errs() << "error: offset " << Offset << " is outside the file\n";
return true;
}
- const auto Length = Lengths[I];
- if (Offset + Length > Code->getBufferSize()) {
+ if (!EmptyLengths)
+ Length = Lengths[I];
+ if (Offset + Length > CodeSize) {
errs() << "error: invalid length " << Length << ", offset + length ("
<< Offset + Length << ") is outside the file.\n";
return true;
More information about the cfe-commits
mailing list