[PATCH] D151286: [clang][Diagnostics][NFC] Don't create oversized CaretLine

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 23 21:49:27 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: clang, cjdb, aaron.ballman.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

  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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151286

Files:
  clang/lib/Frontend/TextDiagnostic.cpp


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);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151286.524996.patch
Type: text/x-patch
Size: 1608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230524/b0c68ff4/attachment-0001.bin>


More information about the cfe-commits mailing list