-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Reland "[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp" #143477
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFull diff: https://github.com/llvm/llvm-project/pull/143477.diff 1 Files Affected:
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index b22d3aaf3183b..d0778ace8ccb2 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,27 @@ static bool fillRanges(MemoryBuffer *Code,
if (Offsets.empty())
Offsets.push_back(0);
- if (Offsets.size() != Lengths.size() &&
- !(Offsets.size() == 1 && Lengths.empty())) {
+ 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) {
- 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);
+ if (!EmptyLengths)
+ 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;
|
Reapply llvm#143236 and fix the bug reported in llvm#143236 (comment).
HazardyKnusperkeks
approved these changes
Jun 12, 2025
tomtor
pushed a commit
to tomtor/llvm-project
that referenced
this pull request
Jun 14, 2025
…llvm#143477) Reapply llvm#143236 and fix the bug reported in llvm#143236 (comment).
akuhlens
pushed a commit
to akuhlens/llvm-project
that referenced
this pull request
Jun 24, 2025
…llvm#143477) Reapply llvm#143236 and fix the bug reported in llvm#143236 (comment).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reapply #143236 and fix the bug reported in #143236 (comment).