[clang] 0157815 - [clang][Diagnostics][NFC] Don't create oversized CaretLine
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Wed May 31 01:21:48 PDT 2023
Author: Timm Bäder
Date: 2023-05-31T10:21:24+02:00
New Revision: 01578153ee8228562d0f64d3847e7fc91573da36
URL: https://github.com/llvm/llvm-project/commit/01578153ee8228562d0f64d3847e7fc91573da36
DIFF: https://github.com/llvm/llvm-project/commit/01578153ee8228562d0f64d3847e7fc91573da36.diff
LOG: [clang][Diagnostics][NFC] Don't create oversized CaretLine
Instead of creating a CaretLine the size of the SourceLine, just leave
it empty at first, let HighlightRange resize it to fit all the ~, then
resize it to fit the ^. Then we can save ourselves the work to remove
the trailing whitespace again.
Differential Revision: https://reviews.llvm.org/D151286
Added:
Modified:
clang/lib/Frontend/TextDiagnostic.cpp
Removed:
################################################################################
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index 74c555e30689..d285b8873977 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1199,20 +1199,16 @@ void TextDiagnostic::emitSnippetAndCaret(
// Build the byte to column map.
const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop);
- // Create a line for the caret that is filled with spaces that is the same
- // number of columns as the line of source code.
- std::string CaretLine(sourceColMap.columns(), ' ');
-
+ std::string CaretLine;
// Highlight all of the characters covered by Ranges with ~ characters.
for (const auto &I : Ranges)
highlightRange(I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts);
// Next, insert the caret itself.
if (CaretLineNo == LineNo) {
- CaretColNo = sourceColMap.byteToContainingColumn(CaretColNo - 1);
- if (CaretLine.size() < CaretColNo + 1)
- CaretLine.resize(CaretColNo + 1, ' ');
- CaretLine[CaretColNo] = '^';
+ size_t Col = sourceColMap.byteToContainingColumn(CaretColNo - 1);
+ CaretLine.resize(std::max(Col + 1, CaretLine.size()), ' ');
+ CaretLine[Col] = '^';
}
std::string FixItInsertionLine = buildFixItInsertionLine(
@@ -1234,10 +1230,6 @@ void TextDiagnostic::emitSnippetAndCaret(
CaretLine = ' ' + CaretLine;
}
- // Finally, remove any blank spaces from the end of CaretLine.
- while (!CaretLine.empty() && CaretLine[CaretLine.size() - 1] == ' ')
- CaretLine.erase(CaretLine.end() - 1);
-
// Emit what we have computed.
emitSnippet(SourceLine, MaxLineNoDisplayWidth, DisplayLineNo);
More information about the cfe-commits
mailing list