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

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


tbaeder updated this revision to Diff 524216.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151075/new/

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,30 +1322,27 @@
     OS << " | ";
   }
 
+  // Print the source line one character at a time.
   bool PrintReversed = false;
-  std::string ToPrint;
   size_t I = 0;
   while (I < SourceLine.size()) {
     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.524216.patch
Type: text/x-patch
Size: 1319 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230522/cc72a93f/attachment.bin>


More information about the cfe-commits mailing list