[clang] 49843c5 - [clang][Diagnostics] Simplify emitSnippet()

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed May 31 00:41:02 PDT 2023


Author: Timm Bäder
Date: 2023-05-31T09:40:24+02:00
New Revision: 49843c5036847a8e2f83852c8a515c2784a1645e

URL: https://github.com/llvm/llvm-project/commit/49843c5036847a8e2f83852c8a515c2784a1645e
DIFF: https://github.com/llvm/llvm-project/commit/49843c5036847a8e2f83852c8a515c2784a1645e.diff

LOG: [clang][Diagnostics] Simplify emitSnippet()

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.

Differential Revision: https://reviews.llvm.org/D151075

Added: 
    

Modified: 
    clang/lib/Frontend/TextDiagnostic.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index 51b901180ee5..9b11294224ed 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1314,30 +1314,27 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
     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';


        


More information about the cfe-commits mailing list