Revert "[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp (#143236)"
This reverts commit 897ccddcc3. 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.
This commit is contained in:
@@ -244,17 +244,17 @@ static bool fillRanges(MemoryBuffer *Code,
|
|||||||
DiagnosticsEngine Diagnostics(
|
DiagnosticsEngine Diagnostics(
|
||||||
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), DiagOpts);
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), DiagOpts);
|
||||||
SourceManager Sources(Diagnostics, Files);
|
SourceManager Sources(Diagnostics, Files);
|
||||||
const auto ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
|
FileID ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
|
||||||
InMemoryFileSystem.get());
|
InMemoryFileSystem.get());
|
||||||
if (!LineRanges.empty()) {
|
if (!LineRanges.empty()) {
|
||||||
if (!Offsets.empty() || !Lengths.empty()) {
|
if (!Offsets.empty() || !Lengths.empty()) {
|
||||||
errs() << "error: cannot use -lines with -offset/-length\n";
|
errs() << "error: cannot use -lines with -offset/-length\n";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &LineRange : LineRanges) {
|
for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
|
||||||
unsigned FromLine, ToLine;
|
unsigned FromLine, ToLine;
|
||||||
if (parseLineRange(LineRange, FromLine, ToLine)) {
|
if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
|
||||||
errs() << "error: invalid <start line>:<end line> pair\n";
|
errs() << "error: invalid <start line>:<end line> pair\n";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -266,12 +266,12 @@ static bool fillRanges(MemoryBuffer *Code,
|
|||||||
errs() << "error: start line should not exceed end line\n";
|
errs() << "error: start line should not exceed end line\n";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const auto Start = Sources.translateLineCol(ID, FromLine, 1);
|
SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
|
||||||
const auto End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
|
SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
|
||||||
if (Start.isInvalid() || End.isInvalid())
|
if (Start.isInvalid() || End.isInvalid())
|
||||||
return true;
|
return true;
|
||||||
const auto Offset = Sources.getFileOffset(Start);
|
unsigned Offset = Sources.getFileOffset(Start);
|
||||||
const auto Length = Sources.getFileOffset(End) - Offset;
|
unsigned Length = Sources.getFileOffset(End) - Offset;
|
||||||
Ranges.push_back(tooling::Range(Offset, Length));
|
Ranges.push_back(tooling::Range(Offset, Length));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -279,25 +279,32 @@ static bool fillRanges(MemoryBuffer *Code,
|
|||||||
|
|
||||||
if (Offsets.empty())
|
if (Offsets.empty())
|
||||||
Offsets.push_back(0);
|
Offsets.push_back(0);
|
||||||
if (Offsets.size() == 1 && Lengths.empty()) {
|
if (Offsets.size() != Lengths.size() &&
|
||||||
Lengths.push_back(Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) -
|
!(Offsets.size() == 1 && Lengths.empty())) {
|
||||||
Offsets[0]);
|
|
||||||
} else if (Offsets.size() != Lengths.size()) {
|
|
||||||
errs() << "error: number of -offset and -length arguments must match.\n";
|
errs() << "error: number of -offset and -length arguments must match.\n";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (unsigned I = 0, E = Offsets.size(); I < E; ++I) {
|
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
|
||||||
const auto Offset = Offsets[I];
|
if (Offsets[i] >= Code->getBufferSize()) {
|
||||||
if (Offset >= Code->getBufferSize()) {
|
errs() << "error: offset " << Offsets[i] << " is outside the file\n";
|
||||||
errs() << "error: offset " << Offset << " is outside the file\n";
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const auto Length = Lengths[I];
|
SourceLocation Start =
|
||||||
if (Offset + Length > Code->getBufferSize()) {
|
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
|
||||||
errs() << "error: invalid length " << Length << ", offset + length ("
|
SourceLocation End;
|
||||||
<< Offset + Length << ") is outside the file.\n";
|
if (i < Lengths.size()) {
|
||||||
return true;
|
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);
|
||||||
}
|
}
|
||||||
|
unsigned Offset = Sources.getFileOffset(Start);
|
||||||
|
unsigned Length = Sources.getFileOffset(End) - Offset;
|
||||||
Ranges.push_back(tooling::Range(Offset, Length));
|
Ranges.push_back(tooling::Range(Offset, Length));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user