[clang] [clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp (PR #143236)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 7 20:59:16 PDT 2025
================
@@ -284,27 +284,23 @@ 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) {
+ 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";
+ unsigned Length;
+ if (I < Size) {
+ Length = Lengths[I];
+ if (Offset + Length > Code->getBufferSize()) {
+ errs() << "error: invalid length " << Length << ", offset + length ("
+ << Offset + Length << ") is outside the file.\n";
return true;
}
- End = Start.getLocWithOffset(Lengths[i]);
} else {
- End = Sources.getLocForEndOfFile(ID);
+ Length = Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) - Offset;
}
- unsigned Offset = Sources.getFileOffset(Start);
----------------
owenca wrote:
> So basically `Start` was computed based on `Offset[i]` and then recalculated to `Offset` which has the same value?
Yes.
> Or are there corner cases, where this yields a different value?
I don't think so.
https://github.com/llvm/llvm-project/pull/143236
More information about the cfe-commits
mailing list