https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/143236
>From 1ece3adee540295efbe04fc74876efd39549e577 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 6 Jun 2025 23:10:18 -0700 Subject: [PATCH 1/3] [clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp --- clang/tools/clang-format/ClangFormat.cpp | 43 ++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) 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; >From e9a71f2972824a90ab6da6279599ca4d79376f55 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sat, 7 Jun 2025 00:31:21 -0700 Subject: [PATCH 2/3] More cleanup: - Don't recalculate Offset for -offset. - Calculate Length only if -length is unspecified. --- clang/tools/clang-format/ClangFormat.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index 441ed442a30a9..03a3cbbe2a9f7 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -285,27 +285,22 @@ static bool fillRanges(MemoryBuffer *Code, return true; } for (unsigned I = 0, E = Offsets.size(), Size = Lengths.size(); I < E; ++I) { - auto Offset = Offsets[I]; + const auto Offset = Offsets[I]; if (Offset >= Code->getBufferSize()) { errs() << "error: offset " << Offset << " is outside the file\n"; return true; } - const auto Start = - Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); - SourceLocation End; + unsigned Length; 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"; + 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(L); } else { - End = Sources.getLocForEndOfFile(ID); + Length = Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) - Offset; } - Offset = Sources.getFileOffset(Start); - const auto Length = Sources.getFileOffset(End) - Offset; Ranges.push_back(tooling::Range(Offset, Length)); } return false; >From 3442498079820c7dafcf784535695bcca27a8ff9 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sat, 7 Jun 2025 12:41:24 -0700 Subject: [PATCH 3/3] Still more cleanup: simplify handling of Offsets and Lengths --- clang/tools/clang-format/ClangFormat.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index 03a3cbbe2a9f7..ad12672fa89c1 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -279,27 +279,24 @@ 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(), Size = Lengths.size(); I < E; ++I) { + 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; } - 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; - } - } else { - Length = Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) - Offset; + 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; } Ranges.push_back(tooling::Range(Offset, Length)); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits