Coverage Report

Created: 2019-07-24 05:18

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