Skip to content

Commit 9220801

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

File tree

2 files changed

+85
-19
lines changed

2 files changed

+85
-19
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 56 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
@@ -521,8 +518,41 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
521518
if (BackColumnsRemoved > Columns(BackEllipse.size()))
522519
SourceLine.replace(SourceEnd.V, std::string::npos, BackEllipse);
523520

521+
// Since we've modified the SourceLine, we also need to adjust the line's
522+
// highlighting information. In particular, if we've removed
523+
// from the front of the line, we need to move the style ranges to the
524+
// left and remove unneeded ranges.
525+
Columns FrontDiff = FrontColumnsRemoved > FrontEllipse.size()
526+
? FrontColumnsRemoved - FrontEllipse.size()
527+
: 0;
528+
Columns CodeStart = FrontColumnsRemoved > FrontEllipse.size()
529+
? FrontEllipse.size()
530+
: FrontColumnsRemoved;
531+
Bytes CodeEnd = Map.columnToByte(CodeStart + ColumnsKept);
532+
533+
for (auto &R : Styles) {
534+
// Style ranges too far left. Just move them where they don't bother.
535+
if (R.End < static_cast<unsigned>(FrontDiff.V)) {
536+
R.Start = R.End = std::numeric_limits<int>::max();
537+
continue;
538+
}
539+
// Move them left. (Note that this can wrap R.Start, but that doesn't
540+
// matter).
541+
R.Start -= FrontDiff.V;
542+
R.End -= FrontDiff.V;
543+
544+
// If the range overlaps the end of the code, don't leak into the back
545+
// ellipse.
546+
if (R.Start < static_cast<unsigned>(CodeEnd.V) &&
547+
R.End > static_cast<unsigned>(CodeEnd.V))
548+
R.End = CodeEnd.V;
549+
// Don't leak into the ellipse at the end.
550+
if (R.Start >= static_cast<unsigned>(CodeEnd.V))
551+
R.Start = R.End = std::numeric_limits<int>::max();
552+
}
553+
524554
// If that's enough then we're done
525-
if (FrontColumnsRemoved + ColumnsKept <= Columns(NonGutterColumns))
555+
if (FrontColumnsRemoved + ColumnsKept <= NonGutterColumns)
526556
return;
527557

528558
// Otherwise remove the front as well
@@ -1391,6 +1421,12 @@ void TextDiagnostic::emitSnippetAndCaret(
13911421
OS.indent(MaxLineNoDisplayWidth + 2) << "| ";
13921422
};
13931423

1424+
Columns MessageLength = DiagOpts.MessageLength;
1425+
1426+
// If we don't have enough columns available, just abort now.
1427+
if (MessageLength != 0 && MessageLength <= Columns(MaxLineNoDisplayWidth + 4))
1428+
return;
1429+
13941430
// Prepare source highlighting information for the lines we're about to
13951431
// emit, starting from the first line.
13961432
std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
@@ -1450,10 +1486,10 @@ void TextDiagnostic::emitSnippetAndCaret(
14501486

14511487
// If the source line is too long for our terminal, select only the
14521488
// "interesting" source region within that line.
1453-
Columns MessageLength = DiagOpts.MessageLength;
1454-
if (MessageLength.V != 0)
1489+
if (MessageLength != 0)
14551490
selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
1456-
MessageLength, SourceColMap);
1491+
MessageLength, SourceColMap,
1492+
SourceStyles[LineNo - Lines.first]);
14571493

14581494
// If we are in -fdiagnostics-print-source-range-info mode, we are trying
14591495
// to produce easily machine parsable output. Add a space before the
@@ -1508,8 +1544,9 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
15081544
// Print the source line one character at a time.
15091545
bool PrintReversed = false;
15101546
std::optional<llvm::raw_ostream::Colors> CurrentColor;
1511-
size_t I = 0;
1547+
size_t I = 0; // Bytes.
15121548
while (I < SourceLine.size()) {
1549+
// llvm::errs() << "I: " <<I << '\n';
15131550
auto [Str, WasPrintable] =
15141551
printableTextForNextCharacter(SourceLine, &I, DiagOpts.TabStop);
15151552

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
11+
a + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
12+
// CHECK: use of undeclared identifier
13+
// 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]] ...
14+
15+
16+
/*😂😂😂*/ a + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
17+
// CHECK: use of undeclared identifier
18+
// CHECK-NEXT: [[YELLOW:.\[0;33m]]/*😂😂😂*/[[RESET]] a + [[GREEN:.\[0;32m]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] + [[GREEN]]1[[RESET]] ...
19+
20+
21+
22+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
23+
// CHECK: [[GREEN:.\[0;32m]]"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[[RESET]];
24+
25+
"😂xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
26+
// CHECK: [[GREEN:.\[0;32m]]"😂xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[[RESET]];
27+
}
28+
29+

0 commit comments

Comments
 (0)