Index: clang/lib/Frontend/TextDiagnostic.cpp =================================================================== --- clang/lib/Frontend/TextDiagnostic.cpp +++ clang/lib/Frontend/TextDiagnostic.cpp @@ -1217,10 +1217,7 @@ // 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 &LR : LineRanges) { if (LR.LineNo == LineNo) @@ -1229,10 +1226,9 @@ // 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( @@ -1254,10 +1250,6 @@ 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);