Skip to content

Commit 3c02150

Browse files
committed
Revert "[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp (#143236)"
This reverts commit 897ccdd. 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.
1 parent 28b753b commit 3c02150

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ static bool fillRanges(MemoryBuffer *Code,
244244
DiagnosticsEngine Diagnostics(
245245
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), DiagOpts);
246246
SourceManager Sources(Diagnostics, Files);
247-
const auto ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
248-
InMemoryFileSystem.get());
247+
FileID ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
248+
InMemoryFileSystem.get());
249249
if (!LineRanges.empty()) {
250250
if (!Offsets.empty() || !Lengths.empty()) {
251251
errs() << "error: cannot use -lines with -offset/-length\n";
252252
return true;
253253
}
254254

255-
for (const auto &LineRange : LineRanges) {
255+
for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
256256
unsigned FromLine, ToLine;
257-
if (parseLineRange(LineRange, FromLine, ToLine)) {
257+
if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
258258
errs() << "error: invalid <start line>:<end line> pair\n";
259259
return true;
260260
}
@@ -266,38 +266,45 @@ static bool fillRanges(MemoryBuffer *Code,
266266
errs() << "error: start line should not exceed end line\n";
267267
return true;
268268
}
269-
const auto Start = Sources.translateLineCol(ID, FromLine, 1);
270-
const auto End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
269+
SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
270+
SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
271271
if (Start.isInvalid() || End.isInvalid())
272272
return true;
273-
const auto Offset = Sources.getFileOffset(Start);
274-
const auto Length = Sources.getFileOffset(End) - Offset;
273+
unsigned Offset = Sources.getFileOffset(Start);
274+
unsigned Length = Sources.getFileOffset(End) - Offset;
275275
Ranges.push_back(tooling::Range(Offset, Length));
276276
}
277277
return false;
278278
}
279279

280280
if (Offsets.empty())
281281
Offsets.push_back(0);
282-
if (Offsets.size() == 1 && Lengths.empty()) {
283-
Lengths.push_back(Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) -
284-
Offsets[0]);
285-
} else if (Offsets.size() != Lengths.size()) {
282+
if (Offsets.size() != Lengths.size() &&
283+
!(Offsets.size() == 1 && Lengths.empty())) {
286284
errs() << "error: number of -offset and -length arguments must match.\n";
287285
return true;
288286
}
289-
for (unsigned I = 0, E = Offsets.size(); I < E; ++I) {
290-
const auto Offset = Offsets[I];
291-
if (Offset >= Code->getBufferSize()) {
292-
errs() << "error: offset " << Offset << " is outside the file\n";
287+
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
288+
if (Offsets[i] >= Code->getBufferSize()) {
289+
errs() << "error: offset " << Offsets[i] << " is outside the file\n";
293290
return true;
294291
}
295-
const auto Length = Lengths[I];
296-
if (Offset + Length > Code->getBufferSize()) {
297-
errs() << "error: invalid length " << Length << ", offset + length ("
298-
<< Offset + Length << ") is outside the file.\n";
299-
return true;
292+
SourceLocation Start =
293+
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
294+
SourceLocation End;
295+
if (i < Lengths.size()) {
296+
if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
297+
errs() << "error: invalid length " << Lengths[i]
298+
<< ", offset + length (" << Offsets[i] + Lengths[i]
299+
<< ") is outside the file.\n";
300+
return true;
301+
}
302+
End = Start.getLocWithOffset(Lengths[i]);
303+
} else {
304+
End = Sources.getLocForEndOfFile(ID);
300305
}
306+
unsigned Offset = Sources.getFileOffset(Start);
307+
unsigned Length = Sources.getFileOffset(End) - Offset;
301308
Ranges.push_back(tooling::Range(Offset, Length));
302309
}
303310
return false;

0 commit comments

Comments
 (0)