Skip to content

Commit 96feee4

Browse files
authored
[clang][NFC] Make ellipse strings constexpr (#165680)
Also rename map to Map, remove the m_ prefix from member variables and fix the naming of the existing color variables.
1 parent 932fa0e commit 96feee4

File tree

1 file changed

+86
-85
lines changed

1 file changed

+86
-85
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@
2222

2323
using namespace clang;
2424

25-
static const enum raw_ostream::Colors noteColor = raw_ostream::CYAN;
26-
static const enum raw_ostream::Colors remarkColor =
27-
raw_ostream::BLUE;
28-
static const enum raw_ostream::Colors fixitColor =
29-
raw_ostream::GREEN;
30-
static const enum raw_ostream::Colors caretColor =
31-
raw_ostream::GREEN;
32-
static const enum raw_ostream::Colors warningColor =
33-
raw_ostream::MAGENTA;
34-
static const enum raw_ostream::Colors templateColor =
35-
raw_ostream::CYAN;
36-
static const enum raw_ostream::Colors errorColor = raw_ostream::RED;
37-
static const enum raw_ostream::Colors fatalColor = raw_ostream::RED;
25+
static constexpr raw_ostream::Colors NoteColor = raw_ostream::CYAN;
26+
static constexpr raw_ostream::Colors RemarkColor = raw_ostream::BLUE;
27+
static constexpr raw_ostream::Colors FixitColor = raw_ostream::GREEN;
28+
static constexpr raw_ostream::Colors CaretColor = raw_ostream::GREEN;
29+
static constexpr raw_ostream::Colors WarningColor = raw_ostream::MAGENTA;
30+
static constexpr raw_ostream::Colors TemplateColor = raw_ostream::CYAN;
31+
static constexpr raw_ostream::Colors ErrorColor = raw_ostream::RED;
32+
static constexpr raw_ostream::Colors FatalColor = raw_ostream::RED;
3833
// Used for changing only the bold attribute.
39-
static const enum raw_ostream::Colors savedColor =
40-
raw_ostream::SAVEDCOLOR;
34+
static constexpr raw_ostream::Colors SavedColor = raw_ostream::SAVEDCOLOR;
4135

4236
// Magenta is taken for 'warning'. Red is already 'error' and 'cyan'
4337
// is already taken for 'note'. Green is already used to underline
@@ -95,11 +89,11 @@ static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
9589

9690
Str = Str.substr(Pos + 1);
9791
if (Normal)
98-
OS.changeColor(templateColor, true);
92+
OS.changeColor(TemplateColor, true);
9993
else {
10094
OS.resetColor();
10195
if (Bold)
102-
OS.changeColor(savedColor, true);
96+
OS.changeColor(SavedColor, true);
10397
}
10498
Normal = !Normal;
10599
}
@@ -289,46 +283,46 @@ static void genColumnByteMapping(StringRef SourceLine, unsigned TabStop,
289283
namespace {
290284
struct SourceColumnMap {
291285
SourceColumnMap(StringRef SourceLine, unsigned TabStop)
292-
: m_SourceLine(SourceLine) {
286+
: SourceLine(SourceLine) {
293287

294-
genColumnByteMapping(SourceLine, TabStop, m_columnToByte, m_byteToColumn);
288+
genColumnByteMapping(SourceLine, TabStop, ColumnToByte, ByteToColumn);
295289

296-
assert(m_byteToColumn.size()==SourceLine.size()+1);
297-
assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size());
298-
assert(m_byteToColumn.size() ==
299-
static_cast<unsigned>(m_columnToByte.back().V + 1));
300-
assert(static_cast<unsigned>(m_byteToColumn.back().V + 1) ==
301-
m_columnToByte.size());
290+
assert(ByteToColumn.size() == SourceLine.size() + 1);
291+
assert(0 < ByteToColumn.size() && 0 < ColumnToByte.size());
292+
assert(ByteToColumn.size() ==
293+
static_cast<unsigned>(ColumnToByte.back().V + 1));
294+
assert(static_cast<unsigned>(ByteToColumn.back().V + 1) ==
295+
ColumnToByte.size());
302296
}
303-
Columns columns() const { return m_byteToColumn.back(); }
304-
Bytes bytes() const { return m_columnToByte.back(); }
297+
Columns columns() const { return ByteToColumn.back(); }
298+
Bytes bytes() const { return ColumnToByte.back(); }
305299

306300
/// Map a byte to the column which it is at the start of, or return -1
307301
/// if it is not at the start of a column (for a UTF-8 trailing byte).
308302
Columns byteToColumn(Bytes N) const {
309-
assert(0 <= N.V && N.V < static_cast<int>(m_byteToColumn.size()));
310-
return m_byteToColumn[N.V];
303+
assert(0 <= N.V && N.V < static_cast<int>(ByteToColumn.size()));
304+
return ByteToColumn[N.V];
311305
}
312306

313307
/// Map a byte to the first column which contains it.
314308
Columns byteToContainingColumn(Bytes N) const {
315-
assert(0 <= N.V && N.V < static_cast<int>(m_byteToColumn.size()));
316-
while (!m_byteToColumn[N.V].isValid())
309+
assert(0 <= N.V && N.V < static_cast<int>(ByteToColumn.size()));
310+
while (!ByteToColumn[N.V].isValid())
317311
--N.V;
318-
return m_byteToColumn[N.V];
312+
return ByteToColumn[N.V];
319313
}
320314

321315
/// Map a column to the byte which starts the column, or return -1 if
322316
/// the column the second or subsequent column of an expanded tab or similar
323317
/// multi-column entity.
324318
Bytes columnToByte(Columns N) const {
325-
assert(0 <= N.V && N.V < static_cast<int>(m_columnToByte.size()));
326-
return m_columnToByte[N.V];
319+
assert(0 <= N.V && N.V < static_cast<int>(ColumnToByte.size()));
320+
return ColumnToByte[N.V];
327321
}
328322

329323
/// Map from a byte index to the next byte which starts a column.
330324
Bytes startOfNextColumn(Bytes N) const {
331-
assert(0 <= N.V && N.V < static_cast<int>(m_byteToColumn.size() - 1));
325+
assert(0 <= N.V && N.V < static_cast<int>(ByteToColumn.size() - 1));
332326
N = N.next();
333327
while (!byteToColumn(N).isValid())
334328
N = N.next();
@@ -337,21 +331,19 @@ struct SourceColumnMap {
337331

338332
/// Map from a byte index to the previous byte which starts a column.
339333
Bytes startOfPreviousColumn(Bytes N) const {
340-
assert(0 < N.V && N.V < static_cast<int>(m_byteToColumn.size()));
334+
assert(0 < N.V && N.V < static_cast<int>(ByteToColumn.size()));
341335
N = N.prev();
342336
while (!byteToColumn(N).isValid())
343337
N = N.prev();
344338
return N;
345339
}
346340

347-
StringRef getSourceLine() const {
348-
return m_SourceLine;
349-
}
341+
StringRef getSourceLine() const { return SourceLine; }
350342

351343
private:
352-
StringRef m_SourceLine;
353-
SmallVector<Columns, 200> m_byteToColumn;
354-
SmallVector<Bytes, 200> m_columnToByte;
344+
StringRef SourceLine;
345+
SmallVector<Columns, 200> ByteToColumn;
346+
SmallVector<Bytes, 200> ColumnToByte;
355347
};
356348
} // end anonymous namespace
357349

@@ -361,12 +353,12 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
361353
std::string &CaretLine,
362354
std::string &FixItInsertionLine,
363355
Columns NonGutterColumns,
364-
const SourceColumnMap &map) {
356+
const SourceColumnMap &Map) {
365357
Columns CaretColumns = Columns(CaretLine.size());
366358
Columns FixItColumns =
367359
Columns(llvm::sys::locale::columnWidth(FixItInsertionLine));
368360
Columns MaxColumns =
369-
std::max({map.columns().V, CaretColumns.V, FixItColumns.V});
361+
std::max({Map.columns().V, CaretColumns.V, FixItColumns.V});
370362
// if the number of columns is less than the desired number we're done
371363
if (MaxColumns <= NonGutterColumns)
372364
return;
@@ -415,14 +407,14 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
415407
// CaretEnd may have been set at the middle of a character
416408
// If it's not at a character's first column then advance it past the current
417409
// character.
418-
while (CaretEnd < map.columns() && !map.columnToByte(CaretEnd).isValid())
410+
while (CaretEnd < Map.columns() && !Map.columnToByte(CaretEnd).isValid())
419411
CaretEnd = CaretEnd.next();
420412

421413
assert(
422-
(CaretStart > map.columns() || map.columnToByte(CaretStart).isValid()) &&
414+
(CaretStart > Map.columns() || Map.columnToByte(CaretStart).isValid()) &&
423415
"CaretStart must not point to a column in the middle of a source"
424416
" line character");
425-
assert((CaretEnd > map.columns() || map.columnToByte(CaretEnd).isValid()) &&
417+
assert((CaretEnd > Map.columns() || Map.columnToByte(CaretEnd).isValid()) &&
426418
"CaretEnd must not point to a column in the middle of a source line"
427419
" character");
428420

@@ -431,20 +423,19 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
431423
// number of columns we have, try to grow the slice to encompass
432424
// more context.
433425

434-
Bytes SourceStart = map.columnToByte(std::min(CaretStart.V, map.columns().V));
435-
Bytes SourceEnd = map.columnToByte(std::min(CaretEnd.V, map.columns().V));
426+
Bytes SourceStart = Map.columnToByte(std::min(CaretStart.V, Map.columns().V));
427+
Bytes SourceEnd = Map.columnToByte(std::min(CaretEnd.V, Map.columns().V));
436428

437429
Columns CaretColumnsOutsideSource =
438430
CaretEnd - CaretStart -
439-
(map.byteToColumn(SourceEnd) - map.byteToColumn(SourceStart));
431+
(Map.byteToColumn(SourceEnd) - Map.byteToColumn(SourceStart));
440432

441-
char const *front_ellipse = " ...";
442-
char const *front_space = " ";
443-
char const *back_ellipse = "...";
444-
Columns EllipsesColumns =
445-
Columns(strlen(front_ellipse) + strlen(back_ellipse));
433+
constexpr StringRef FrontEllipse = " ...";
434+
constexpr StringRef FrontSpace = " ";
435+
constexpr StringRef BackEllipse = "...";
436+
Columns EllipsesColumns = Columns(FrontEllipse.size() + BackEllipse.size());
446437

447-
Columns TargetColumns = Columns(NonGutterColumns);
438+
Columns TargetColumns = NonGutterColumns;
448439
// Give us extra room for the ellipses
449440
// and any of the caret line that extends past the source
450441
if (TargetColumns > EllipsesColumns + CaretColumnsOutsideSource)
@@ -454,47 +445,47 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
454445
bool ExpandedRegion = false;
455446

456447
if (SourceStart > 0) {
457-
Bytes NewStart = map.startOfPreviousColumn(SourceStart);
448+
Bytes NewStart = Map.startOfPreviousColumn(SourceStart);
458449

459450
// Skip over any whitespace we see here; we're looking for
460451
// another bit of interesting text.
461452
// FIXME: Detect non-ASCII whitespace characters too.
462453
while (NewStart > 0 && isWhitespace(SourceLine[NewStart.V]))
463-
NewStart = map.startOfPreviousColumn(NewStart);
454+
NewStart = Map.startOfPreviousColumn(NewStart);
464455

465456
// Skip over this bit of "interesting" text.
466457
while (NewStart > 0) {
467-
Bytes Prev = map.startOfPreviousColumn(NewStart);
458+
Bytes Prev = Map.startOfPreviousColumn(NewStart);
468459
if (isWhitespace(SourceLine[Prev.V]))
469460
break;
470461
NewStart = Prev;
471462
}
472463

473-
assert(map.byteToColumn(NewStart).isValid());
464+
assert(Map.byteToColumn(NewStart).isValid());
474465
Columns NewColumns =
475-
map.byteToColumn(SourceEnd) - map.byteToColumn(NewStart);
466+
Map.byteToColumn(SourceEnd) - Map.byteToColumn(NewStart);
476467
if (NewColumns <= TargetColumns) {
477468
SourceStart = NewStart;
478469
ExpandedRegion = true;
479470
}
480471
}
481472

482473
if (SourceEnd < SourceLine.size()) {
483-
Bytes NewEnd = map.startOfNextColumn(SourceEnd);
474+
Bytes NewEnd = Map.startOfNextColumn(SourceEnd);
484475

485476
// Skip over any whitespace we see here; we're looking for
486477
// another bit of interesting text.
487478
// FIXME: Detect non-ASCII whitespace characters too.
488479
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd.V]))
489-
NewEnd = map.startOfNextColumn(NewEnd);
480+
NewEnd = Map.startOfNextColumn(NewEnd);
490481

491482
// Skip over this bit of "interesting" text.
492483
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd.V]))
493-
NewEnd = map.startOfNextColumn(NewEnd);
484+
NewEnd = Map.startOfNextColumn(NewEnd);
494485

495-
assert(map.byteToColumn(NewEnd).isValid());
486+
assert(Map.byteToColumn(NewEnd).isValid());
496487
Columns NewColumns =
497-
map.byteToColumn(NewEnd) - map.byteToColumn(SourceStart);
488+
Map.byteToColumn(NewEnd) - Map.byteToColumn(SourceStart);
498489
if (NewColumns <= TargetColumns) {
499490
SourceEnd = NewEnd;
500491
ExpandedRegion = true;
@@ -505,8 +496,8 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
505496
break;
506497
}
507498

508-
CaretStart = map.byteToColumn(SourceStart);
509-
CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
499+
CaretStart = Map.byteToColumn(SourceStart);
500+
CaretEnd = Map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
510501

511502
// [CaretStart, CaretEnd) is the slice we want. Update the various
512503
// output lines to show only this slice.
@@ -516,8 +507,8 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
516507
assert(CaretStart <= CaretEnd);
517508

518509
Columns BackColumnsRemoved =
519-
map.byteToColumn(Bytes{static_cast<int>(SourceLine.size())}) -
520-
map.byteToColumn(SourceEnd);
510+
Map.byteToColumn(Bytes{static_cast<int>(SourceLine.size())}) -
511+
Map.byteToColumn(SourceEnd);
521512
Columns FrontColumnsRemoved = CaretStart;
522513
Columns ColumnsKept = CaretEnd - CaretStart;
523514

@@ -527,19 +518,19 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
527518

528519
// The line needs some truncation, and we'd prefer to keep the front
529520
// if possible, so remove the back
530-
if (BackColumnsRemoved > Columns(strlen(back_ellipse)))
531-
SourceLine.replace(SourceEnd.V, std::string::npos, back_ellipse);
521+
if (BackColumnsRemoved > Columns(BackEllipse.size()))
522+
SourceLine.replace(SourceEnd.V, std::string::npos, BackEllipse);
532523

533524
// If that's enough then we're done
534525
if (FrontColumnsRemoved + ColumnsKept <= Columns(NonGutterColumns))
535526
return;
536527

537528
// Otherwise remove the front as well
538-
if (FrontColumnsRemoved > Columns(strlen(front_ellipse))) {
539-
SourceLine.replace(0, SourceStart.V, front_ellipse);
540-
CaretLine.replace(0, CaretStart.V, front_space);
529+
if (FrontColumnsRemoved > Columns(FrontEllipse.size())) {
530+
SourceLine.replace(0, SourceStart.V, FrontEllipse);
531+
CaretLine.replace(0, CaretStart.V, FrontSpace);
541532
if (!FixItInsertionLine.empty())
542-
FixItInsertionLine.replace(0, CaretStart.V, front_space);
533+
FixItInsertionLine.replace(0, CaretStart.V, FrontSpace);
543534
}
544535
}
545536

@@ -733,11 +724,21 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
733724
switch (Level) {
734725
case DiagnosticsEngine::Ignored:
735726
llvm_unreachable("Invalid diagnostic type");
736-
case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break;
737-
case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break;
738-
case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break;
739-
case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break;
740-
case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break;
727+
case DiagnosticsEngine::Note:
728+
OS.changeColor(NoteColor, true);
729+
break;
730+
case DiagnosticsEngine::Remark:
731+
OS.changeColor(RemarkColor, true);
732+
break;
733+
case DiagnosticsEngine::Warning:
734+
OS.changeColor(WarningColor, true);
735+
break;
736+
case DiagnosticsEngine::Error:
737+
OS.changeColor(ErrorColor, true);
738+
break;
739+
case DiagnosticsEngine::Fatal:
740+
OS.changeColor(FatalColor, true);
741+
break;
741742
}
742743
}
743744

@@ -765,7 +766,7 @@ void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
765766
if (ShowColors && !IsSupplemental) {
766767
// Print primary diagnostic messages in bold and without color, to visually
767768
// indicate the transition from continuation notes and other output.
768-
OS.changeColor(savedColor, true);
769+
OS.changeColor(SavedColor, true);
769770
Bold = true;
770771
}
771772

@@ -843,7 +844,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
843844
return;
844845

845846
if (DiagOpts.ShowColors)
846-
OS.changeColor(savedColor, true);
847+
OS.changeColor(SavedColor, true);
847848

848849
emitFilename(PLoc.getFilename(), Loc.getManager());
849850
switch (DiagOpts.getFormat()) {
@@ -1470,7 +1471,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14701471
if (!CaretLine.empty()) {
14711472
indentForLineNumbers();
14721473
if (DiagOpts.ShowColors)
1473-
OS.changeColor(caretColor, true);
1474+
OS.changeColor(CaretColor, true);
14741475
OS << CaretLine << '\n';
14751476
if (DiagOpts.ShowColors)
14761477
OS.resetColor();
@@ -1480,7 +1481,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14801481
indentForLineNumbers();
14811482
if (DiagOpts.ShowColors)
14821483
// Print fixit line in color
1483-
OS.changeColor(fixitColor, false);
1484+
OS.changeColor(FixitColor, false);
14841485
if (DiagOpts.ShowSourceRanges)
14851486
OS << ' ';
14861487
OS << FixItInsertionLine << '\n';

0 commit comments

Comments
 (0)