xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/TextDiagnostic.cpp (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 //===--- TextDiagnostic.cpp - Text Diagnostic Pretty-Printing -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Frontend/TextDiagnostic.h"
10 #include "clang/Basic/CharInfo.h"
11 #include "clang/Basic/DiagnosticOptions.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Lex/Lexer.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/ConvertUTF.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/Locale.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <algorithm>
23 #include <optional>
24 
25 using namespace clang;
26 
27 static const enum raw_ostream::Colors noteColor = raw_ostream::CYAN;
28 static const enum raw_ostream::Colors remarkColor =
29   raw_ostream::BLUE;
30 static const enum raw_ostream::Colors fixitColor =
31   raw_ostream::GREEN;
32 static const enum raw_ostream::Colors caretColor =
33   raw_ostream::GREEN;
34 static const enum raw_ostream::Colors warningColor =
35   raw_ostream::MAGENTA;
36 static const enum raw_ostream::Colors templateColor =
37   raw_ostream::CYAN;
38 static const enum raw_ostream::Colors errorColor = raw_ostream::RED;
39 static const enum raw_ostream::Colors fatalColor = raw_ostream::RED;
40 // Used for changing only the bold attribute.
41 static const enum raw_ostream::Colors savedColor =
42   raw_ostream::SAVEDCOLOR;
43 
44 /// Add highlights to differences in template strings.
45 static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
46                                       bool &Normal, bool Bold) {
47   while (true) {
48     size_t Pos = Str.find(ToggleHighlight);
49     OS << Str.slice(0, Pos);
50     if (Pos == StringRef::npos)
51       break;
52 
53     Str = Str.substr(Pos + 1);
54     if (Normal)
55       OS.changeColor(templateColor, true);
56     else {
57       OS.resetColor();
58       if (Bold)
59         OS.changeColor(savedColor, true);
60     }
61     Normal = !Normal;
62   }
63 }
64 
65 /// Number of spaces to indent when word-wrapping.
66 const unsigned WordWrapIndentation = 6;
67 
68 static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) {
69   int bytes = 0;
70   while (0<i) {
71     if (SourceLine[--i]=='\t')
72       break;
73     ++bytes;
74   }
75   return bytes;
76 }
77 
78 /// returns a printable representation of first item from input range
79 ///
80 /// This function returns a printable representation of the next item in a line
81 ///  of source. If the next byte begins a valid and printable character, that
82 ///  character is returned along with 'true'.
83 ///
84 /// Otherwise, if the next byte begins a valid, but unprintable character, a
85 ///  printable, escaped representation of the character is returned, along with
86 ///  'false'. Otherwise a printable, escaped representation of the next byte
87 ///  is returned along with 'false'.
88 ///
89 /// \note The index is updated to be used with a subsequent call to
90 ///        printableTextForNextCharacter.
91 ///
92 /// \param SourceLine The line of source
93 /// \param I Pointer to byte index,
94 /// \param TabStop used to expand tabs
95 /// \return pair(printable text, 'true' iff original text was printable)
96 ///
97 static std::pair<SmallString<16>, bool>
98 printableTextForNextCharacter(StringRef SourceLine, size_t *I,
99                               unsigned TabStop) {
100   assert(I && "I must not be null");
101   assert(*I < SourceLine.size() && "must point to a valid index");
102 
103   if (SourceLine[*I] == '\t') {
104     assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
105            "Invalid -ftabstop value");
106     unsigned Col = bytesSincePreviousTabOrLineBegin(SourceLine, *I);
107     unsigned NumSpaces = TabStop - (Col % TabStop);
108     assert(0 < NumSpaces && NumSpaces <= TabStop
109            && "Invalid computation of space amt");
110     ++(*I);
111 
112     SmallString<16> ExpandedTab;
113     ExpandedTab.assign(NumSpaces, ' ');
114     return std::make_pair(ExpandedTab, true);
115   }
116 
117   const unsigned char *Begin = SourceLine.bytes_begin() + *I;
118 
119   // Fast path for the common ASCII case.
120   if (*Begin < 0x80 && llvm::sys::locale::isPrint(*Begin)) {
121     ++(*I);
122     return std::make_pair(SmallString<16>(Begin, Begin + 1), true);
123   }
124   unsigned CharSize = llvm::getNumBytesForUTF8(*Begin);
125   const unsigned char *End = Begin + CharSize;
126 
127   // Convert it to UTF32 and check if it's printable.
128   if (End <= SourceLine.bytes_end() && llvm::isLegalUTF8Sequence(Begin, End)) {
129     llvm::UTF32 C;
130     llvm::UTF32 *CPtr = &C;
131 
132     // Begin and end before conversion.
133     unsigned char const *OriginalBegin = Begin;
134     llvm::ConversionResult Res = llvm::ConvertUTF8toUTF32(
135         &Begin, End, &CPtr, CPtr + 1, llvm::strictConversion);
136     (void)Res;
137     assert(Res == llvm::conversionOK);
138     assert(OriginalBegin < Begin);
139     assert((Begin - OriginalBegin) == CharSize);
140 
141     (*I) += (Begin - OriginalBegin);
142 
143     // Valid, multi-byte, printable UTF8 character.
144     if (llvm::sys::locale::isPrint(C))
145       return std::make_pair(SmallString<16>(OriginalBegin, End), true);
146 
147     // Valid but not printable.
148     SmallString<16> Str("<U+>");
149     while (C) {
150       Str.insert(Str.begin() + 3, llvm::hexdigit(C % 16));
151       C /= 16;
152     }
153     while (Str.size() < 8)
154       Str.insert(Str.begin() + 3, llvm::hexdigit(0));
155     return std::make_pair(Str, false);
156   }
157 
158   // Otherwise, not printable since it's not valid UTF8.
159   SmallString<16> ExpandedByte("<XX>");
160   unsigned char Byte = SourceLine[*I];
161   ExpandedByte[1] = llvm::hexdigit(Byte / 16);
162   ExpandedByte[2] = llvm::hexdigit(Byte % 16);
163   ++(*I);
164   return std::make_pair(ExpandedByte, false);
165 }
166 
167 static void expandTabs(std::string &SourceLine, unsigned TabStop) {
168   size_t I = SourceLine.size();
169   while (I > 0) {
170     I--;
171     if (SourceLine[I] != '\t')
172       continue;
173     size_t TmpI = I;
174     auto [Str, Printable] =
175         printableTextForNextCharacter(SourceLine, &TmpI, TabStop);
176     SourceLine.replace(I, 1, Str.c_str());
177   }
178 }
179 
180 /// \p BytesOut:
181 ///  A mapping from columns to the byte of the source line that produced the
182 ///  character displaying at that column. This is the inverse of \p ColumnsOut.
183 ///
184 /// The last element in the array is the number of bytes in the source string.
185 ///
186 /// example: (given a tabstop of 8)
187 ///
188 ///    "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7}
189 ///
190 ///  (\\u3042 is represented in UTF-8 by three bytes and takes two columns to
191 ///   display)
192 ///
193 /// \p ColumnsOut:
194 ///  A mapping from the bytes
195 ///  of the printable representation of the line to the columns those printable
196 ///  characters will appear at (numbering the first column as 0).
197 ///
198 /// If a byte 'i' corresponds to multiple columns (e.g. the byte contains a tab
199 ///  character) then the array will map that byte to the first column the
200 ///  tab appears at and the next value in the map will have been incremented
201 ///  more than once.
202 ///
203 /// If a byte is the first in a sequence of bytes that together map to a single
204 ///  entity in the output, then the array will map that byte to the appropriate
205 ///  column while the subsequent bytes will be -1.
206 ///
207 /// The last element in the array does not correspond to any byte in the input
208 ///  and instead is the number of columns needed to display the source
209 ///
210 /// example: (given a tabstop of 8)
211 ///
212 ///    "a \t \u3042" -> {0,1,2,8,9,-1,-1,11}
213 ///
214 ///  (\\u3042 is represented in UTF-8 by three bytes and takes two columns to
215 ///   display)
216 static void genColumnByteMapping(StringRef SourceLine, unsigned TabStop,
217                                  SmallVectorImpl<int> &BytesOut,
218                                  SmallVectorImpl<int> &ColumnsOut) {
219   assert(BytesOut.empty());
220   assert(ColumnsOut.empty());
221 
222   if (SourceLine.empty()) {
223     BytesOut.resize(1u, 0);
224     ColumnsOut.resize(1u, 0);
225     return;
226   }
227 
228   ColumnsOut.resize(SourceLine.size() + 1, -1);
229 
230   int Columns = 0;
231   size_t I = 0;
232   while (I < SourceLine.size()) {
233     ColumnsOut[I] = Columns;
234     BytesOut.resize(Columns + 1, -1);
235     BytesOut.back() = I;
236     auto [Str, Printable] =
237         printableTextForNextCharacter(SourceLine, &I, TabStop);
238     Columns += llvm::sys::locale::columnWidth(Str);
239   }
240 
241   ColumnsOut.back() = Columns;
242   BytesOut.resize(Columns + 1, -1);
243   BytesOut.back() = I;
244 }
245 
246 namespace {
247 struct SourceColumnMap {
248   SourceColumnMap(StringRef SourceLine, unsigned TabStop)
249   : m_SourceLine(SourceLine) {
250 
251     genColumnByteMapping(SourceLine, TabStop, m_columnToByte, m_byteToColumn);
252 
253     assert(m_byteToColumn.size()==SourceLine.size()+1);
254     assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size());
255     assert(m_byteToColumn.size()
256            == static_cast<unsigned>(m_columnToByte.back()+1));
257     assert(static_cast<unsigned>(m_byteToColumn.back()+1)
258            == m_columnToByte.size());
259   }
260   int columns() const { return m_byteToColumn.back(); }
261   int bytes() const { return m_columnToByte.back(); }
262 
263   /// Map a byte to the column which it is at the start of, or return -1
264   /// if it is not at the start of a column (for a UTF-8 trailing byte).
265   int byteToColumn(int n) const {
266     assert(0<=n && n<static_cast<int>(m_byteToColumn.size()));
267     return m_byteToColumn[n];
268   }
269 
270   /// Map a byte to the first column which contains it.
271   int byteToContainingColumn(int N) const {
272     assert(0 <= N && N < static_cast<int>(m_byteToColumn.size()));
273     while (m_byteToColumn[N] == -1)
274       --N;
275     return m_byteToColumn[N];
276   }
277 
278   /// Map a column to the byte which starts the column, or return -1 if
279   /// the column the second or subsequent column of an expanded tab or similar
280   /// multi-column entity.
281   int columnToByte(int n) const {
282     assert(0<=n && n<static_cast<int>(m_columnToByte.size()));
283     return m_columnToByte[n];
284   }
285 
286   /// Map from a byte index to the next byte which starts a column.
287   int startOfNextColumn(int N) const {
288     assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1));
289     while (byteToColumn(++N) == -1) {}
290     return N;
291   }
292 
293   /// Map from a byte index to the previous byte which starts a column.
294   int startOfPreviousColumn(int N) const {
295     assert(0 < N && N < static_cast<int>(m_byteToColumn.size()));
296     while (byteToColumn(--N) == -1) {}
297     return N;
298   }
299 
300   StringRef getSourceLine() const {
301     return m_SourceLine;
302   }
303 
304 private:
305   const std::string m_SourceLine;
306   SmallVector<int,200> m_byteToColumn;
307   SmallVector<int,200> m_columnToByte;
308 };
309 } // end anonymous namespace
310 
311 /// When the source code line we want to print is too long for
312 /// the terminal, select the "interesting" region.
313 static void selectInterestingSourceRegion(std::string &SourceLine,
314                                           std::string &CaretLine,
315                                           std::string &FixItInsertionLine,
316                                           unsigned Columns,
317                                           const SourceColumnMap &map) {
318   unsigned CaretColumns = CaretLine.size();
319   unsigned FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine);
320   unsigned MaxColumns = std::max(static_cast<unsigned>(map.columns()),
321                                  std::max(CaretColumns, FixItColumns));
322   // if the number of columns is less than the desired number we're done
323   if (MaxColumns <= Columns)
324     return;
325 
326   // No special characters are allowed in CaretLine.
327   assert(llvm::none_of(CaretLine, [](char c) { return c < ' ' || '~' < c; }));
328 
329   // Find the slice that we need to display the full caret line
330   // correctly.
331   unsigned CaretStart = 0, CaretEnd = CaretLine.size();
332   for (; CaretStart != CaretEnd; ++CaretStart)
333     if (!isWhitespace(CaretLine[CaretStart]))
334       break;
335 
336   for (; CaretEnd != CaretStart; --CaretEnd)
337     if (!isWhitespace(CaretLine[CaretEnd - 1]))
338       break;
339 
340   // caret has already been inserted into CaretLine so the above whitespace
341   // check is guaranteed to include the caret
342 
343   // If we have a fix-it line, make sure the slice includes all of the
344   // fix-it information.
345   if (!FixItInsertionLine.empty()) {
346     unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
347     for (; FixItStart != FixItEnd; ++FixItStart)
348       if (!isWhitespace(FixItInsertionLine[FixItStart]))
349         break;
350 
351     for (; FixItEnd != FixItStart; --FixItEnd)
352       if (!isWhitespace(FixItInsertionLine[FixItEnd - 1]))
353         break;
354 
355     // We can safely use the byte offset FixItStart as the column offset
356     // because the characters up until FixItStart are all ASCII whitespace
357     // characters.
358     unsigned FixItStartCol = FixItStart;
359     unsigned FixItEndCol
360       = llvm::sys::locale::columnWidth(FixItInsertionLine.substr(0, FixItEnd));
361 
362     CaretStart = std::min(FixItStartCol, CaretStart);
363     CaretEnd = std::max(FixItEndCol, CaretEnd);
364   }
365 
366   // CaretEnd may have been set at the middle of a character
367   // If it's not at a character's first column then advance it past the current
368   //   character.
369   while (static_cast<int>(CaretEnd) < map.columns() &&
370          -1 == map.columnToByte(CaretEnd))
371     ++CaretEnd;
372 
373   assert((static_cast<int>(CaretStart) > map.columns() ||
374           -1!=map.columnToByte(CaretStart)) &&
375          "CaretStart must not point to a column in the middle of a source"
376          " line character");
377   assert((static_cast<int>(CaretEnd) > map.columns() ||
378           -1!=map.columnToByte(CaretEnd)) &&
379          "CaretEnd must not point to a column in the middle of a source line"
380          " character");
381 
382   // CaretLine[CaretStart, CaretEnd) contains all of the interesting
383   // parts of the caret line. While this slice is smaller than the
384   // number of columns we have, try to grow the slice to encompass
385   // more context.
386 
387   unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart,
388                                                              map.columns()));
389   unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd,
390                                                            map.columns()));
391 
392   unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart
393     - (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart));
394 
395   char const *front_ellipse = "  ...";
396   char const *front_space   = "     ";
397   char const *back_ellipse = "...";
398   unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse);
399 
400   unsigned TargetColumns = Columns;
401   // Give us extra room for the ellipses
402   //  and any of the caret line that extends past the source
403   if (TargetColumns > ellipses_space+CaretColumnsOutsideSource)
404     TargetColumns -= ellipses_space+CaretColumnsOutsideSource;
405 
406   while (SourceStart>0 || SourceEnd<SourceLine.size()) {
407     bool ExpandedRegion = false;
408 
409     if (SourceStart>0) {
410       unsigned NewStart = map.startOfPreviousColumn(SourceStart);
411 
412       // Skip over any whitespace we see here; we're looking for
413       // another bit of interesting text.
414       // FIXME: Detect non-ASCII whitespace characters too.
415       while (NewStart && isWhitespace(SourceLine[NewStart]))
416         NewStart = map.startOfPreviousColumn(NewStart);
417 
418       // Skip over this bit of "interesting" text.
419       while (NewStart) {
420         unsigned Prev = map.startOfPreviousColumn(NewStart);
421         if (isWhitespace(SourceLine[Prev]))
422           break;
423         NewStart = Prev;
424       }
425 
426       assert(map.byteToColumn(NewStart) != -1);
427       unsigned NewColumns = map.byteToColumn(SourceEnd) -
428                               map.byteToColumn(NewStart);
429       if (NewColumns <= TargetColumns) {
430         SourceStart = NewStart;
431         ExpandedRegion = true;
432       }
433     }
434 
435     if (SourceEnd<SourceLine.size()) {
436       unsigned NewEnd = map.startOfNextColumn(SourceEnd);
437 
438       // Skip over any whitespace we see here; we're looking for
439       // another bit of interesting text.
440       // FIXME: Detect non-ASCII whitespace characters too.
441       while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
442         NewEnd = map.startOfNextColumn(NewEnd);
443 
444       // Skip over this bit of "interesting" text.
445       while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
446         NewEnd = map.startOfNextColumn(NewEnd);
447 
448       assert(map.byteToColumn(NewEnd) != -1);
449       unsigned NewColumns = map.byteToColumn(NewEnd) -
450                               map.byteToColumn(SourceStart);
451       if (NewColumns <= TargetColumns) {
452         SourceEnd = NewEnd;
453         ExpandedRegion = true;
454       }
455     }
456 
457     if (!ExpandedRegion)
458       break;
459   }
460 
461   CaretStart = map.byteToColumn(SourceStart);
462   CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
463 
464   // [CaretStart, CaretEnd) is the slice we want. Update the various
465   // output lines to show only this slice.
466   assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 &&
467          SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1);
468   assert(SourceStart <= SourceEnd);
469   assert(CaretStart <= CaretEnd);
470 
471   unsigned BackColumnsRemoved
472     = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd);
473   unsigned FrontColumnsRemoved = CaretStart;
474   unsigned ColumnsKept = CaretEnd-CaretStart;
475 
476   // We checked up front that the line needed truncation
477   assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns);
478 
479   // The line needs some truncation, and we'd prefer to keep the front
480   //  if possible, so remove the back
481   if (BackColumnsRemoved > strlen(back_ellipse))
482     SourceLine.replace(SourceEnd, std::string::npos, back_ellipse);
483 
484   // If that's enough then we're done
485   if (FrontColumnsRemoved+ColumnsKept <= Columns)
486     return;
487 
488   // Otherwise remove the front as well
489   if (FrontColumnsRemoved > strlen(front_ellipse)) {
490     SourceLine.replace(0, SourceStart, front_ellipse);
491     CaretLine.replace(0, CaretStart, front_space);
492     if (!FixItInsertionLine.empty())
493       FixItInsertionLine.replace(0, CaretStart, front_space);
494   }
495 }
496 
497 /// Skip over whitespace in the string, starting at the given
498 /// index.
499 ///
500 /// \returns The index of the first non-whitespace character that is
501 /// greater than or equal to Idx or, if no such character exists,
502 /// returns the end of the string.
503 static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) {
504   while (Idx < Length && isWhitespace(Str[Idx]))
505     ++Idx;
506   return Idx;
507 }
508 
509 /// If the given character is the start of some kind of
510 /// balanced punctuation (e.g., quotes or parentheses), return the
511 /// character that will terminate the punctuation.
512 ///
513 /// \returns The ending punctuation character, if any, or the NULL
514 /// character if the input character does not start any punctuation.
515 static inline char findMatchingPunctuation(char c) {
516   switch (c) {
517   case '\'': return '\'';
518   case '`': return '\'';
519   case '"':  return '"';
520   case '(':  return ')';
521   case '[': return ']';
522   case '{': return '}';
523   default: break;
524   }
525 
526   return 0;
527 }
528 
529 /// Find the end of the word starting at the given offset
530 /// within a string.
531 ///
532 /// \returns the index pointing one character past the end of the
533 /// word.
534 static unsigned findEndOfWord(unsigned Start, StringRef Str,
535                               unsigned Length, unsigned Column,
536                               unsigned Columns) {
537   assert(Start < Str.size() && "Invalid start position!");
538   unsigned End = Start + 1;
539 
540   // If we are already at the end of the string, take that as the word.
541   if (End == Str.size())
542     return End;
543 
544   // Determine if the start of the string is actually opening
545   // punctuation, e.g., a quote or parentheses.
546   char EndPunct = findMatchingPunctuation(Str[Start]);
547   if (!EndPunct) {
548     // This is a normal word. Just find the first space character.
549     while (End < Length && !isWhitespace(Str[End]))
550       ++End;
551     return End;
552   }
553 
554   // We have the start of a balanced punctuation sequence (quotes,
555   // parentheses, etc.). Determine the full sequence is.
556   SmallString<16> PunctuationEndStack;
557   PunctuationEndStack.push_back(EndPunct);
558   while (End < Length && !PunctuationEndStack.empty()) {
559     if (Str[End] == PunctuationEndStack.back())
560       PunctuationEndStack.pop_back();
561     else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
562       PunctuationEndStack.push_back(SubEndPunct);
563 
564     ++End;
565   }
566 
567   // Find the first space character after the punctuation ended.
568   while (End < Length && !isWhitespace(Str[End]))
569     ++End;
570 
571   unsigned PunctWordLength = End - Start;
572   if (// If the word fits on this line
573       Column + PunctWordLength <= Columns ||
574       // ... or the word is "short enough" to take up the next line
575       // without too much ugly white space
576       PunctWordLength < Columns/3)
577     return End; // Take the whole thing as a single "word".
578 
579   // The whole quoted/parenthesized string is too long to print as a
580   // single "word". Instead, find the "word" that starts just after
581   // the punctuation and use that end-point instead. This will recurse
582   // until it finds something small enough to consider a word.
583   return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
584 }
585 
586 /// Print the given string to a stream, word-wrapping it to
587 /// some number of columns in the process.
588 ///
589 /// \param OS the stream to which the word-wrapping string will be
590 /// emitted.
591 /// \param Str the string to word-wrap and output.
592 /// \param Columns the number of columns to word-wrap to.
593 /// \param Column the column number at which the first character of \p
594 /// Str will be printed. This will be non-zero when part of the first
595 /// line has already been printed.
596 /// \param Bold if the current text should be bold
597 /// \returns true if word-wrapping was required, or false if the
598 /// string fit on the first line.
599 static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns,
600                              unsigned Column, bool Bold) {
601   const unsigned Length = std::min(Str.find('\n'), Str.size());
602   bool TextNormal = true;
603 
604   bool Wrapped = false;
605   for (unsigned WordStart = 0, WordEnd; WordStart < Length;
606        WordStart = WordEnd) {
607     // Find the beginning of the next word.
608     WordStart = skipWhitespace(WordStart, Str, Length);
609     if (WordStart == Length)
610       break;
611 
612     // Find the end of this word.
613     WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
614 
615     // Does this word fit on the current line?
616     unsigned WordLength = WordEnd - WordStart;
617     if (Column + WordLength < Columns) {
618       // This word fits on the current line; print it there.
619       if (WordStart) {
620         OS << ' ';
621         Column += 1;
622       }
623       applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
624                                 TextNormal, Bold);
625       Column += WordLength;
626       continue;
627     }
628 
629     // This word does not fit on the current line, so wrap to the next
630     // line.
631     OS << '\n';
632     OS.indent(WordWrapIndentation);
633     applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
634                               TextNormal, Bold);
635     Column = WordWrapIndentation + WordLength;
636     Wrapped = true;
637   }
638 
639   // Append any remaning text from the message with its existing formatting.
640   applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold);
641 
642   assert(TextNormal && "Text highlighted at end of diagnostic message.");
643 
644   return Wrapped;
645 }
646 
647 TextDiagnostic::TextDiagnostic(raw_ostream &OS,
648                                const LangOptions &LangOpts,
649                                DiagnosticOptions *DiagOpts)
650   : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {}
651 
652 TextDiagnostic::~TextDiagnostic() {}
653 
654 void TextDiagnostic::emitDiagnosticMessage(
655     FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
656     StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
657     DiagOrStoredDiag D) {
658   uint64_t StartOfLocationInfo = OS.tell();
659 
660   // Emit the location of this particular diagnostic.
661   if (Loc.isValid())
662     emitDiagnosticLoc(Loc, PLoc, Level, Ranges);
663 
664   if (DiagOpts->ShowColors)
665     OS.resetColor();
666 
667   if (DiagOpts->ShowLevel)
668     printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
669   printDiagnosticMessage(OS,
670                          /*IsSupplemental*/ Level == DiagnosticsEngine::Note,
671                          Message, OS.tell() - StartOfLocationInfo,
672                          DiagOpts->MessageLength, DiagOpts->ShowColors);
673 }
674 
675 /*static*/ void
676 TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
677                                      DiagnosticsEngine::Level Level,
678                                      bool ShowColors) {
679   if (ShowColors) {
680     // Print diagnostic category in bold and color
681     switch (Level) {
682     case DiagnosticsEngine::Ignored:
683       llvm_unreachable("Invalid diagnostic type");
684     case DiagnosticsEngine::Note:    OS.changeColor(noteColor, true); break;
685     case DiagnosticsEngine::Remark:  OS.changeColor(remarkColor, true); break;
686     case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break;
687     case DiagnosticsEngine::Error:   OS.changeColor(errorColor, true); break;
688     case DiagnosticsEngine::Fatal:   OS.changeColor(fatalColor, true); break;
689     }
690   }
691 
692   switch (Level) {
693   case DiagnosticsEngine::Ignored:
694     llvm_unreachable("Invalid diagnostic type");
695   case DiagnosticsEngine::Note:    OS << "note: "; break;
696   case DiagnosticsEngine::Remark:  OS << "remark: "; break;
697   case DiagnosticsEngine::Warning: OS << "warning: "; break;
698   case DiagnosticsEngine::Error:   OS << "error: "; break;
699   case DiagnosticsEngine::Fatal:   OS << "fatal error: "; break;
700   }
701 
702   if (ShowColors)
703     OS.resetColor();
704 }
705 
706 /*static*/
707 void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
708                                             bool IsSupplemental,
709                                             StringRef Message,
710                                             unsigned CurrentColumn,
711                                             unsigned Columns, bool ShowColors) {
712   bool Bold = false;
713   if (ShowColors && !IsSupplemental) {
714     // Print primary diagnostic messages in bold and without color, to visually
715     // indicate the transition from continuation notes and other output.
716     OS.changeColor(savedColor, true);
717     Bold = true;
718   }
719 
720   if (Columns)
721     printWordWrapped(OS, Message, Columns, CurrentColumn, Bold);
722   else {
723     bool Normal = true;
724     applyTemplateHighlighting(OS, Message, Normal, Bold);
725     assert(Normal && "Formatting should have returned to normal");
726   }
727 
728   if (ShowColors)
729     OS.resetColor();
730   OS << '\n';
731 }
732 
733 void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) {
734 #ifdef _WIN32
735   SmallString<4096> TmpFilename;
736 #endif
737   if (DiagOpts->AbsolutePath) {
738     auto File = SM.getFileManager().getOptionalFileRef(Filename);
739     if (File) {
740       // We want to print a simplified absolute path, i. e. without "dots".
741       //
742       // The hardest part here are the paths like "<part1>/<link>/../<part2>".
743       // On Unix-like systems, we cannot just collapse "<link>/..", because
744       // paths are resolved sequentially, and, thereby, the path
745       // "<part1>/<part2>" may point to a different location. That is why
746       // we use FileManager::getCanonicalName(), which expands all indirections
747       // with llvm::sys::fs::real_path() and caches the result.
748       //
749       // On the other hand, it would be better to preserve as much of the
750       // original path as possible, because that helps a user to recognize it.
751       // real_path() expands all links, which sometimes too much. Luckily,
752       // on Windows we can just use llvm::sys::path::remove_dots(), because,
753       // on that system, both aforementioned paths point to the same place.
754 #ifdef _WIN32
755       TmpFilename = File->getName();
756       llvm::sys::fs::make_absolute(TmpFilename);
757       llvm::sys::path::native(TmpFilename);
758       llvm::sys::path::remove_dots(TmpFilename, /* remove_dot_dot */ true);
759       Filename = StringRef(TmpFilename.data(), TmpFilename.size());
760 #else
761       Filename = SM.getFileManager().getCanonicalName(*File);
762 #endif
763     }
764   }
765 
766   OS << Filename;
767 }
768 
769 /// Print out the file/line/column information and include trace.
770 ///
771 /// This method handles the emission of the diagnostic location information.
772 /// This includes extracting as much location information as is present for
773 /// the diagnostic and printing it, as well as any include stack or source
774 /// ranges necessary.
775 void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
776                                        DiagnosticsEngine::Level Level,
777                                        ArrayRef<CharSourceRange> Ranges) {
778   if (PLoc.isInvalid()) {
779     // At least print the file name if available:
780     if (FileID FID = Loc.getFileID(); FID.isValid()) {
781       if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
782         emitFilename(FE->getName(), Loc.getManager());
783         OS << ": ";
784       }
785     }
786     return;
787   }
788   unsigned LineNo = PLoc.getLine();
789 
790   if (!DiagOpts->ShowLocation)
791     return;
792 
793   if (DiagOpts->ShowColors)
794     OS.changeColor(savedColor, true);
795 
796   emitFilename(PLoc.getFilename(), Loc.getManager());
797   switch (DiagOpts->getFormat()) {
798   case DiagnosticOptions::SARIF:
799   case DiagnosticOptions::Clang:
800     if (DiagOpts->ShowLine)
801       OS << ':' << LineNo;
802     break;
803   case DiagnosticOptions::MSVC:  OS << '('  << LineNo; break;
804   case DiagnosticOptions::Vi:    OS << " +" << LineNo; break;
805   }
806 
807   if (DiagOpts->ShowColumn)
808     // Compute the column number.
809     if (unsigned ColNo = PLoc.getColumn()) {
810       if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) {
811         OS << ',';
812         // Visual Studio 2010 or earlier expects column number to be off by one
813         if (LangOpts.MSCompatibilityVersion &&
814             !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
815           ColNo--;
816       } else
817         OS << ':';
818       OS << ColNo;
819     }
820   switch (DiagOpts->getFormat()) {
821   case DiagnosticOptions::SARIF:
822   case DiagnosticOptions::Clang:
823   case DiagnosticOptions::Vi:    OS << ':';    break;
824   case DiagnosticOptions::MSVC:
825     // MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the
826     // space and prints 'file(4): error'.
827     OS << ')';
828     if (LangOpts.MSCompatibilityVersion &&
829         !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
830       OS << ' ';
831     OS << ':';
832     break;
833   }
834 
835   if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
836     FileID CaretFileID = Loc.getExpansionLoc().getFileID();
837     bool PrintedRange = false;
838     const SourceManager &SM = Loc.getManager();
839 
840     for (const auto &R : Ranges) {
841       // Ignore invalid ranges.
842       if (!R.isValid())
843         continue;
844 
845       SourceLocation B = SM.getExpansionLoc(R.getBegin());
846       CharSourceRange ERange = SM.getExpansionRange(R.getEnd());
847       SourceLocation E = ERange.getEnd();
848 
849       // If the start or end of the range is in another file, just
850       // discard it.
851       if (SM.getFileID(B) != CaretFileID || SM.getFileID(E) != CaretFileID)
852         continue;
853 
854       // Add in the length of the token, so that we cover multi-char
855       // tokens.
856       unsigned TokSize = 0;
857       if (ERange.isTokenRange())
858         TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
859 
860       FullSourceLoc BF(B, SM), EF(E, SM);
861       OS << '{'
862          << BF.getLineNumber() << ':' << BF.getColumnNumber() << '-'
863          << EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize)
864          << '}';
865       PrintedRange = true;
866     }
867 
868     if (PrintedRange)
869       OS << ':';
870   }
871   OS << ' ';
872 }
873 
874 void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
875   if (DiagOpts->ShowLocation && PLoc.isValid()) {
876     OS << "In file included from ";
877     emitFilename(PLoc.getFilename(), Loc.getManager());
878     OS << ':' << PLoc.getLine() << ":\n";
879   } else
880     OS << "In included file:\n";
881 }
882 
883 void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
884                                         StringRef ModuleName) {
885   if (DiagOpts->ShowLocation && PLoc.isValid())
886     OS << "In module '" << ModuleName << "' imported from "
887        << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
888   else
889     OS << "In module '" << ModuleName << "':\n";
890 }
891 
892 void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc,
893                                                 PresumedLoc PLoc,
894                                                 StringRef ModuleName) {
895   if (DiagOpts->ShowLocation && PLoc.isValid())
896     OS << "While building module '" << ModuleName << "' imported from "
897       << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
898   else
899     OS << "While building module '" << ModuleName << "':\n";
900 }
901 
902 /// Find the suitable set of lines to show to include a set of ranges.
903 static std::optional<std::pair<unsigned, unsigned>>
904 findLinesForRange(const CharSourceRange &R, FileID FID,
905                   const SourceManager &SM) {
906   if (!R.isValid())
907     return std::nullopt;
908 
909   SourceLocation Begin = R.getBegin();
910   SourceLocation End = R.getEnd();
911   if (SM.getFileID(Begin) != FID || SM.getFileID(End) != FID)
912     return std::nullopt;
913 
914   return std::make_pair(SM.getExpansionLineNumber(Begin),
915                         SM.getExpansionLineNumber(End));
916 }
917 
918 /// Add as much of range B into range A as possible without exceeding a maximum
919 /// size of MaxRange. Ranges are inclusive.
920 static std::pair<unsigned, unsigned>
921 maybeAddRange(std::pair<unsigned, unsigned> A, std::pair<unsigned, unsigned> B,
922               unsigned MaxRange) {
923   // If A is already the maximum size, we're done.
924   unsigned Slack = MaxRange - (A.second - A.first + 1);
925   if (Slack == 0)
926     return A;
927 
928   // Easy case: merge succeeds within MaxRange.
929   unsigned Min = std::min(A.first, B.first);
930   unsigned Max = std::max(A.second, B.second);
931   if (Max - Min + 1 <= MaxRange)
932     return {Min, Max};
933 
934   // If we can't reach B from A within MaxRange, there's nothing to do.
935   // Don't add lines to the range that contain nothing interesting.
936   if ((B.first > A.first && B.first - A.first + 1 > MaxRange) ||
937       (B.second < A.second && A.second - B.second + 1 > MaxRange))
938     return A;
939 
940   // Otherwise, expand A towards B to produce a range of size MaxRange. We
941   // attempt to expand by the same amount in both directions if B strictly
942   // contains A.
943 
944   // Expand downwards by up to half the available amount, then upwards as
945   // much as possible, then downwards as much as possible.
946   A.second = std::min(A.second + (Slack + 1) / 2, Max);
947   Slack = MaxRange - (A.second - A.first + 1);
948   A.first = std::max(Min + Slack, A.first) - Slack;
949   A.second = std::min(A.first + MaxRange - 1, Max);
950   return A;
951 }
952 
953 struct LineRange {
954   unsigned LineNo;
955   unsigned StartCol;
956   unsigned EndCol;
957 };
958 
959 /// Highlight \p R (with ~'s) on the current source line.
960 static void highlightRange(const LineRange &R, const SourceColumnMap &Map,
961                            std::string &CaretLine) {
962   // Pick the first non-whitespace column.
963   unsigned StartColNo = R.StartCol;
964   while (StartColNo < Map.getSourceLine().size() &&
965          (Map.getSourceLine()[StartColNo] == ' ' ||
966           Map.getSourceLine()[StartColNo] == '\t'))
967     StartColNo = Map.startOfNextColumn(StartColNo);
968 
969   // Pick the last non-whitespace column.
970   unsigned EndColNo =
971       std::min(static_cast<size_t>(R.EndCol), Map.getSourceLine().size());
972   while (EndColNo && (Map.getSourceLine()[EndColNo - 1] == ' ' ||
973                       Map.getSourceLine()[EndColNo - 1] == '\t'))
974     EndColNo = Map.startOfPreviousColumn(EndColNo);
975 
976   // If the start/end passed each other, then we are trying to highlight a
977   // range that just exists in whitespace. That most likely means we have
978   // a multi-line highlighting range that covers a blank line.
979   if (StartColNo > EndColNo)
980     return;
981 
982   // Fill the range with ~'s.
983   StartColNo = Map.byteToContainingColumn(StartColNo);
984   EndColNo = Map.byteToContainingColumn(EndColNo);
985 
986   assert(StartColNo <= EndColNo && "Invalid range!");
987   if (CaretLine.size() < EndColNo)
988     CaretLine.resize(EndColNo, ' ');
989   std::fill(CaretLine.begin() + StartColNo, CaretLine.begin() + EndColNo, '~');
990 }
991 
992 static std::string buildFixItInsertionLine(FileID FID,
993                                            unsigned LineNo,
994                                            const SourceColumnMap &map,
995                                            ArrayRef<FixItHint> Hints,
996                                            const SourceManager &SM,
997                                            const DiagnosticOptions *DiagOpts) {
998   std::string FixItInsertionLine;
999   if (Hints.empty() || !DiagOpts->ShowFixits)
1000     return FixItInsertionLine;
1001   unsigned PrevHintEndCol = 0;
1002 
1003   for (const auto &H : Hints) {
1004     if (H.CodeToInsert.empty())
1005       continue;
1006 
1007     // We have an insertion hint. Determine whether the inserted
1008     // code contains no newlines and is on the same line as the caret.
1009     std::pair<FileID, unsigned> HintLocInfo =
1010         SM.getDecomposedExpansionLoc(H.RemoveRange.getBegin());
1011     if (FID == HintLocInfo.first &&
1012         LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) &&
1013         StringRef(H.CodeToInsert).find_first_of("\n\r") == StringRef::npos) {
1014       // Insert the new code into the line just below the code
1015       // that the user wrote.
1016       // Note: When modifying this function, be very careful about what is a
1017       // "column" (printed width, platform-dependent) and what is a
1018       // "byte offset" (SourceManager "column").
1019       unsigned HintByteOffset =
1020           SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
1021 
1022       // The hint must start inside the source or right at the end
1023       assert(HintByteOffset < static_cast<unsigned>(map.bytes()) + 1);
1024       unsigned HintCol = map.byteToContainingColumn(HintByteOffset);
1025 
1026       // If we inserted a long previous hint, push this one forwards, and add
1027       // an extra space to show that this is not part of the previous
1028       // completion. This is sort of the best we can do when two hints appear
1029       // to overlap.
1030       //
1031       // Note that if this hint is located immediately after the previous
1032       // hint, no space will be added, since the location is more important.
1033       if (HintCol < PrevHintEndCol)
1034         HintCol = PrevHintEndCol + 1;
1035 
1036       // This should NOT use HintByteOffset, because the source might have
1037       // Unicode characters in earlier columns.
1038       unsigned NewFixItLineSize = FixItInsertionLine.size() +
1039                                   (HintCol - PrevHintEndCol) +
1040                                   H.CodeToInsert.size();
1041       if (NewFixItLineSize > FixItInsertionLine.size())
1042         FixItInsertionLine.resize(NewFixItLineSize, ' ');
1043 
1044       std::copy(H.CodeToInsert.begin(), H.CodeToInsert.end(),
1045                 FixItInsertionLine.end() - H.CodeToInsert.size());
1046 
1047       PrevHintEndCol = HintCol + llvm::sys::locale::columnWidth(H.CodeToInsert);
1048     }
1049   }
1050 
1051   expandTabs(FixItInsertionLine, DiagOpts->TabStop);
1052 
1053   return FixItInsertionLine;
1054 }
1055 
1056 static unsigned getNumDisplayWidth(unsigned N) {
1057   unsigned L = 1u, M = 10u;
1058   while (M <= N && ++L != std::numeric_limits<unsigned>::digits10 + 1)
1059     M *= 10u;
1060 
1061   return L;
1062 }
1063 
1064 /// Filter out invalid ranges, ranges that don't fit into the window of
1065 /// source lines we will print, and ranges from other files.
1066 ///
1067 /// For the remaining ranges, convert them to simple LineRange structs,
1068 /// which only cover one line at a time.
1069 static SmallVector<LineRange>
1070 prepareAndFilterRanges(const SmallVectorImpl<CharSourceRange> &Ranges,
1071                        const SourceManager &SM,
1072                        const std::pair<unsigned, unsigned> &Lines, FileID FID,
1073                        const LangOptions &LangOpts) {
1074   SmallVector<LineRange> LineRanges;
1075 
1076   for (const CharSourceRange &R : Ranges) {
1077     if (R.isInvalid())
1078       continue;
1079     SourceLocation Begin = R.getBegin();
1080     SourceLocation End = R.getEnd();
1081 
1082     unsigned StartLineNo = SM.getExpansionLineNumber(Begin);
1083     if (StartLineNo > Lines.second || SM.getFileID(Begin) != FID)
1084       continue;
1085 
1086     unsigned EndLineNo = SM.getExpansionLineNumber(End);
1087     if (EndLineNo < Lines.first || SM.getFileID(End) != FID)
1088       continue;
1089 
1090     unsigned StartColumn = SM.getExpansionColumnNumber(Begin);
1091     unsigned EndColumn = SM.getExpansionColumnNumber(End);
1092     if (R.isTokenRange())
1093       EndColumn += Lexer::MeasureTokenLength(End, SM, LangOpts);
1094 
1095     // Only a single line.
1096     if (StartLineNo == EndLineNo) {
1097       LineRanges.push_back({StartLineNo, StartColumn - 1, EndColumn - 1});
1098       continue;
1099     }
1100 
1101     // Start line.
1102     LineRanges.push_back({StartLineNo, StartColumn - 1, ~0u});
1103 
1104     // Middle lines.
1105     for (unsigned S = StartLineNo + 1; S != EndLineNo; ++S)
1106       LineRanges.push_back({S, 0, ~0u});
1107 
1108     // End line.
1109     LineRanges.push_back({EndLineNo, 0, EndColumn - 1});
1110   }
1111 
1112   return LineRanges;
1113 }
1114 
1115 /// Emit a code snippet and caret line.
1116 ///
1117 /// This routine emits a single line's code snippet and caret line..
1118 ///
1119 /// \param Loc The location for the caret.
1120 /// \param Ranges The underlined ranges for this code snippet.
1121 /// \param Hints The FixIt hints active for this diagnostic.
1122 void TextDiagnostic::emitSnippetAndCaret(
1123     FullSourceLoc Loc, DiagnosticsEngine::Level Level,
1124     SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) {
1125   assert(Loc.isValid() && "must have a valid source location here");
1126   assert(Loc.isFileID() && "must have a file location here");
1127 
1128   // If caret diagnostics are enabled and we have location, we want to
1129   // emit the caret.  However, we only do this if the location moved
1130   // from the last diagnostic, if the last diagnostic was a note that
1131   // was part of a different warning or error diagnostic, or if the
1132   // diagnostic has ranges.  We don't want to emit the same caret
1133   // multiple times if one loc has multiple diagnostics.
1134   if (!DiagOpts->ShowCarets)
1135     return;
1136   if (Loc == LastLoc && Ranges.empty() && Hints.empty() &&
1137       (LastLevel != DiagnosticsEngine::Note || Level == LastLevel))
1138     return;
1139 
1140   FileID FID = Loc.getFileID();
1141   const SourceManager &SM = Loc.getManager();
1142 
1143   // Get information about the buffer it points into.
1144   bool Invalid = false;
1145   StringRef BufData = Loc.getBufferData(&Invalid);
1146   if (Invalid)
1147     return;
1148   const char *BufStart = BufData.data();
1149   const char *BufEnd = BufStart + BufData.size();
1150 
1151   unsigned CaretLineNo = Loc.getLineNumber();
1152   unsigned CaretColNo = Loc.getColumnNumber();
1153 
1154   // Arbitrarily stop showing snippets when the line is too long.
1155   static const size_t MaxLineLengthToPrint = 4096;
1156   if (CaretColNo > MaxLineLengthToPrint)
1157     return;
1158 
1159   // Find the set of lines to include.
1160   const unsigned MaxLines = DiagOpts->SnippetLineLimit;
1161   std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
1162   unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
1163   for (const auto &I : Ranges) {
1164     if (auto OptionalRange = findLinesForRange(I, FID, SM))
1165       Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);
1166 
1167     DisplayLineNo =
1168         std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));
1169   }
1170 
1171   // Our line numbers look like:
1172   // " [number] | "
1173   // Where [number] is MaxLineNoDisplayWidth columns
1174   // and the full thing is therefore MaxLineNoDisplayWidth + 4 columns.
1175   unsigned MaxLineNoDisplayWidth =
1176       DiagOpts->ShowLineNumbers
1177           ? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines))
1178           : 0;
1179   auto indentForLineNumbers = [&] {
1180     if (MaxLineNoDisplayWidth > 0)
1181       OS.indent(MaxLineNoDisplayWidth + 2) << "| ";
1182   };
1183 
1184   SmallVector<LineRange> LineRanges =
1185       prepareAndFilterRanges(Ranges, SM, Lines, FID, LangOpts);
1186 
1187   for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1;
1188        ++LineNo, ++DisplayLineNo) {
1189     // Rewind from the current position to the start of the line.
1190     const char *LineStart =
1191         BufStart +
1192         SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second;
1193     if (LineStart == BufEnd)
1194       break;
1195 
1196     // Compute the line end.
1197     const char *LineEnd = LineStart;
1198     while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd)
1199       ++LineEnd;
1200 
1201     // Arbitrarily stop showing snippets when the line is too long.
1202     // FIXME: Don't print any lines in this case.
1203     if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint)
1204       return;
1205 
1206     // Copy the line of code into an std::string for ease of manipulation.
1207     std::string SourceLine(LineStart, LineEnd);
1208     // Remove trailing null bytes.
1209     while (!SourceLine.empty() && SourceLine.back() == '\0' &&
1210            (LineNo != CaretLineNo || SourceLine.size() > CaretColNo))
1211       SourceLine.pop_back();
1212 
1213     // Build the byte to column map.
1214     const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop);
1215 
1216     std::string CaretLine;
1217     // Highlight all of the characters covered by Ranges with ~ characters.
1218     for (const auto &LR : LineRanges) {
1219       if (LR.LineNo == LineNo)
1220         highlightRange(LR, sourceColMap, CaretLine);
1221     }
1222 
1223     // Next, insert the caret itself.
1224     if (CaretLineNo == LineNo) {
1225       size_t Col = sourceColMap.byteToContainingColumn(CaretColNo - 1);
1226       CaretLine.resize(std::max(Col + 1, CaretLine.size()), ' ');
1227       CaretLine[Col] = '^';
1228     }
1229 
1230     std::string FixItInsertionLine = buildFixItInsertionLine(
1231         FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get());
1232 
1233     // If the source line is too long for our terminal, select only the
1234     // "interesting" source region within that line.
1235     unsigned Columns = DiagOpts->MessageLength;
1236     if (Columns)
1237       selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
1238                                     Columns, sourceColMap);
1239 
1240     // If we are in -fdiagnostics-print-source-range-info mode, we are trying
1241     // to produce easily machine parsable output.  Add a space before the
1242     // source line and the caret to make it trivial to tell the main diagnostic
1243     // line from what the user is intended to see.
1244     if (DiagOpts->ShowSourceRanges && !SourceLine.empty()) {
1245       SourceLine = ' ' + SourceLine;
1246       CaretLine = ' ' + CaretLine;
1247     }
1248 
1249     // Emit what we have computed.
1250     emitSnippet(SourceLine, MaxLineNoDisplayWidth, DisplayLineNo);
1251 
1252     if (!CaretLine.empty()) {
1253       indentForLineNumbers();
1254       if (DiagOpts->ShowColors)
1255         OS.changeColor(caretColor, true);
1256       OS << CaretLine << '\n';
1257       if (DiagOpts->ShowColors)
1258         OS.resetColor();
1259     }
1260 
1261     if (!FixItInsertionLine.empty()) {
1262       indentForLineNumbers();
1263       if (DiagOpts->ShowColors)
1264         // Print fixit line in color
1265         OS.changeColor(fixitColor, false);
1266       if (DiagOpts->ShowSourceRanges)
1267         OS << ' ';
1268       OS << FixItInsertionLine << '\n';
1269       if (DiagOpts->ShowColors)
1270         OS.resetColor();
1271     }
1272   }
1273 
1274   // Print out any parseable fixit information requested by the options.
1275   emitParseableFixits(Hints, SM);
1276 }
1277 
1278 void TextDiagnostic::emitSnippet(StringRef SourceLine,
1279                                  unsigned MaxLineNoDisplayWidth,
1280                                  unsigned LineNo) {
1281   // Emit line number.
1282   if (MaxLineNoDisplayWidth > 0) {
1283     unsigned LineNoDisplayWidth = getNumDisplayWidth(LineNo);
1284     OS.indent(MaxLineNoDisplayWidth - LineNoDisplayWidth + 1)
1285         << LineNo << " | ";
1286   }
1287 
1288   // Print the source line one character at a time.
1289   bool PrintReversed = false;
1290   size_t I = 0;
1291   while (I < SourceLine.size()) {
1292     auto [Str, WasPrintable] =
1293         printableTextForNextCharacter(SourceLine, &I, DiagOpts->TabStop);
1294 
1295     // Toggle inverted colors on or off for this character.
1296     if (DiagOpts->ShowColors) {
1297       if (WasPrintable == PrintReversed) {
1298         PrintReversed = !PrintReversed;
1299         if (PrintReversed)
1300           OS.reverseColor();
1301         else
1302           OS.resetColor();
1303       }
1304     }
1305     OS << Str;
1306   }
1307 
1308   if (DiagOpts->ShowColors)
1309     OS.resetColor();
1310 
1311   OS << '\n';
1312 }
1313 
1314 void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints,
1315                                          const SourceManager &SM) {
1316   if (!DiagOpts->ShowParseableFixits)
1317     return;
1318 
1319   // We follow FixItRewriter's example in not (yet) handling
1320   // fix-its in macros.
1321   for (const auto &H : Hints) {
1322     if (H.RemoveRange.isInvalid() || H.RemoveRange.getBegin().isMacroID() ||
1323         H.RemoveRange.getEnd().isMacroID())
1324       return;
1325   }
1326 
1327   for (const auto &H : Hints) {
1328     SourceLocation BLoc = H.RemoveRange.getBegin();
1329     SourceLocation ELoc = H.RemoveRange.getEnd();
1330 
1331     std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
1332     std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
1333 
1334     // Adjust for token ranges.
1335     if (H.RemoveRange.isTokenRange())
1336       EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
1337 
1338     // We specifically do not do word-wrapping or tab-expansion here,
1339     // because this is supposed to be easy to parse.
1340     PresumedLoc PLoc = SM.getPresumedLoc(BLoc);
1341     if (PLoc.isInvalid())
1342       break;
1343 
1344     OS << "fix-it:\"";
1345     OS.write_escaped(PLoc.getFilename());
1346     OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second)
1347       << ':' << SM.getColumnNumber(BInfo.first, BInfo.second)
1348       << '-' << SM.getLineNumber(EInfo.first, EInfo.second)
1349       << ':' << SM.getColumnNumber(EInfo.first, EInfo.second)
1350       << "}:\"";
1351     OS.write_escaped(H.CodeToInsert);
1352     OS << "\"\n";
1353   }
1354 }
1355