Skip to content

Commit bb6391a

Browse files
committed
[clang] Make ellipse strings constexpr
And use sizeof() so we can avoid a potential runtime call to strlen. Also rename map to Map, remove the m_ prefix from member variables and fix the naming of the existing color variables.
1 parent 67db5fd commit bb6391a

File tree

1 file changed

+86
-87
lines changed

1 file changed

+86
-87
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 86 additions & 87 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,69 +283,65 @@ 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));
332-
N = N.next();
325+
assert(0 <= N.V && N.V < static_cast<int>(ByteToColumn.size() - 1));
333326
while (!byteToColumn(N).isValid())
334327
N = N.next();
335328
return N;
336329
}
337330

338331
/// Map from a byte index to the previous byte which starts a column.
339332
Bytes startOfPreviousColumn(Bytes N) const {
340-
assert(0 < N.V && N.V < static_cast<int>(m_byteToColumn.size()));
341-
N = N.prev();
333+
assert(0 < N.V && N.V < static_cast<int>(ByteToColumn.size()));
342334
while (!byteToColumn(N).isValid())
343335
N = N.prev();
344336
return N;
345337
}
346338

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

351341
private:
352-
StringRef m_SourceLine;
353-
SmallVector<Columns, 200> m_byteToColumn;
354-
SmallVector<Bytes, 200> m_columnToByte;
342+
StringRef SourceLine;
343+
SmallVector<Columns, 200> ByteToColumn;
344+
SmallVector<Bytes, 200> ColumnToByte;
355345
};
356346
} // end anonymous namespace
357347

@@ -361,12 +351,12 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
361351
std::string &CaretLine,
362352
std::string &FixItInsertionLine,
363353
Columns NonGutterColumns,
364-
const SourceColumnMap &map) {
354+
const SourceColumnMap &Map) {
365355
Columns CaretColumns = Columns(CaretLine.size());
366356
Columns FixItColumns =
367357
Columns(llvm::sys::locale::columnWidth(FixItInsertionLine));
368358
Columns MaxColumns =
369-
std::max({map.columns().V, CaretColumns.V, FixItColumns.V});
359+
std::max({Map.columns().V, CaretColumns.V, FixItColumns.V});
370360
// if the number of columns is less than the desired number we're done
371361
if (MaxColumns <= NonGutterColumns)
372362
return;
@@ -415,14 +405,14 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
415405
// CaretEnd may have been set at the middle of a character
416406
// If it's not at a character's first column then advance it past the current
417407
// character.
418-
while (CaretEnd < map.columns() && !map.columnToByte(CaretEnd).isValid())
408+
while (CaretEnd < Map.columns() && !Map.columnToByte(CaretEnd).isValid())
419409
CaretEnd = CaretEnd.next();
420410

421411
assert(
422-
(CaretStart > map.columns() || map.columnToByte(CaretStart).isValid()) &&
412+
(CaretStart > Map.columns() || Map.columnToByte(CaretStart).isValid()) &&
423413
"CaretStart must not point to a column in the middle of a source"
424414
" line character");
425-
assert((CaretEnd > map.columns() || map.columnToByte(CaretEnd).isValid()) &&
415+
assert((CaretEnd > Map.columns() || Map.columnToByte(CaretEnd).isValid()) &&
426416
"CaretEnd must not point to a column in the middle of a source line"
427417
" character");
428418

@@ -431,20 +421,19 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
431421
// number of columns we have, try to grow the slice to encompass
432422
// more context.
433423

434-
Bytes SourceStart = map.columnToByte(std::min(CaretStart.V, map.columns().V));
435-
Bytes SourceEnd = map.columnToByte(std::min(CaretEnd.V, map.columns().V));
424+
Bytes SourceStart = Map.columnToByte(std::min(CaretStart.V, Map.columns().V));
425+
Bytes SourceEnd = Map.columnToByte(std::min(CaretEnd.V, Map.columns().V));
436426

437427
Columns CaretColumnsOutsideSource =
438428
CaretEnd - CaretStart -
439-
(map.byteToColumn(SourceEnd) - map.byteToColumn(SourceStart));
429+
(Map.byteToColumn(SourceEnd) - Map.byteToColumn(SourceStart));
440430

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));
431+
constexpr StringRef FrontEllipse = " ...";
432+
constexpr StringRef FrontSpace = " ";
433+
constexpr StringRef BackEllipse = "...";
434+
Columns EllipsesColumns = Columns(FrontEllipse.size() + BackEllipse.size());
446435

447-
Columns TargetColumns = Columns(NonGutterColumns);
436+
Columns TargetColumns = NonGutterColumns;
448437
// Give us extra room for the ellipses
449438
// and any of the caret line that extends past the source
450439
if (TargetColumns > EllipsesColumns + CaretColumnsOutsideSource)
@@ -454,47 +443,47 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
454443
bool ExpandedRegion = false;
455444

456445
if (SourceStart > 0) {
457-
Bytes NewStart = map.startOfPreviousColumn(SourceStart);
446+
Bytes NewStart = Map.startOfPreviousColumn(SourceStart);
458447

459448
// Skip over any whitespace we see here; we're looking for
460449
// another bit of interesting text.
461450
// FIXME: Detect non-ASCII whitespace characters too.
462451
while (NewStart > 0 && isWhitespace(SourceLine[NewStart.V]))
463-
NewStart = map.startOfPreviousColumn(NewStart);
452+
NewStart = Map.startOfPreviousColumn(NewStart);
464453

465454
// Skip over this bit of "interesting" text.
466455
while (NewStart > 0) {
467-
Bytes Prev = map.startOfPreviousColumn(NewStart);
456+
Bytes Prev = Map.startOfPreviousColumn(NewStart);
468457
if (isWhitespace(SourceLine[Prev.V]))
469458
break;
470459
NewStart = Prev;
471460
}
472461

473-
assert(map.byteToColumn(NewStart).isValid());
462+
assert(Map.byteToColumn(NewStart).isValid());
474463
Columns NewColumns =
475-
map.byteToColumn(SourceEnd) - map.byteToColumn(NewStart);
464+
Map.byteToColumn(SourceEnd) - Map.byteToColumn(NewStart);
476465
if (NewColumns <= TargetColumns) {
477466
SourceStart = NewStart;
478467
ExpandedRegion = true;
479468
}
480469
}
481470

482471
if (SourceEnd < SourceLine.size()) {
483-
Bytes NewEnd = map.startOfNextColumn(SourceEnd);
472+
Bytes NewEnd = Map.startOfNextColumn(SourceEnd);
484473

485474
// Skip over any whitespace we see here; we're looking for
486475
// another bit of interesting text.
487476
// FIXME: Detect non-ASCII whitespace characters too.
488477
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd.V]))
489-
NewEnd = map.startOfNextColumn(NewEnd);
478+
NewEnd = Map.startOfNextColumn(NewEnd);
490479

491480
// Skip over this bit of "interesting" text.
492481
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd.V]))
493-
NewEnd = map.startOfNextColumn(NewEnd);
482+
NewEnd = Map.startOfNextColumn(NewEnd);
494483

495-
assert(map.byteToColumn(NewEnd).isValid());
484+
assert(Map.byteToColumn(NewEnd).isValid());
496485
Columns NewColumns =
497-
map.byteToColumn(NewEnd) - map.byteToColumn(SourceStart);
486+
Map.byteToColumn(NewEnd) - Map.byteToColumn(SourceStart);
498487
if (NewColumns <= TargetColumns) {
499488
SourceEnd = NewEnd;
500489
ExpandedRegion = true;
@@ -505,8 +494,8 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
505494
break;
506495
}
507496

508-
CaretStart = map.byteToColumn(SourceStart);
509-
CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
497+
CaretStart = Map.byteToColumn(SourceStart);
498+
CaretEnd = Map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
510499

511500
// [CaretStart, CaretEnd) is the slice we want. Update the various
512501
// output lines to show only this slice.
@@ -516,8 +505,8 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
516505
assert(CaretStart <= CaretEnd);
517506

518507
Columns BackColumnsRemoved =
519-
map.byteToColumn(Bytes{static_cast<int>(SourceLine.size())}) -
520-
map.byteToColumn(SourceEnd);
508+
Map.byteToColumn(Bytes{static_cast<int>(SourceLine.size())}) -
509+
Map.byteToColumn(SourceEnd);
521510
Columns FrontColumnsRemoved = CaretStart;
522511
Columns ColumnsKept = CaretEnd - CaretStart;
523512

@@ -527,19 +516,19 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
527516

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

533522
// If that's enough then we're done
534523
if (FrontColumnsRemoved + ColumnsKept <= Columns(NonGutterColumns))
535524
return;
536525

537526
// 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);
527+
if (FrontColumnsRemoved > Columns(FrontEllipse.size())) {
528+
SourceLine.replace(0, SourceStart.V, FrontEllipse);
529+
CaretLine.replace(0, CaretStart.V, FrontSpace);
541530
if (!FixItInsertionLine.empty())
542-
FixItInsertionLine.replace(0, CaretStart.V, front_space);
531+
FixItInsertionLine.replace(0, CaretStart.V, FrontSpace);
543532
}
544533
}
545534

@@ -733,11 +722,21 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
733722
switch (Level) {
734723
case DiagnosticsEngine::Ignored:
735724
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;
725+
case DiagnosticsEngine::Note:
726+
OS.changeColor(NoteColor, true);
727+
break;
728+
case DiagnosticsEngine::Remark:
729+
OS.changeColor(RemarkColor, true);
730+
break;
731+
case DiagnosticsEngine::Warning:
732+
OS.changeColor(WarningColor, true);
733+
break;
734+
case DiagnosticsEngine::Error:
735+
OS.changeColor(ErrorColor, true);
736+
break;
737+
case DiagnosticsEngine::Fatal:
738+
OS.changeColor(FatalColor, true);
739+
break;
741740
}
742741
}
743742

@@ -765,7 +764,7 @@ void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
765764
if (ShowColors && !IsSupplemental) {
766765
// Print primary diagnostic messages in bold and without color, to visually
767766
// indicate the transition from continuation notes and other output.
768-
OS.changeColor(savedColor, true);
767+
OS.changeColor(SavedColor, true);
769768
Bold = true;
770769
}
771770

@@ -843,7 +842,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
843842
return;
844843

845844
if (DiagOpts.ShowColors)
846-
OS.changeColor(savedColor, true);
845+
OS.changeColor(SavedColor, true);
847846

848847
emitFilename(PLoc.getFilename(), Loc.getManager());
849848
switch (DiagOpts.getFormat()) {
@@ -1470,7 +1469,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14701469
if (!CaretLine.empty()) {
14711470
indentForLineNumbers();
14721471
if (DiagOpts.ShowColors)
1473-
OS.changeColor(caretColor, true);
1472+
OS.changeColor(CaretColor, true);
14741473
OS << CaretLine << '\n';
14751474
if (DiagOpts.ShowColors)
14761475
OS.resetColor();
@@ -1480,7 +1479,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14801479
indentForLineNumbers();
14811480
if (DiagOpts.ShowColors)
14821481
// Print fixit line in color
1483-
OS.changeColor(fixitColor, false);
1482+
OS.changeColor(FixitColor, false);
14841483
if (DiagOpts.ShowSourceRanges)
14851484
OS << ' ';
14861485
OS << FixItInsertionLine << '\n';

0 commit comments

Comments
 (0)