Skip to content

Commit dfafb7f

Browse files
committed
[clang][NFC] More range for loops in TextDiagnostic.cpp
1 parent 650d69f commit dfafb7f

File tree

1 file changed

+60
-64
lines changed

1 file changed

+60
-64
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -856,15 +856,14 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
856856
FileID CaretFileID = Loc.getExpansionLoc().getFileID();
857857
bool PrintedRange = false;
858858

859-
for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(),
860-
RE = Ranges.end();
861-
RI != RE; ++RI) {
859+
for (const auto &R : Ranges) {
862860
// Ignore invalid ranges.
863-
if (!RI->isValid()) continue;
861+
if (!R.isValid())
862+
continue;
864863

865864
auto &SM = Loc.getManager();
866-
SourceLocation B = SM.getExpansionLoc(RI->getBegin());
867-
CharSourceRange ERange = SM.getExpansionRange(RI->getEnd());
865+
SourceLocation B = SM.getExpansionLoc(R.getBegin());
866+
CharSourceRange ERange = SM.getExpansionRange(R.getEnd());
868867
SourceLocation E = ERange.getEnd();
869868
bool IsTokenRange = ERange.isTokenRange();
870869

@@ -1068,51 +1067,51 @@ static std::string buildFixItInsertionLine(FileID FID,
10681067
return FixItInsertionLine;
10691068
unsigned PrevHintEndCol = 0;
10701069

1071-
for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
1072-
I != E; ++I) {
1073-
if (!I->CodeToInsert.empty()) {
1074-
// We have an insertion hint. Determine whether the inserted
1075-
// code contains no newlines and is on the same line as the caret.
1076-
std::pair<FileID, unsigned> HintLocInfo
1077-
= SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin());
1078-
if (FID == HintLocInfo.first &&
1079-
LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) &&
1080-
StringRef(I->CodeToInsert).find_first_of("\n\r") == StringRef::npos) {
1081-
// Insert the new code into the line just below the code
1082-
// that the user wrote.
1083-
// Note: When modifying this function, be very careful about what is a
1084-
// "column" (printed width, platform-dependent) and what is a
1085-
// "byte offset" (SourceManager "column").
1086-
unsigned HintByteOffset
1087-
= SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
1088-
1089-
// The hint must start inside the source or right at the end
1090-
assert(HintByteOffset < static_cast<unsigned>(map.bytes())+1);
1091-
unsigned HintCol = map.byteToContainingColumn(HintByteOffset);
1092-
1093-
// If we inserted a long previous hint, push this one forwards, and add
1094-
// an extra space to show that this is not part of the previous
1095-
// completion. This is sort of the best we can do when two hints appear
1096-
// to overlap.
1097-
//
1098-
// Note that if this hint is located immediately after the previous
1099-
// hint, no space will be added, since the location is more important.
1100-
if (HintCol < PrevHintEndCol)
1101-
HintCol = PrevHintEndCol + 1;
1102-
1103-
// This should NOT use HintByteOffset, because the source might have
1104-
// Unicode characters in earlier columns.
1105-
unsigned NewFixItLineSize = FixItInsertionLine.size() +
1106-
(HintCol - PrevHintEndCol) + I->CodeToInsert.size();
1107-
if (NewFixItLineSize > FixItInsertionLine.size())
1108-
FixItInsertionLine.resize(NewFixItLineSize, ' ');
1109-
1110-
std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(),
1111-
FixItInsertionLine.end() - I->CodeToInsert.size());
1112-
1113-
PrevHintEndCol =
1114-
HintCol + llvm::sys::locale::columnWidth(I->CodeToInsert);
1115-
}
1070+
for (const auto &H : Hints) {
1071+
if (H.CodeToInsert.empty())
1072+
continue;
1073+
1074+
// We have an insertion hint. Determine whether the inserted
1075+
// code contains no newlines and is on the same line as the caret.
1076+
std::pair<FileID, unsigned> HintLocInfo =
1077+
SM.getDecomposedExpansionLoc(H.RemoveRange.getBegin());
1078+
if (FID == HintLocInfo.first &&
1079+
LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) &&
1080+
StringRef(H.CodeToInsert).find_first_of("\n\r") == StringRef::npos) {
1081+
// Insert the new code into the line just below the code
1082+
// that the user wrote.
1083+
// Note: When modifying this function, be very careful about what is a
1084+
// "column" (printed width, platform-dependent) and what is a
1085+
// "byte offset" (SourceManager "column").
1086+
unsigned HintByteOffset =
1087+
SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
1088+
1089+
// The hint must start inside the source or right at the end
1090+
assert(HintByteOffset < static_cast<unsigned>(map.bytes()) + 1);
1091+
unsigned HintCol = map.byteToContainingColumn(HintByteOffset);
1092+
1093+
// If we inserted a long previous hint, push this one forwards, and add
1094+
// an extra space to show that this is not part of the previous
1095+
// completion. This is sort of the best we can do when two hints appear
1096+
// to overlap.
1097+
//
1098+
// Note that if this hint is located immediately after the previous
1099+
// hint, no space will be added, since the location is more important.
1100+
if (HintCol < PrevHintEndCol)
1101+
HintCol = PrevHintEndCol + 1;
1102+
1103+
// This should NOT use HintByteOffset, because the source might have
1104+
// Unicode characters in earlier columns.
1105+
unsigned NewFixItLineSize = FixItInsertionLine.size() +
1106+
(HintCol - PrevHintEndCol) +
1107+
H.CodeToInsert.size();
1108+
if (NewFixItLineSize > FixItInsertionLine.size())
1109+
FixItInsertionLine.resize(NewFixItLineSize, ' ');
1110+
1111+
std::copy(H.CodeToInsert.begin(), H.CodeToInsert.end(),
1112+
FixItInsertionLine.end() - H.CodeToInsert.size());
1113+
1114+
PrevHintEndCol = HintCol + llvm::sys::locale::columnWidth(H.CodeToInsert);
11161115
}
11171116
}
11181117

@@ -1168,7 +1167,7 @@ void TextDiagnostic::emitSnippetAndCaret(
11681167
// Find the set of lines to include.
11691168
const unsigned MaxLines = DiagOpts->SnippetLineLimit;
11701169
std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
1171-
for (auto &I : Ranges) {
1170+
for (const auto &I : Ranges) {
11721171
if (auto OptionalRange = findLinesForRange(I, FID, SM))
11731172
Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);
11741173
}
@@ -1211,7 +1210,7 @@ void TextDiagnostic::emitSnippetAndCaret(
12111210
std::string CaretLine(sourceColMap.columns(), ' ');
12121211

12131212
// Highlight all of the characters covered by Ranges with ~ characters.
1214-
for (auto &I : Ranges)
1213+
for (const auto &I : Ranges)
12151214
highlightRange(I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts);
12161215

12171216
// Next, insert the caret itself.
@@ -1315,24 +1314,21 @@ void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints,
13151314

13161315
// We follow FixItRewriter's example in not (yet) handling
13171316
// fix-its in macros.
1318-
for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
1319-
I != E; ++I) {
1320-
if (I->RemoveRange.isInvalid() ||
1321-
I->RemoveRange.getBegin().isMacroID() ||
1322-
I->RemoveRange.getEnd().isMacroID())
1317+
for (const auto &H : Hints) {
1318+
if (H.RemoveRange.isInvalid() || H.RemoveRange.getBegin().isMacroID() ||
1319+
H.RemoveRange.getEnd().isMacroID())
13231320
return;
13241321
}
13251322

1326-
for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
1327-
I != E; ++I) {
1328-
SourceLocation BLoc = I->RemoveRange.getBegin();
1329-
SourceLocation ELoc = I->RemoveRange.getEnd();
1323+
for (const auto &H : Hints) {
1324+
SourceLocation BLoc = H.RemoveRange.getBegin();
1325+
SourceLocation ELoc = H.RemoveRange.getEnd();
13301326

13311327
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
13321328
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
13331329

13341330
// Adjust for token ranges.
1335-
if (I->RemoveRange.isTokenRange())
1331+
if (H.RemoveRange.isTokenRange())
13361332
EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
13371333

13381334
// We specifically do not do word-wrapping or tab-expansion here,
@@ -1348,7 +1344,7 @@ void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints,
13481344
<< '-' << SM.getLineNumber(EInfo.first, EInfo.second)
13491345
<< ':' << SM.getColumnNumber(EInfo.first, EInfo.second)
13501346
<< "}:\"";
1351-
OS.write_escaped(I->CodeToInsert);
1347+
OS.write_escaped(H.CodeToInsert);
13521348
OS << "\"\n";
13531349
}
13541350
}

0 commit comments

Comments
 (0)