Skip to content

Commit 1ece3ad

Browse files
committed
[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp
1 parent 0c495ce commit 1ece3ad

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 22 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-
FileID ID = createInMemoryFile("<irrelevant>", *Code, Sources, Files,
248-
InMemoryFileSystem.get());
247+
const auto 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 (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
255+
for (const auto &LineRange : LineRanges) {
256256
unsigned FromLine, ToLine;
257-
if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
257+
if (parseLineRange(LineRange, FromLine, ToLine)) {
258258
errs() << "error: invalid <start line>:<end line> pair\n";
259259
return true;
260260
}
@@ -266,12 +266,12 @@ static bool fillRanges(MemoryBuffer *Code,
266266
errs() << "error: start line should not exceed end line\n";
267267
return true;
268268
}
269-
SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
270-
SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
269+
const auto Start = Sources.translateLineCol(ID, FromLine, 1);
270+
const auto End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
271271
if (Start.isInvalid() || End.isInvalid())
272272
return true;
273-
unsigned Offset = Sources.getFileOffset(Start);
274-
unsigned Length = Sources.getFileOffset(End) - Offset;
273+
const auto Offset = Sources.getFileOffset(Start);
274+
const auto Length = Sources.getFileOffset(End) - Offset;
275275
Ranges.push_back(tooling::Range(Offset, Length));
276276
}
277277
return false;
@@ -284,27 +284,28 @@ static bool fillRanges(MemoryBuffer *Code,
284284
errs() << "error: number of -offset and -length arguments must match.\n";
285285
return true;
286286
}
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";
287+
for (unsigned I = 0, E = Offsets.size(), Size = Lengths.size(); I < E; ++I) {
288+
auto Offset = Offsets[I];
289+
if (Offset >= Code->getBufferSize()) {
290+
errs() << "error: offset " << Offset << " is outside the file\n";
290291
return true;
291292
}
292-
SourceLocation Start =
293-
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
293+
const auto Start =
294+
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
294295
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";
296+
if (I < Size) {
297+
const auto L = Lengths[I];
298+
if (Offset + L > Code->getBufferSize()) {
299+
errs() << "error: invalid length " << L << ", offset + length ("
300+
<< Offset + L << ") is outside the file.\n";
300301
return true;
301302
}
302-
End = Start.getLocWithOffset(Lengths[i]);
303+
End = Start.getLocWithOffset(L);
303304
} else {
304305
End = Sources.getLocForEndOfFile(ID);
305306
}
306-
unsigned Offset = Sources.getFileOffset(Start);
307-
unsigned Length = Sources.getFileOffset(End) - Offset;
307+
Offset = Sources.getFileOffset(Start);
308+
const auto Length = Sources.getFileOffset(End) - Offset;
308309
Ranges.push_back(tooling::Range(Offset, Length));
309310
}
310311
return false;

0 commit comments

Comments
 (0)