[PATCH] D151075: [clang][Diagnostics] Simplify emitSnippet()

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 22 03:32:47 PDT 2023


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

  Don't try to minimize the times we invoke operator<< on the output
  stream by keeping a ToPrint string around. Instead, just print the
  characters as we iterate over them.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151075

Files:
  clang/lib/Frontend/TextDiagnostic.cpp


Index: clang/lib/Frontend/TextDiagnostic.cpp
===================================================================
--- clang/lib/Frontend/TextDiagnostic.cpp
+++ clang/lib/Frontend/TextDiagnostic.cpp
@@ -1322,6 +1322,7 @@
     OS << " | ";
   }
 
+  // Print the source line one character at a time.
   bool PrintReversed = false;
   std::string ToPrint;
   size_t I = 0;
@@ -1329,23 +1330,20 @@
     auto [Str, WasPrintable] =
         printableTextForNextCharacter(SourceLine, &I, DiagOpts->TabStop);
 
-    if (DiagOpts->ShowColors && WasPrintable == PrintReversed) {
-      if (PrintReversed)
-        OS.reverseColor();
-      OS << ToPrint;
-      ToPrint.clear();
-      if (DiagOpts->ShowColors)
-        OS.resetColor();
+    // Toggle inverted colors on or off for this character.
+    if (DiagOpts->ShowColors) {
+      if (WasPrintable == PrintReversed) {
+        PrintReversed = !PrintReversed;
+        if (PrintReversed)
+          OS.reverseColor();
+        else
+          OS.resetColor();
+      }
     }
-
-    PrintReversed = !WasPrintable;
-    ToPrint += Str;
+    OS << Str;
   }
 
-  if (PrintReversed && DiagOpts->ShowColors)
-    OS.reverseColor();
-  OS << ToPrint;
-  if (PrintReversed && DiagOpts->ShowColors)
+  if (DiagOpts->ShowColors)
     OS.resetColor();
 
   OS << '\n';


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151075.524215.patch
Type: text/x-patch
Size: 1306 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230522/779b76a2/attachment.bin>


More information about the cfe-commits mailing list