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