Skip to content

Commit a1bcecb

Browse files
committed
[clang] Adjust TextDiagnostic style ranges for interesting source region
1 parent 96feee4 commit a1bcecb

File tree

2 files changed

+81
-19
lines changed

2 files changed

+81
-19
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,13 @@ struct SourceColumnMap {
349349

350350
/// When the source code line we want to print is too long for
351351
/// the terminal, select the "interesting" region.
352-
static void selectInterestingSourceRegion(std::string &SourceLine,
353-
std::string &CaretLine,
354-
std::string &FixItInsertionLine,
355-
Columns NonGutterColumns,
356-
const SourceColumnMap &Map) {
357-
Columns CaretColumns = Columns(CaretLine.size());
358-
Columns FixItColumns =
359-
Columns(llvm::sys::locale::columnWidth(FixItInsertionLine));
352+
static void selectInterestingSourceRegion(
353+
std::string &SourceLine, std::string &CaretLine,
354+
std::string &FixItInsertionLine, Columns NonGutterColumns,
355+
const SourceColumnMap &Map,
356+
SmallVector<clang::TextDiagnostic::StyleRange> &Styles) {
357+
Columns CaretColumns = CaretLine.size();
358+
Columns FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine);
360359
Columns MaxColumns =
361360
std::max({Map.columns().V, CaretColumns.V, FixItColumns.V});
362361
// if the number of columns is less than the desired number we're done
@@ -369,13 +368,11 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
369368
// Find the slice that we need to display the full caret line
370369
// correctly.
371370
Columns CaretStart = 0, CaretEnd = CaretLine.size();
372-
for (; CaretStart != CaretEnd; CaretStart = CaretStart.next())
373-
if (!isWhitespace(CaretLine[CaretStart.V]))
374-
break;
371+
while (CaretStart != CaretEnd && isWhitespace(CaretLine[CaretStart.V]))
372+
CaretStart = CaretStart.next();
375373

376-
for (; CaretEnd != CaretStart; CaretEnd = CaretEnd.prev())
377-
if (!isWhitespace(CaretLine[CaretEnd.V - 1]))
378-
break;
374+
while (CaretEnd != CaretStart && isWhitespace(CaretLine[CaretEnd.V]))
375+
CaretEnd = CaretEnd.prev();
379376

380377
// caret has already been inserted into CaretLine so the above whitespace
381378
// check is guaranteed to include the caret
@@ -516,13 +513,42 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
516513
assert(FrontColumnsRemoved + ColumnsKept + BackColumnsRemoved >
517514
NonGutterColumns);
518515

516+
// Since we've modified the SourceLine, we also need to adjust the line's
517+
// highlighting information. In particular, if we've removed
518+
// from the front of the line, we need to move the style ranges to the
519+
// left and remove unneeded ranges.
520+
Bytes BytesRemoved =
521+
FrontColumnsRemoved > FrontEllipse.size()
522+
? (Map.columnToByte(FrontColumnsRemoved) - Bytes(FrontEllipse.size()))
523+
: 0;
524+
Bytes CodeEnd = Map.columnToByte(CaretEnd);
525+
for (auto &R : Styles) {
526+
if (R.End < static_cast<unsigned>(BytesRemoved.V)) {
527+
R.Start = R.End = std::numeric_limits<int>::max();
528+
continue;
529+
}
530+
// Move them left. (Note that this can wrap R.Start, but that doesn't
531+
// matter).
532+
R.Start -= BytesRemoved.V;
533+
R.End -= BytesRemoved.V;
534+
535+
// Don't leak into the ellipse at the end.
536+
if (R.Start < static_cast<unsigned>(CodeEnd.V) &&
537+
R.End > static_cast<unsigned>(CodeEnd.V))
538+
R.End = CodeEnd.V;
539+
540+
// Remove style ranges after the end of the snippet.
541+
if (R.Start >= static_cast<unsigned>(CodeEnd.V))
542+
R.Start = R.End = std::numeric_limits<int>::max();
543+
}
544+
519545
// The line needs some truncation, and we'd prefer to keep the front
520546
// if possible, so remove the back
521547
if (BackColumnsRemoved > Columns(BackEllipse.size()))
522548
SourceLine.replace(SourceEnd.V, std::string::npos, BackEllipse);
523549

524550
// If that's enough then we're done
525-
if (FrontColumnsRemoved + ColumnsKept <= Columns(NonGutterColumns))
551+
if (FrontColumnsRemoved + ColumnsKept <= NonGutterColumns)
526552
return;
527553

528554
// Otherwise remove the front as well
@@ -1391,6 +1417,12 @@ void TextDiagnostic::emitSnippetAndCaret(
13911417
OS.indent(MaxLineNoDisplayWidth + 2) << "| ";
13921418
};
13931419

1420+
Columns MessageLength = DiagOpts.MessageLength;
1421+
1422+
// If we don't have enough columns available, just abort now.
1423+
if (MessageLength != 0 && MessageLength <= Columns(MaxLineNoDisplayWidth + 4))
1424+
return;
1425+
13941426
// Prepare source highlighting information for the lines we're about to
13951427
// emit, starting from the first line.
13961428
std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
@@ -1450,10 +1482,10 @@ void TextDiagnostic::emitSnippetAndCaret(
14501482

14511483
// If the source line is too long for our terminal, select only the
14521484
// "interesting" source region within that line.
1453-
Columns MessageLength = DiagOpts.MessageLength;
1454-
if (MessageLength.V != 0)
1485+
if (MessageLength != 0)
14551486
selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
1456-
MessageLength, SourceColMap);
1487+
MessageLength, SourceColMap,
1488+
SourceStyles[LineNo - Lines.first]);
14571489

14581490
// If we are in -fdiagnostics-print-source-range-info mode, we are trying
14591491
// to produce easily machine parsable output. Add a space before the
@@ -1508,7 +1540,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
15081540
// Print the source line one character at a time.
15091541
bool PrintReversed = false;
15101542
std::optional<llvm::raw_ostream::Colors> CurrentColor;
1511-
size_t I = 0;
1543+
size_t I = 0; // Bytes.
15121544
while (I < SourceLine.size()) {
15131545
auto [Str, WasPrintable] =
15141546
printableTextForNextCharacter(SourceLine, &I, DiagOpts.TabStop);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: not %clang_cc1 %s -fmessage-length=40 -fcolor-diagnostics -fno-show-source-location -Wunused-value -o - 2>&1 | FileCheck %s
2+
3+
// REQUIRES: ansi-escape-sequences
4+
5+
int main() {
6+
1 + + if;
7+
// CHECK: expected expression
8+
// CHECK-NEXT: ...+ [[MAGENTA:.\[0;34m]]if[[RESET:.\[0m]];
9+
10+
/*😂*/1 + + if;
11+
// CHECK: expected expression
12+
// CHECK-NEXT: ...+ [[MAGENTA:.\[0;34m]]if[[RESET:.\[0m]];
13+
14+
a + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
15+
// CHECK: use of undeclared identifier
16+
// CHECK-NEXT: a + [[GREEN:.\[0;32m]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] ...
17+
18+
19+
/*😂😂😂*/ a + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
20+
// CHECK: use of undeclared identifier
21+
// CHECK-NEXT: [[YELLOW:.\[0;33m]]/*😂😂😂*/[[RESET]] a + [[GREEN:.\[0;32m]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] ...
22+
23+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
24+
// CHECK: [[GREEN:.\[0;32m]]"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[[RESET]];
25+
26+
"😂xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
27+
// CHECK: [[GREEN:.\[0;32m]]"😂xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[[RESET]];
28+
}
29+
30+

0 commit comments

Comments
 (0)