[clang][Diagnostics] Use llvm::raw_ostream::indent()
authorTimm Bäder <tbaeder@redhat.com>
Mon, 22 May 2023 11:06:56 +0000 (13:06 +0200)
committerTimm Bäder <tbaeder@redhat.com>
Wed, 31 May 2023 07:40:24 +0000 (09:40 +0200)
Differential Revision: https://reviews.llvm.org/D151078

clang/lib/Frontend/TextDiagnostic.cpp

index 9b11294..930033a 100644 (file)
@@ -615,9 +615,6 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str,
   const unsigned Length = std::min(Str.find('\n'), Str.size());
   bool TextNormal = true;
 
-  // The string used to indent each line.
-  SmallString<16> IndentStr;
-  IndentStr.assign(Indentation, ' ');
   bool Wrapped = false;
   for (unsigned WordStart = 0, WordEnd; WordStart < Length;
        WordStart = WordEnd) {
@@ -646,7 +643,7 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str,
     // This word does not fit on the current line, so wrap to the next
     // line.
     OS << '\n';
-    OS.write(&IndentStr[0], Indentation);
+    OS.indent(Indentation);
     applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
                               TextNormal, Bold);
     Column = Indentation + WordLength;
@@ -1188,12 +1185,8 @@ void TextDiagnostic::emitSnippetAndCaret(
           ? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines))
           : 0;
   auto indentForLineNumbers = [&] {
-    if (MaxLineNoDisplayWidth > 0) {
-      OS << ' ';
-      for (unsigned I = 0; I != MaxLineNoDisplayWidth; ++I)
-        OS << ' ';
-      OS << " | ";
-    }
+    if (MaxLineNoDisplayWidth > 0)
+      OS.indent(MaxLineNoDisplayWidth + 2) << "| ";
   };
 
   for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1;
@@ -1307,11 +1300,8 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
   // Emit line number.
   if (MaxLineNoDisplayWidth > 0) {
     unsigned LineNoDisplayWidth = getNumDisplayWidth(LineNo);
-    OS << ' ';
-    for (unsigned I = LineNoDisplayWidth; I < MaxLineNoDisplayWidth; ++I)
-      OS << ' ';
-    OS << LineNo;
-    OS << " | ";
+    OS.indent(MaxLineNoDisplayWidth - LineNoDisplayWidth + 1)
+        << LineNo << " | ";
   }
 
   // Print the source line one character at a time.