From 844da34736c0439ae50017826a7393406e75acd8 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sun, 3 May 2009 04:33:32 +0000 Subject: [PATCH] When a fix-it hint would span multiple lines, don't print it; half a fix-it hint is much worse than no fix-it hint. (Fixes PR4084). When we need to truncate a source line to fix in the terminal, make sure to take the width of the fix-it information into account, too. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70656 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/TextDiagnosticPrinter.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp index bcf30a5f56..a2cf8f958c 100644 --- a/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/lib/Frontend/TextDiagnosticPrinter.cpp @@ -132,7 +132,25 @@ static void SelectInterestingSourceRegion(std::string &SourceLine, for (; CaretEnd != CaretStart; --CaretEnd) if (!isspace(CaretLine[CaretEnd - 1])) break; + + // If we have a fix-it line, make sure the slice includes all of the + // fix-it information. + if (!FixItInsertionLine.empty()) { + unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); + for (; FixItStart != FixItEnd; ++FixItStart) + if (!isspace(FixItInsertionLine[FixItStart])) + break; + for (; FixItEnd != FixItStart; --FixItEnd) + if (!isspace(FixItInsertionLine[FixItEnd - 1])) + break; + + if (FixItStart < CaretStart) + CaretStart = FixItStart; + if (FixItEnd > CaretEnd) + CaretEnd = FixItEnd; + } + // CaretLine[CaretStart, CaretEnd) contains all of the interesting // parts of the caret line. While this slice is smaller than the // number of columns we have, try to grow the slice to encompass @@ -379,6 +397,9 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, FixItInsertionLine.resize(LastColumnModified, ' '); std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(), FixItInsertionLine.begin() + HintColNo - 1); + } else { + FixItInsertionLine.clear(); + break; } } } -- 2.40.0