/src/llvm-project/clang/lib/Frontend/TextDiagnostic.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | bool &Normal, bool Bold) { |
47 | 0 | while (true) { |
48 | 0 | size_t Pos = Str.find(ToggleHighlight); |
49 | 0 | OS << Str.slice(0, Pos); |
50 | 0 | if (Pos == StringRef::npos) |
51 | 0 | break; |
52 | | |
53 | 0 | Str = Str.substr(Pos + 1); |
54 | 0 | if (Normal) |
55 | 0 | OS.changeColor(templateColor, true); |
56 | 0 | else { |
57 | 0 | OS.resetColor(); |
58 | 0 | if (Bold) |
59 | 0 | OS.changeColor(savedColor, true); |
60 | 0 | } |
61 | 0 | Normal = !Normal; |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | /// Number of spaces to indent when word-wrapping. |
66 | | const unsigned WordWrapIndentation = 6; |
67 | | |
68 | 0 | static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) { |
69 | 0 | int bytes = 0; |
70 | 0 | while (0<i) { |
71 | 0 | if (SourceLine[--i]=='\t') |
72 | 0 | break; |
73 | 0 | ++bytes; |
74 | 0 | } |
75 | 0 | return bytes; |
76 | 0 | } |
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 | 0 | unsigned TabStop) { |
100 | 0 | assert(I && "I must not be null"); |
101 | 0 | assert(*I < SourceLine.size() && "must point to a valid index"); |
102 | | |
103 | 0 | if (SourceLine[*I] == '\t') { |
104 | 0 | assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop && |
105 | 0 | "Invalid -ftabstop value"); |
106 | 0 | unsigned Col = bytesSincePreviousTabOrLineBegin(SourceLine, *I); |
107 | 0 | unsigned NumSpaces = TabStop - (Col % TabStop); |
108 | 0 | assert(0 < NumSpaces && NumSpaces <= TabStop |
109 | 0 | && "Invalid computation of space amt"); |
110 | 0 | ++(*I); |
111 | |
|
112 | 0 | SmallString<16> ExpandedTab; |
113 | 0 | ExpandedTab.assign(NumSpaces, ' '); |
114 | 0 | return std::make_pair(ExpandedTab, true); |
115 | 0 | } |
116 | | |
117 | 0 | const unsigned char *Begin = SourceLine.bytes_begin() + *I; |
118 | | |
119 | | // Fast path for the common ASCII case. |
120 | 0 | if (*Begin < 0x80 && llvm::sys::locale::isPrint(*Begin)) { |
121 | 0 | ++(*I); |
122 | 0 | return std::make_pair(SmallString<16>(Begin, Begin + 1), true); |
123 | 0 | } |
124 | 0 | unsigned CharSize = llvm::getNumBytesForUTF8(*Begin); |
125 | 0 | const unsigned char *End = Begin + CharSize; |
126 | | |
127 | | // Convert it to UTF32 and check if it's printable. |
128 | 0 | if (End <= SourceLine.bytes_end() && llvm::isLegalUTF8Sequence(Begin, End)) { |
129 | 0 | llvm::UTF32 C; |
130 | 0 | llvm::UTF32 *CPtr = &C; |
131 | | |
132 | | // Begin and end before conversion. |
133 | 0 | unsigned char const *OriginalBegin = Begin; |
134 | 0 | llvm::ConversionResult Res = llvm::ConvertUTF8toUTF32( |
135 | 0 | &Begin, End, &CPtr, CPtr + 1, llvm::strictConversion); |
136 | 0 | (void)Res; |
137 | 0 | assert(Res == llvm::conversionOK); |
138 | 0 | assert(OriginalBegin < Begin); |
139 | 0 | assert((Begin - OriginalBegin) == CharSize); |
140 | | |
141 | 0 | (*I) += (Begin - OriginalBegin); |
142 | | |
143 | | // Valid, multi-byte, printable UTF8 character. |
144 | 0 | if (llvm::sys::locale::isPrint(C)) |
145 | 0 | return std::make_pair(SmallString<16>(OriginalBegin, End), true); |
146 | | |
147 | | // Valid but not printable. |
148 | 0 | SmallString<16> Str("<U+>"); |
149 | 0 | while (C) { |
150 | 0 | Str.insert(Str.begin() + 3, llvm::hexdigit(C % 16)); |
151 | 0 | C /= 16; |
152 | 0 | } |
153 | 0 | while (Str.size() < 8) |
154 | 0 | Str.insert(Str.begin() + 3, llvm::hexdigit(0)); |
155 | 0 | return std::make_pair(Str, false); |
156 | 0 | } |
157 | | |
158 | | // Otherwise, not printable since it's not valid UTF8. |
159 | 0 | SmallString<16> ExpandedByte("<XX>"); |
160 | 0 | unsigned char Byte = SourceLine[*I]; |
161 | 0 | ExpandedByte[1] = llvm::hexdigit(Byte / 16); |
162 | 0 | ExpandedByte[2] = llvm::hexdigit(Byte % 16); |
163 | 0 | ++(*I); |
164 | 0 | return std::make_pair(ExpandedByte, false); |
165 | 0 | } |
166 | | |
167 | 0 | static void expandTabs(std::string &SourceLine, unsigned TabStop) { |
168 | 0 | size_t I = SourceLine.size(); |
169 | 0 | while (I > 0) { |
170 | 0 | I--; |
171 | 0 | if (SourceLine[I] != '\t') |
172 | 0 | continue; |
173 | 0 | size_t TmpI = I; |
174 | 0 | auto [Str, Printable] = |
175 | 0 | printableTextForNextCharacter(SourceLine, &TmpI, TabStop); |
176 | 0 | SourceLine.replace(I, 1, Str.c_str()); |
177 | 0 | } |
178 | 0 | } |
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 | 0 | SmallVectorImpl<int> &ColumnsOut) { |
219 | 0 | assert(BytesOut.empty()); |
220 | 0 | assert(ColumnsOut.empty()); |
221 | | |
222 | 0 | if (SourceLine.empty()) { |
223 | 0 | BytesOut.resize(1u, 0); |
224 | 0 | ColumnsOut.resize(1u, 0); |
225 | 0 | return; |
226 | 0 | } |
227 | | |
228 | 0 | ColumnsOut.resize(SourceLine.size() + 1, -1); |
229 | |
|
230 | 0 | int Columns = 0; |
231 | 0 | size_t I = 0; |
232 | 0 | while (I < SourceLine.size()) { |
233 | 0 | ColumnsOut[I] = Columns; |
234 | 0 | BytesOut.resize(Columns + 1, -1); |
235 | 0 | BytesOut.back() = I; |
236 | 0 | auto [Str, Printable] = |
237 | 0 | printableTextForNextCharacter(SourceLine, &I, TabStop); |
238 | 0 | Columns += llvm::sys::locale::columnWidth(Str); |
239 | 0 | } |
240 | |
|
241 | 0 | ColumnsOut.back() = Columns; |
242 | 0 | BytesOut.resize(Columns + 1, -1); |
243 | 0 | BytesOut.back() = I; |
244 | 0 | } |
245 | | |
246 | | namespace { |
247 | | struct SourceColumnMap { |
248 | | SourceColumnMap(StringRef SourceLine, unsigned TabStop) |
249 | 0 | : m_SourceLine(SourceLine) { |
250 | |
|
251 | 0 | genColumnByteMapping(SourceLine, TabStop, m_columnToByte, m_byteToColumn); |
252 | |
|
253 | 0 | assert(m_byteToColumn.size()==SourceLine.size()+1); |
254 | 0 | assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size()); |
255 | 0 | assert(m_byteToColumn.size() |
256 | 0 | == static_cast<unsigned>(m_columnToByte.back()+1)); |
257 | 0 | assert(static_cast<unsigned>(m_byteToColumn.back()+1) |
258 | 0 | == m_columnToByte.size()); |
259 | 0 | } |
260 | 0 | int columns() const { return m_byteToColumn.back(); } |
261 | 0 | 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 | 0 | int byteToColumn(int n) const { |
266 | 0 | assert(0<=n && n<static_cast<int>(m_byteToColumn.size())); |
267 | 0 | return m_byteToColumn[n]; |
268 | 0 | } |
269 | | |
270 | | /// Map a byte to the first column which contains it. |
271 | 0 | int byteToContainingColumn(int N) const { |
272 | 0 | assert(0 <= N && N < static_cast<int>(m_byteToColumn.size())); |
273 | 0 | while (m_byteToColumn[N] == -1) |
274 | 0 | --N; |
275 | 0 | return m_byteToColumn[N]; |
276 | 0 | } |
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 | 0 | int columnToByte(int n) const { |
282 | 0 | assert(0<=n && n<static_cast<int>(m_columnToByte.size())); |
283 | 0 | return m_columnToByte[n]; |
284 | 0 | } |
285 | | |
286 | | /// Map from a byte index to the next byte which starts a column. |
287 | 0 | int startOfNextColumn(int N) const { |
288 | 0 | assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1)); |
289 | 0 | while (byteToColumn(++N) == -1) {} |
290 | 0 | return N; |
291 | 0 | } |
292 | | |
293 | | /// Map from a byte index to the previous byte which starts a column. |
294 | 0 | int startOfPreviousColumn(int N) const { |
295 | 0 | assert(0 < N && N < static_cast<int>(m_byteToColumn.size())); |
296 | 0 | while (byteToColumn(--N) == -1) {} |
297 | 0 | return N; |
298 | 0 | } |
299 | | |
300 | 0 | StringRef getSourceLine() const { |
301 | 0 | return m_SourceLine; |
302 | 0 | } |
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 | 0 | const SourceColumnMap &map) { |
318 | 0 | unsigned CaretColumns = CaretLine.size(); |
319 | 0 | unsigned FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine); |
320 | 0 | unsigned MaxColumns = std::max(static_cast<unsigned>(map.columns()), |
321 | 0 | std::max(CaretColumns, FixItColumns)); |
322 | | // if the number of columns is less than the desired number we're done |
323 | 0 | if (MaxColumns <= Columns) |
324 | 0 | return; |
325 | | |
326 | | // No special characters are allowed in CaretLine. |
327 | 0 | 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 | 0 | unsigned CaretStart = 0, CaretEnd = CaretLine.size(); |
332 | 0 | for (; CaretStart != CaretEnd; ++CaretStart) |
333 | 0 | if (!isWhitespace(CaretLine[CaretStart])) |
334 | 0 | break; |
335 | |
|
336 | 0 | for (; CaretEnd != CaretStart; --CaretEnd) |
337 | 0 | if (!isWhitespace(CaretLine[CaretEnd - 1])) |
338 | 0 | 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 | 0 | if (!FixItInsertionLine.empty()) { |
346 | 0 | unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); |
347 | 0 | for (; FixItStart != FixItEnd; ++FixItStart) |
348 | 0 | if (!isWhitespace(FixItInsertionLine[FixItStart])) |
349 | 0 | break; |
350 | |
|
351 | 0 | for (; FixItEnd != FixItStart; --FixItEnd) |
352 | 0 | if (!isWhitespace(FixItInsertionLine[FixItEnd - 1])) |
353 | 0 | 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 | 0 | unsigned FixItStartCol = FixItStart; |
359 | 0 | unsigned FixItEndCol |
360 | 0 | = llvm::sys::locale::columnWidth(FixItInsertionLine.substr(0, FixItEnd)); |
361 | |
|
362 | 0 | CaretStart = std::min(FixItStartCol, CaretStart); |
363 | 0 | CaretEnd = std::max(FixItEndCol, CaretEnd); |
364 | 0 | } |
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 | 0 | while (static_cast<int>(CaretEnd) < map.columns() && |
370 | 0 | -1 == map.columnToByte(CaretEnd)) |
371 | 0 | ++CaretEnd; |
372 | |
|
373 | 0 | assert((static_cast<int>(CaretStart) > map.columns() || |
374 | 0 | -1!=map.columnToByte(CaretStart)) && |
375 | 0 | "CaretStart must not point to a column in the middle of a source" |
376 | 0 | " line character"); |
377 | 0 | assert((static_cast<int>(CaretEnd) > map.columns() || |
378 | 0 | -1!=map.columnToByte(CaretEnd)) && |
379 | 0 | "CaretEnd must not point to a column in the middle of a source line" |
380 | 0 | " 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 | 0 | unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart, |
388 | 0 | map.columns())); |
389 | 0 | unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd, |
390 | 0 | map.columns())); |
391 | |
|
392 | 0 | unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart |
393 | 0 | - (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart)); |
394 | |
|
395 | 0 | char const *front_ellipse = " ..."; |
396 | 0 | char const *front_space = " "; |
397 | 0 | char const *back_ellipse = "..."; |
398 | 0 | unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse); |
399 | |
|
400 | 0 | unsigned TargetColumns = Columns; |
401 | | // Give us extra room for the ellipses |
402 | | // and any of the caret line that extends past the source |
403 | 0 | if (TargetColumns > ellipses_space+CaretColumnsOutsideSource) |
404 | 0 | TargetColumns -= ellipses_space+CaretColumnsOutsideSource; |
405 | |
|
406 | 0 | while (SourceStart>0 || SourceEnd<SourceLine.size()) { |
407 | 0 | bool ExpandedRegion = false; |
408 | |
|
409 | 0 | if (SourceStart>0) { |
410 | 0 | 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 | 0 | while (NewStart && isWhitespace(SourceLine[NewStart])) |
416 | 0 | NewStart = map.startOfPreviousColumn(NewStart); |
417 | | |
418 | | // Skip over this bit of "interesting" text. |
419 | 0 | while (NewStart) { |
420 | 0 | unsigned Prev = map.startOfPreviousColumn(NewStart); |
421 | 0 | if (isWhitespace(SourceLine[Prev])) |
422 | 0 | break; |
423 | 0 | NewStart = Prev; |
424 | 0 | } |
425 | |
|
426 | 0 | assert(map.byteToColumn(NewStart) != -1); |
427 | 0 | unsigned NewColumns = map.byteToColumn(SourceEnd) - |
428 | 0 | map.byteToColumn(NewStart); |
429 | 0 | if (NewColumns <= TargetColumns) { |
430 | 0 | SourceStart = NewStart; |
431 | 0 | ExpandedRegion = true; |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | 0 | if (SourceEnd<SourceLine.size()) { |
436 | 0 | 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 | 0 | while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd])) |
442 | 0 | NewEnd = map.startOfNextColumn(NewEnd); |
443 | | |
444 | | // Skip over this bit of "interesting" text. |
445 | 0 | while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd])) |
446 | 0 | NewEnd = map.startOfNextColumn(NewEnd); |
447 | |
|
448 | 0 | assert(map.byteToColumn(NewEnd) != -1); |
449 | 0 | unsigned NewColumns = map.byteToColumn(NewEnd) - |
450 | 0 | map.byteToColumn(SourceStart); |
451 | 0 | if (NewColumns <= TargetColumns) { |
452 | 0 | SourceEnd = NewEnd; |
453 | 0 | ExpandedRegion = true; |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | 0 | if (!ExpandedRegion) |
458 | 0 | break; |
459 | 0 | } |
460 | |
|
461 | 0 | CaretStart = map.byteToColumn(SourceStart); |
462 | 0 | 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 | 0 | assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 && |
467 | 0 | SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1); |
468 | 0 | assert(SourceStart <= SourceEnd); |
469 | 0 | assert(CaretStart <= CaretEnd); |
470 | | |
471 | 0 | unsigned BackColumnsRemoved |
472 | 0 | = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd); |
473 | 0 | unsigned FrontColumnsRemoved = CaretStart; |
474 | 0 | unsigned ColumnsKept = CaretEnd-CaretStart; |
475 | | |
476 | | // We checked up front that the line needed truncation |
477 | 0 | 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 | 0 | if (BackColumnsRemoved > strlen(back_ellipse)) |
482 | 0 | SourceLine.replace(SourceEnd, std::string::npos, back_ellipse); |
483 | | |
484 | | // If that's enough then we're done |
485 | 0 | if (FrontColumnsRemoved+ColumnsKept <= Columns) |
486 | 0 | return; |
487 | | |
488 | | // Otherwise remove the front as well |
489 | 0 | if (FrontColumnsRemoved > strlen(front_ellipse)) { |
490 | 0 | SourceLine.replace(0, SourceStart, front_ellipse); |
491 | 0 | CaretLine.replace(0, CaretStart, front_space); |
492 | 0 | if (!FixItInsertionLine.empty()) |
493 | 0 | FixItInsertionLine.replace(0, CaretStart, front_space); |
494 | 0 | } |
495 | 0 | } |
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 | 0 | static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) { |
504 | 0 | while (Idx < Length && isWhitespace(Str[Idx])) |
505 | 0 | ++Idx; |
506 | 0 | return Idx; |
507 | 0 | } |
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 | 0 | static inline char findMatchingPunctuation(char c) { |
516 | 0 | switch (c) { |
517 | 0 | case '\'': return '\''; |
518 | 0 | case '`': return '\''; |
519 | 0 | case '"': return '"'; |
520 | 0 | case '(': return ')'; |
521 | 0 | case '[': return ']'; |
522 | 0 | case '{': return '}'; |
523 | 0 | default: break; |
524 | 0 | } |
525 | | |
526 | 0 | return 0; |
527 | 0 | } |
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 | 0 | unsigned Columns) { |
537 | 0 | assert(Start < Str.size() && "Invalid start position!"); |
538 | 0 | unsigned End = Start + 1; |
539 | | |
540 | | // If we are already at the end of the string, take that as the word. |
541 | 0 | if (End == Str.size()) |
542 | 0 | return End; |
543 | | |
544 | | // Determine if the start of the string is actually opening |
545 | | // punctuation, e.g., a quote or parentheses. |
546 | 0 | char EndPunct = findMatchingPunctuation(Str[Start]); |
547 | 0 | if (!EndPunct) { |
548 | | // This is a normal word. Just find the first space character. |
549 | 0 | while (End < Length && !isWhitespace(Str[End])) |
550 | 0 | ++End; |
551 | 0 | return End; |
552 | 0 | } |
553 | | |
554 | | // We have the start of a balanced punctuation sequence (quotes, |
555 | | // parentheses, etc.). Determine the full sequence is. |
556 | 0 | SmallString<16> PunctuationEndStack; |
557 | 0 | PunctuationEndStack.push_back(EndPunct); |
558 | 0 | while (End < Length && !PunctuationEndStack.empty()) { |
559 | 0 | if (Str[End] == PunctuationEndStack.back()) |
560 | 0 | PunctuationEndStack.pop_back(); |
561 | 0 | else if (char SubEndPunct = findMatchingPunctuation(Str[End])) |
562 | 0 | PunctuationEndStack.push_back(SubEndPunct); |
563 | |
|
564 | 0 | ++End; |
565 | 0 | } |
566 | | |
567 | | // Find the first space character after the punctuation ended. |
568 | 0 | while (End < Length && !isWhitespace(Str[End])) |
569 | 0 | ++End; |
570 | |
|
571 | 0 | unsigned PunctWordLength = End - Start; |
572 | 0 | if (// If the word fits on this line |
573 | 0 | Column + PunctWordLength <= Columns || |
574 | | // ... or the word is "short enough" to take up the next line |
575 | | // without too much ugly white space |
576 | 0 | PunctWordLength < Columns/3) |
577 | 0 | 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 | 0 | return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); |
584 | 0 | } |
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 | 0 | unsigned Column, bool Bold) { |
601 | 0 | const unsigned Length = std::min(Str.find('\n'), Str.size()); |
602 | 0 | bool TextNormal = true; |
603 | |
|
604 | 0 | bool Wrapped = false; |
605 | 0 | for (unsigned WordStart = 0, WordEnd; WordStart < Length; |
606 | 0 | WordStart = WordEnd) { |
607 | | // Find the beginning of the next word. |
608 | 0 | WordStart = skipWhitespace(WordStart, Str, Length); |
609 | 0 | if (WordStart == Length) |
610 | 0 | break; |
611 | | |
612 | | // Find the end of this word. |
613 | 0 | WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); |
614 | | |
615 | | // Does this word fit on the current line? |
616 | 0 | unsigned WordLength = WordEnd - WordStart; |
617 | 0 | if (Column + WordLength < Columns) { |
618 | | // This word fits on the current line; print it there. |
619 | 0 | if (WordStart) { |
620 | 0 | OS << ' '; |
621 | 0 | Column += 1; |
622 | 0 | } |
623 | 0 | applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), |
624 | 0 | TextNormal, Bold); |
625 | 0 | Column += WordLength; |
626 | 0 | continue; |
627 | 0 | } |
628 | | |
629 | | // This word does not fit on the current line, so wrap to the next |
630 | | // line. |
631 | 0 | OS << '\n'; |
632 | 0 | OS.indent(WordWrapIndentation); |
633 | 0 | applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), |
634 | 0 | TextNormal, Bold); |
635 | 0 | Column = WordWrapIndentation + WordLength; |
636 | 0 | Wrapped = true; |
637 | 0 | } |
638 | | |
639 | | // Append any remaning text from the message with its existing formatting. |
640 | 0 | applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold); |
641 | |
|
642 | 0 | assert(TextNormal && "Text highlighted at end of diagnostic message."); |
643 | | |
644 | 0 | return Wrapped; |
645 | 0 | } |
646 | | |
647 | | TextDiagnostic::TextDiagnostic(raw_ostream &OS, |
648 | | const LangOptions &LangOpts, |
649 | | DiagnosticOptions *DiagOpts) |
650 | 0 | : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {} |
651 | | |
652 | 0 | TextDiagnostic::~TextDiagnostic() {} |
653 | | |
654 | | void TextDiagnostic::emitDiagnosticMessage( |
655 | | FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, |
656 | | StringRef Message, ArrayRef<clang::CharSourceRange> Ranges, |
657 | 0 | DiagOrStoredDiag D) { |
658 | 0 | uint64_t StartOfLocationInfo = OS.tell(); |
659 | | |
660 | | // Emit the location of this particular diagnostic. |
661 | 0 | if (Loc.isValid()) |
662 | 0 | emitDiagnosticLoc(Loc, PLoc, Level, Ranges); |
663 | |
|
664 | 0 | if (DiagOpts->ShowColors) |
665 | 0 | OS.resetColor(); |
666 | |
|
667 | 0 | if (DiagOpts->ShowLevel) |
668 | 0 | printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); |
669 | 0 | printDiagnosticMessage(OS, |
670 | 0 | /*IsSupplemental*/ Level == DiagnosticsEngine::Note, |
671 | 0 | Message, OS.tell() - StartOfLocationInfo, |
672 | 0 | DiagOpts->MessageLength, DiagOpts->ShowColors); |
673 | 0 | } |
674 | | |
675 | | /*static*/ void |
676 | | TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, |
677 | | DiagnosticsEngine::Level Level, |
678 | 0 | bool ShowColors) { |
679 | 0 | if (ShowColors) { |
680 | | // Print diagnostic category in bold and color |
681 | 0 | switch (Level) { |
682 | 0 | case DiagnosticsEngine::Ignored: |
683 | 0 | llvm_unreachable("Invalid diagnostic type"); |
684 | 0 | case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break; |
685 | 0 | case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break; |
686 | 0 | case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break; |
687 | 0 | case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break; |
688 | 0 | case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break; |
689 | 0 | } |
690 | 0 | } |
691 | | |
692 | 0 | switch (Level) { |
693 | 0 | case DiagnosticsEngine::Ignored: |
694 | 0 | llvm_unreachable("Invalid diagnostic type"); |
695 | 0 | case DiagnosticsEngine::Note: OS << "note: "; break; |
696 | 0 | case DiagnosticsEngine::Remark: OS << "remark: "; break; |
697 | 0 | case DiagnosticsEngine::Warning: OS << "warning: "; break; |
698 | 0 | case DiagnosticsEngine::Error: OS << "error: "; break; |
699 | 0 | case DiagnosticsEngine::Fatal: OS << "fatal error: "; break; |
700 | 0 | } |
701 | | |
702 | 0 | if (ShowColors) |
703 | 0 | OS.resetColor(); |
704 | 0 | } |
705 | | |
706 | | /*static*/ |
707 | | void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, |
708 | | bool IsSupplemental, |
709 | | StringRef Message, |
710 | | unsigned CurrentColumn, |
711 | 0 | unsigned Columns, bool ShowColors) { |
712 | 0 | bool Bold = false; |
713 | 0 | 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 | 0 | OS.changeColor(savedColor, true); |
717 | 0 | Bold = true; |
718 | 0 | } |
719 | |
|
720 | 0 | if (Columns) |
721 | 0 | printWordWrapped(OS, Message, Columns, CurrentColumn, Bold); |
722 | 0 | else { |
723 | 0 | bool Normal = true; |
724 | 0 | applyTemplateHighlighting(OS, Message, Normal, Bold); |
725 | 0 | assert(Normal && "Formatting should have returned to normal"); |
726 | 0 | } |
727 | | |
728 | 0 | if (ShowColors) |
729 | 0 | OS.resetColor(); |
730 | 0 | OS << '\n'; |
731 | 0 | } |
732 | | |
733 | 0 | void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { |
734 | | #ifdef _WIN32 |
735 | | SmallString<4096> TmpFilename; |
736 | | #endif |
737 | 0 | if (DiagOpts->AbsolutePath) { |
738 | 0 | auto File = SM.getFileManager().getOptionalFileRef(Filename); |
739 | 0 | 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 | 0 | Filename = SM.getFileManager().getCanonicalName(*File); |
762 | 0 | #endif |
763 | 0 | } |
764 | 0 | } |
765 | |
|
766 | 0 | OS << Filename; |
767 | 0 | } |
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 | 0 | ArrayRef<CharSourceRange> Ranges) { |
778 | 0 | if (PLoc.isInvalid()) { |
779 | | // At least print the file name if available: |
780 | 0 | if (FileID FID = Loc.getFileID(); FID.isValid()) { |
781 | 0 | if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) { |
782 | 0 | emitFilename(FE->getName(), Loc.getManager()); |
783 | 0 | OS << ": "; |
784 | 0 | } |
785 | 0 | } |
786 | 0 | return; |
787 | 0 | } |
788 | 0 | unsigned LineNo = PLoc.getLine(); |
789 | |
|
790 | 0 | if (!DiagOpts->ShowLocation) |
791 | 0 | return; |
792 | | |
793 | 0 | if (DiagOpts->ShowColors) |
794 | 0 | OS.changeColor(savedColor, true); |
795 | |
|
796 | 0 | emitFilename(PLoc.getFilename(), Loc.getManager()); |
797 | 0 | switch (DiagOpts->getFormat()) { |
798 | 0 | case DiagnosticOptions::SARIF: |
799 | 0 | case DiagnosticOptions::Clang: |
800 | 0 | if (DiagOpts->ShowLine) |
801 | 0 | OS << ':' << LineNo; |
802 | 0 | break; |
803 | 0 | case DiagnosticOptions::MSVC: OS << '(' << LineNo; break; |
804 | 0 | case DiagnosticOptions::Vi: OS << " +" << LineNo; break; |
805 | 0 | } |
806 | | |
807 | 0 | if (DiagOpts->ShowColumn) |
808 | | // Compute the column number. |
809 | 0 | if (unsigned ColNo = PLoc.getColumn()) { |
810 | 0 | if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) { |
811 | 0 | OS << ','; |
812 | | // Visual Studio 2010 or earlier expects column number to be off by one |
813 | 0 | if (LangOpts.MSCompatibilityVersion && |
814 | 0 | !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012)) |
815 | 0 | ColNo--; |
816 | 0 | } else |
817 | 0 | OS << ':'; |
818 | 0 | OS << ColNo; |
819 | 0 | } |
820 | 0 | switch (DiagOpts->getFormat()) { |
821 | 0 | case DiagnosticOptions::SARIF: |
822 | 0 | case DiagnosticOptions::Clang: |
823 | 0 | case DiagnosticOptions::Vi: OS << ':'; break; |
824 | 0 | case DiagnosticOptions::MSVC: |
825 | | // MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the |
826 | | // space and prints 'file(4): error'. |
827 | 0 | OS << ')'; |
828 | 0 | if (LangOpts.MSCompatibilityVersion && |
829 | 0 | !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)) |
830 | 0 | OS << ' '; |
831 | 0 | OS << ':'; |
832 | 0 | break; |
833 | 0 | } |
834 | | |
835 | 0 | if (DiagOpts->ShowSourceRanges && !Ranges.empty()) { |
836 | 0 | FileID CaretFileID = Loc.getExpansionLoc().getFileID(); |
837 | 0 | bool PrintedRange = false; |
838 | 0 | const SourceManager &SM = Loc.getManager(); |
839 | |
|
840 | 0 | for (const auto &R : Ranges) { |
841 | | // Ignore invalid ranges. |
842 | 0 | if (!R.isValid()) |
843 | 0 | continue; |
844 | | |
845 | 0 | SourceLocation B = SM.getExpansionLoc(R.getBegin()); |
846 | 0 | CharSourceRange ERange = SM.getExpansionRange(R.getEnd()); |
847 | 0 | SourceLocation E = ERange.getEnd(); |
848 | | |
849 | | // If the start or end of the range is in another file, just |
850 | | // discard it. |
851 | 0 | if (SM.getFileID(B) != CaretFileID || SM.getFileID(E) != CaretFileID) |
852 | 0 | continue; |
853 | | |
854 | | // Add in the length of the token, so that we cover multi-char |
855 | | // tokens. |
856 | 0 | unsigned TokSize = 0; |
857 | 0 | if (ERange.isTokenRange()) |
858 | 0 | TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts); |
859 | |
|
860 | 0 | FullSourceLoc BF(B, SM), EF(E, SM); |
861 | 0 | OS << '{' |
862 | 0 | << BF.getLineNumber() << ':' << BF.getColumnNumber() << '-' |
863 | 0 | << EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize) |
864 | 0 | << '}'; |
865 | 0 | PrintedRange = true; |
866 | 0 | } |
867 | |
|
868 | 0 | if (PrintedRange) |
869 | 0 | OS << ':'; |
870 | 0 | } |
871 | 0 | OS << ' '; |
872 | 0 | } |
873 | | |
874 | 0 | void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { |
875 | 0 | if (DiagOpts->ShowLocation && PLoc.isValid()) { |
876 | 0 | OS << "In file included from "; |
877 | 0 | emitFilename(PLoc.getFilename(), Loc.getManager()); |
878 | 0 | OS << ':' << PLoc.getLine() << ":\n"; |
879 | 0 | } else |
880 | 0 | OS << "In included file:\n"; |
881 | 0 | } |
882 | | |
883 | | void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, |
884 | 0 | StringRef ModuleName) { |
885 | 0 | if (DiagOpts->ShowLocation && PLoc.isValid()) |
886 | 0 | OS << "In module '" << ModuleName << "' imported from " |
887 | 0 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; |
888 | 0 | else |
889 | 0 | OS << "In module '" << ModuleName << "':\n"; |
890 | 0 | } |
891 | | |
892 | | void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc, |
893 | | PresumedLoc PLoc, |
894 | 0 | StringRef ModuleName) { |
895 | 0 | if (DiagOpts->ShowLocation && PLoc.isValid()) |
896 | 0 | OS << "While building module '" << ModuleName << "' imported from " |
897 | 0 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; |
898 | 0 | else |
899 | 0 | OS << "While building module '" << ModuleName << "':\n"; |
900 | 0 | } |
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 | 0 | const SourceManager &SM) { |
906 | 0 | if (!R.isValid()) |
907 | 0 | return std::nullopt; |
908 | | |
909 | 0 | SourceLocation Begin = R.getBegin(); |
910 | 0 | SourceLocation End = R.getEnd(); |
911 | 0 | if (SM.getFileID(Begin) != FID || SM.getFileID(End) != FID) |
912 | 0 | return std::nullopt; |
913 | | |
914 | 0 | return std::make_pair(SM.getExpansionLineNumber(Begin), |
915 | 0 | SM.getExpansionLineNumber(End)); |
916 | 0 | } |
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 | 0 | unsigned MaxRange) { |
923 | | // If A is already the maximum size, we're done. |
924 | 0 | unsigned Slack = MaxRange - (A.second - A.first + 1); |
925 | 0 | if (Slack == 0) |
926 | 0 | return A; |
927 | | |
928 | | // Easy case: merge succeeds within MaxRange. |
929 | 0 | unsigned Min = std::min(A.first, B.first); |
930 | 0 | unsigned Max = std::max(A.second, B.second); |
931 | 0 | if (Max - Min + 1 <= MaxRange) |
932 | 0 | 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 | 0 | if ((B.first > A.first && B.first - A.first + 1 > MaxRange) || |
937 | 0 | (B.second < A.second && A.second - B.second + 1 > MaxRange)) |
938 | 0 | 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 | 0 | A.second = std::min(A.second + (Slack + 1) / 2, Max); |
947 | 0 | Slack = MaxRange - (A.second - A.first + 1); |
948 | 0 | A.first = std::max(Min + Slack, A.first) - Slack; |
949 | 0 | A.second = std::min(A.first + MaxRange - 1, Max); |
950 | 0 | return A; |
951 | 0 | } |
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 | 0 | std::string &CaretLine) { |
962 | | // Pick the first non-whitespace column. |
963 | 0 | unsigned StartColNo = R.StartCol; |
964 | 0 | while (StartColNo < Map.getSourceLine().size() && |
965 | 0 | (Map.getSourceLine()[StartColNo] == ' ' || |
966 | 0 | Map.getSourceLine()[StartColNo] == '\t')) |
967 | 0 | StartColNo = Map.startOfNextColumn(StartColNo); |
968 | | |
969 | | // Pick the last non-whitespace column. |
970 | 0 | unsigned EndColNo = |
971 | 0 | std::min(static_cast<size_t>(R.EndCol), Map.getSourceLine().size()); |
972 | 0 | while (EndColNo && (Map.getSourceLine()[EndColNo - 1] == ' ' || |
973 | 0 | Map.getSourceLine()[EndColNo - 1] == '\t')) |
974 | 0 | 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 | 0 | if (StartColNo > EndColNo) |
980 | 0 | return; |
981 | | |
982 | | // Fill the range with ~'s. |
983 | 0 | StartColNo = Map.byteToContainingColumn(StartColNo); |
984 | 0 | EndColNo = Map.byteToContainingColumn(EndColNo); |
985 | |
|
986 | 0 | assert(StartColNo <= EndColNo && "Invalid range!"); |
987 | 0 | if (CaretLine.size() < EndColNo) |
988 | 0 | CaretLine.resize(EndColNo, ' '); |
989 | 0 | std::fill(CaretLine.begin() + StartColNo, CaretLine.begin() + EndColNo, '~'); |
990 | 0 | } |
991 | | |
992 | | static std::string buildFixItInsertionLine(FileID FID, |
993 | | unsigned LineNo, |
994 | | const SourceColumnMap &map, |
995 | | ArrayRef<FixItHint> Hints, |
996 | | const SourceManager &SM, |
997 | 0 | const DiagnosticOptions *DiagOpts) { |
998 | 0 | std::string FixItInsertionLine; |
999 | 0 | if (Hints.empty() || !DiagOpts->ShowFixits) |
1000 | 0 | return FixItInsertionLine; |
1001 | 0 | unsigned PrevHintEndCol = 0; |
1002 | |
|
1003 | 0 | for (const auto &H : Hints) { |
1004 | 0 | if (H.CodeToInsert.empty()) |
1005 | 0 | 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 | 0 | std::pair<FileID, unsigned> HintLocInfo = |
1010 | 0 | SM.getDecomposedExpansionLoc(H.RemoveRange.getBegin()); |
1011 | 0 | if (FID == HintLocInfo.first && |
1012 | 0 | LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) && |
1013 | 0 | 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 | 0 | unsigned HintByteOffset = |
1020 | 0 | SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1; |
1021 | | |
1022 | | // The hint must start inside the source or right at the end |
1023 | 0 | assert(HintByteOffset < static_cast<unsigned>(map.bytes()) + 1); |
1024 | 0 | 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 | 0 | if (HintCol < PrevHintEndCol) |
1034 | 0 | HintCol = PrevHintEndCol + 1; |
1035 | | |
1036 | | // This should NOT use HintByteOffset, because the source might have |
1037 | | // Unicode characters in earlier columns. |
1038 | 0 | unsigned NewFixItLineSize = FixItInsertionLine.size() + |
1039 | 0 | (HintCol - PrevHintEndCol) + |
1040 | 0 | H.CodeToInsert.size(); |
1041 | 0 | if (NewFixItLineSize > FixItInsertionLine.size()) |
1042 | 0 | FixItInsertionLine.resize(NewFixItLineSize, ' '); |
1043 | |
|
1044 | 0 | std::copy(H.CodeToInsert.begin(), H.CodeToInsert.end(), |
1045 | 0 | FixItInsertionLine.end() - H.CodeToInsert.size()); |
1046 | |
|
1047 | 0 | PrevHintEndCol = HintCol + llvm::sys::locale::columnWidth(H.CodeToInsert); |
1048 | 0 | } |
1049 | 0 | } |
1050 | |
|
1051 | 0 | expandTabs(FixItInsertionLine, DiagOpts->TabStop); |
1052 | |
|
1053 | 0 | return FixItInsertionLine; |
1054 | 0 | } |
1055 | | |
1056 | 0 | static unsigned getNumDisplayWidth(unsigned N) { |
1057 | 0 | unsigned L = 1u, M = 10u; |
1058 | 0 | while (M <= N && ++L != std::numeric_limits<unsigned>::digits10 + 1) |
1059 | 0 | M *= 10u; |
1060 | |
|
1061 | 0 | return L; |
1062 | 0 | } |
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 | 0 | const LangOptions &LangOpts) { |
1074 | 0 | SmallVector<LineRange> LineRanges; |
1075 | |
|
1076 | 0 | for (const CharSourceRange &R : Ranges) { |
1077 | 0 | if (R.isInvalid()) |
1078 | 0 | continue; |
1079 | 0 | SourceLocation Begin = R.getBegin(); |
1080 | 0 | SourceLocation End = R.getEnd(); |
1081 | |
|
1082 | 0 | unsigned StartLineNo = SM.getExpansionLineNumber(Begin); |
1083 | 0 | if (StartLineNo > Lines.second || SM.getFileID(Begin) != FID) |
1084 | 0 | continue; |
1085 | | |
1086 | 0 | unsigned EndLineNo = SM.getExpansionLineNumber(End); |
1087 | 0 | if (EndLineNo < Lines.first || SM.getFileID(End) != FID) |
1088 | 0 | continue; |
1089 | | |
1090 | 0 | unsigned StartColumn = SM.getExpansionColumnNumber(Begin); |
1091 | 0 | unsigned EndColumn = SM.getExpansionColumnNumber(End); |
1092 | 0 | if (R.isTokenRange()) |
1093 | 0 | EndColumn += Lexer::MeasureTokenLength(End, SM, LangOpts); |
1094 | | |
1095 | | // Only a single line. |
1096 | 0 | if (StartLineNo == EndLineNo) { |
1097 | 0 | LineRanges.push_back({StartLineNo, StartColumn - 1, EndColumn - 1}); |
1098 | 0 | continue; |
1099 | 0 | } |
1100 | | |
1101 | | // Start line. |
1102 | 0 | LineRanges.push_back({StartLineNo, StartColumn - 1, ~0u}); |
1103 | | |
1104 | | // Middle lines. |
1105 | 0 | for (unsigned S = StartLineNo + 1; S != EndLineNo; ++S) |
1106 | 0 | LineRanges.push_back({S, 0, ~0u}); |
1107 | | |
1108 | | // End line. |
1109 | 0 | LineRanges.push_back({EndLineNo, 0, EndColumn - 1}); |
1110 | 0 | } |
1111 | |
|
1112 | 0 | return LineRanges; |
1113 | 0 | } |
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 | 0 | SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) { |
1125 | 0 | assert(Loc.isValid() && "must have a valid source location here"); |
1126 | 0 | 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 | 0 | if (!DiagOpts->ShowCarets) |
1135 | 0 | return; |
1136 | 0 | if (Loc == LastLoc && Ranges.empty() && Hints.empty() && |
1137 | 0 | (LastLevel != DiagnosticsEngine::Note || Level == LastLevel)) |
1138 | 0 | return; |
1139 | | |
1140 | 0 | FileID FID = Loc.getFileID(); |
1141 | 0 | const SourceManager &SM = Loc.getManager(); |
1142 | | |
1143 | | // Get information about the buffer it points into. |
1144 | 0 | bool Invalid = false; |
1145 | 0 | StringRef BufData = Loc.getBufferData(&Invalid); |
1146 | 0 | if (Invalid) |
1147 | 0 | return; |
1148 | 0 | const char *BufStart = BufData.data(); |
1149 | 0 | const char *BufEnd = BufStart + BufData.size(); |
1150 | |
|
1151 | 0 | unsigned CaretLineNo = Loc.getLineNumber(); |
1152 | 0 | unsigned CaretColNo = Loc.getColumnNumber(); |
1153 | | |
1154 | | // Arbitrarily stop showing snippets when the line is too long. |
1155 | 0 | static const size_t MaxLineLengthToPrint = 4096; |
1156 | 0 | if (CaretColNo > MaxLineLengthToPrint) |
1157 | 0 | return; |
1158 | | |
1159 | | // Find the set of lines to include. |
1160 | 0 | const unsigned MaxLines = DiagOpts->SnippetLineLimit; |
1161 | 0 | std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo}; |
1162 | 0 | unsigned DisplayLineNo = Loc.getPresumedLoc().getLine(); |
1163 | 0 | for (const auto &I : Ranges) { |
1164 | 0 | if (auto OptionalRange = findLinesForRange(I, FID, SM)) |
1165 | 0 | Lines = maybeAddRange(Lines, *OptionalRange, MaxLines); |
1166 | |
|
1167 | 0 | DisplayLineNo = |
1168 | 0 | std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin())); |
1169 | 0 | } |
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 | 0 | unsigned MaxLineNoDisplayWidth = |
1176 | 0 | DiagOpts->ShowLineNumbers |
1177 | 0 | ? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines)) |
1178 | 0 | : 0; |
1179 | 0 | auto indentForLineNumbers = [&] { |
1180 | 0 | if (MaxLineNoDisplayWidth > 0) |
1181 | 0 | OS.indent(MaxLineNoDisplayWidth + 2) << "| "; |
1182 | 0 | }; |
1183 | |
|
1184 | 0 | SmallVector<LineRange> LineRanges = |
1185 | 0 | prepareAndFilterRanges(Ranges, SM, Lines, FID, LangOpts); |
1186 | |
|
1187 | 0 | for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1; |
1188 | 0 | ++LineNo, ++DisplayLineNo) { |
1189 | | // Rewind from the current position to the start of the line. |
1190 | 0 | const char *LineStart = |
1191 | 0 | BufStart + |
1192 | 0 | SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second; |
1193 | 0 | if (LineStart == BufEnd) |
1194 | 0 | break; |
1195 | | |
1196 | | // Compute the line end. |
1197 | 0 | const char *LineEnd = LineStart; |
1198 | 0 | while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd) |
1199 | 0 | ++LineEnd; |
1200 | | |
1201 | | // Arbitrarily stop showing snippets when the line is too long. |
1202 | | // FIXME: Don't print any lines in this case. |
1203 | 0 | if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint) |
1204 | 0 | return; |
1205 | | |
1206 | | // Copy the line of code into an std::string for ease of manipulation. |
1207 | 0 | std::string SourceLine(LineStart, LineEnd); |
1208 | | // Remove trailing null bytes. |
1209 | 0 | while (!SourceLine.empty() && SourceLine.back() == '\0' && |
1210 | 0 | (LineNo != CaretLineNo || SourceLine.size() > CaretColNo)) |
1211 | 0 | SourceLine.pop_back(); |
1212 | | |
1213 | | // Build the byte to column map. |
1214 | 0 | const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); |
1215 | |
|
1216 | 0 | std::string CaretLine; |
1217 | | // Highlight all of the characters covered by Ranges with ~ characters. |
1218 | 0 | for (const auto &LR : LineRanges) { |
1219 | 0 | if (LR.LineNo == LineNo) |
1220 | 0 | highlightRange(LR, sourceColMap, CaretLine); |
1221 | 0 | } |
1222 | | |
1223 | | // Next, insert the caret itself. |
1224 | 0 | if (CaretLineNo == LineNo) { |
1225 | 0 | size_t Col = sourceColMap.byteToContainingColumn(CaretColNo - 1); |
1226 | 0 | CaretLine.resize(std::max(Col + 1, CaretLine.size()), ' '); |
1227 | 0 | CaretLine[Col] = '^'; |
1228 | 0 | } |
1229 | |
|
1230 | 0 | std::string FixItInsertionLine = buildFixItInsertionLine( |
1231 | 0 | 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 | 0 | unsigned Columns = DiagOpts->MessageLength; |
1236 | 0 | if (Columns) |
1237 | 0 | selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, |
1238 | 0 | 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 | 0 | if (DiagOpts->ShowSourceRanges && !SourceLine.empty()) { |
1245 | 0 | SourceLine = ' ' + SourceLine; |
1246 | 0 | CaretLine = ' ' + CaretLine; |
1247 | 0 | } |
1248 | | |
1249 | | // Emit what we have computed. |
1250 | 0 | emitSnippet(SourceLine, MaxLineNoDisplayWidth, DisplayLineNo); |
1251 | |
|
1252 | 0 | if (!CaretLine.empty()) { |
1253 | 0 | indentForLineNumbers(); |
1254 | 0 | if (DiagOpts->ShowColors) |
1255 | 0 | OS.changeColor(caretColor, true); |
1256 | 0 | OS << CaretLine << '\n'; |
1257 | 0 | if (DiagOpts->ShowColors) |
1258 | 0 | OS.resetColor(); |
1259 | 0 | } |
1260 | |
|
1261 | 0 | if (!FixItInsertionLine.empty()) { |
1262 | 0 | indentForLineNumbers(); |
1263 | 0 | if (DiagOpts->ShowColors) |
1264 | | // Print fixit line in color |
1265 | 0 | OS.changeColor(fixitColor, false); |
1266 | 0 | if (DiagOpts->ShowSourceRanges) |
1267 | 0 | OS << ' '; |
1268 | 0 | OS << FixItInsertionLine << '\n'; |
1269 | 0 | if (DiagOpts->ShowColors) |
1270 | 0 | OS.resetColor(); |
1271 | 0 | } |
1272 | 0 | } |
1273 | | |
1274 | | // Print out any parseable fixit information requested by the options. |
1275 | 0 | emitParseableFixits(Hints, SM); |
1276 | 0 | } |
1277 | | |
1278 | | void TextDiagnostic::emitSnippet(StringRef SourceLine, |
1279 | | unsigned MaxLineNoDisplayWidth, |
1280 | 0 | unsigned LineNo) { |
1281 | | // Emit line number. |
1282 | 0 | if (MaxLineNoDisplayWidth > 0) { |
1283 | 0 | unsigned LineNoDisplayWidth = getNumDisplayWidth(LineNo); |
1284 | 0 | OS.indent(MaxLineNoDisplayWidth - LineNoDisplayWidth + 1) |
1285 | 0 | << LineNo << " | "; |
1286 | 0 | } |
1287 | | |
1288 | | // Print the source line one character at a time. |
1289 | 0 | bool PrintReversed = false; |
1290 | 0 | size_t I = 0; |
1291 | 0 | while (I < SourceLine.size()) { |
1292 | 0 | auto [Str, WasPrintable] = |
1293 | 0 | printableTextForNextCharacter(SourceLine, &I, DiagOpts->TabStop); |
1294 | | |
1295 | | // Toggle inverted colors on or off for this character. |
1296 | 0 | if (DiagOpts->ShowColors) { |
1297 | 0 | if (WasPrintable == PrintReversed) { |
1298 | 0 | PrintReversed = !PrintReversed; |
1299 | 0 | if (PrintReversed) |
1300 | 0 | OS.reverseColor(); |
1301 | 0 | else |
1302 | 0 | OS.resetColor(); |
1303 | 0 | } |
1304 | 0 | } |
1305 | 0 | OS << Str; |
1306 | 0 | } |
1307 | |
|
1308 | 0 | if (DiagOpts->ShowColors) |
1309 | 0 | OS.resetColor(); |
1310 | |
|
1311 | 0 | OS << '\n'; |
1312 | 0 | } |
1313 | | |
1314 | | void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints, |
1315 | 0 | const SourceManager &SM) { |
1316 | 0 | if (!DiagOpts->ShowParseableFixits) |
1317 | 0 | return; |
1318 | | |
1319 | | // We follow FixItRewriter's example in not (yet) handling |
1320 | | // fix-its in macros. |
1321 | 0 | for (const auto &H : Hints) { |
1322 | 0 | if (H.RemoveRange.isInvalid() || H.RemoveRange.getBegin().isMacroID() || |
1323 | 0 | H.RemoveRange.getEnd().isMacroID()) |
1324 | 0 | return; |
1325 | 0 | } |
1326 | | |
1327 | 0 | for (const auto &H : Hints) { |
1328 | 0 | SourceLocation BLoc = H.RemoveRange.getBegin(); |
1329 | 0 | SourceLocation ELoc = H.RemoveRange.getEnd(); |
1330 | |
|
1331 | 0 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc); |
1332 | 0 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc); |
1333 | | |
1334 | | // Adjust for token ranges. |
1335 | 0 | if (H.RemoveRange.isTokenRange()) |
1336 | 0 | 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 | 0 | PresumedLoc PLoc = SM.getPresumedLoc(BLoc); |
1341 | 0 | if (PLoc.isInvalid()) |
1342 | 0 | break; |
1343 | | |
1344 | 0 | OS << "fix-it:\""; |
1345 | 0 | OS.write_escaped(PLoc.getFilename()); |
1346 | 0 | OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second) |
1347 | 0 | << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) |
1348 | 0 | << '-' << SM.getLineNumber(EInfo.first, EInfo.second) |
1349 | 0 | << ':' << SM.getColumnNumber(EInfo.first, EInfo.second) |
1350 | 0 | << "}:\""; |
1351 | 0 | OS.write_escaped(H.CodeToInsert); |
1352 | 0 | OS << "\"\n"; |
1353 | 0 | } |
1354 | 0 | } |