From d2a520ee9ed8f9f67f561bd8e074781cef7a202c Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Wed, 23 Aug 2017 15:16:47 +0000 Subject: [PATCH] [clang-format] Emit absolute splits before lines for comments Summary: This patch makes the splits emitted for the beginning of comment lines during reformatting absolute. Previously, they were relative to the start of the non-whitespace content of the line, which messes up further TailOffset calculations in breakProtrudingToken. This fixes an assertion failure reported in bug 34236: https://bugs.llvm.org/show_bug.cgi?id=34236. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D36956 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311559 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/BreakableToken.cpp | 22 ++++++++++------------ unittests/Format/FormatTestComments.cpp | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/Format/BreakableToken.cpp b/lib/Format/BreakableToken.cpp index 20e3e5b1df..b9e48e6301 100644 --- a/lib/Format/BreakableToken.cpp +++ b/lib/Format/BreakableToken.cpp @@ -545,15 +545,18 @@ void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, } BreakableToken::Split BreakableBlockComment::getSplitBefore( - unsigned LineIndex, - unsigned PreviousEndColumn, - unsigned ColumnLimit, + unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit, llvm::Regex &CommentPragmasRegex) const { if (!mayReflow(LineIndex, CommentPragmasRegex)) return Split(StringRef::npos, 0); StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); - return getReflowSplit(TrimmedContent, ReflowPrefix, PreviousEndColumn, - ColumnLimit); + Split Result = getReflowSplit(TrimmedContent, ReflowPrefix, PreviousEndColumn, + ColumnLimit); + // Result is relative to TrimmedContent. Adapt it relative to + // Content[LineIndex]. + if (Result.first != StringRef::npos) + Result.first += Content[LineIndex].size() - TrimmedContent.size(); + return Result; } unsigned BreakableBlockComment::getReflownColumn( @@ -633,17 +636,12 @@ void BreakableBlockComment::replaceWhitespaceBefore( /*CurrentPrefix=*/ReflowPrefix, InPPDirective, /*Newlines=*/0, /*Spaces=*/0); // Check if we need to also insert a break at the whitespace range. - // For this we first adapt the reflow split relative to the beginning of the - // content. // Note that we don't need a penalty for this break, since it doesn't change // the total number of lines. - Split BreakSplit = SplitBefore; - BreakSplit.first += TrimmedContent.data() - Content[LineIndex].data(); unsigned ReflownColumn = getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn); - if (ReflownColumn > ColumnLimit) { - insertBreak(LineIndex, 0, BreakSplit, Whitespaces); - } + if (ReflownColumn > ColumnLimit) + insertBreak(LineIndex, 0, SplitBefore, Whitespaces); return; } diff --git a/unittests/Format/FormatTestComments.cpp b/unittests/Format/FormatTestComments.cpp index 79dc003ae8..70fa965ade 100644 --- a/unittests/Format/FormatTestComments.cpp +++ b/unittests/Format/FormatTestComments.cpp @@ -2803,6 +2803,23 @@ TEST_F(FormatTestComments, NonTrailingBlockComments) { " A = B;", getLLVMStyleWithColumns(40))); } + +TEST_F(FormatTestComments, NoCrush_Bug34236) { + // This is a test case from a crasher reported in: + // https://bugs.llvm.org/show_bug.cgi?id=34236 + EXPECT_EQ( + R"( +/* */ /* + * a + * b c + * d*/)", + format( + R"( +/* */ /* + * a b + * c d*/)", + getLLVMStyleWithColumns(80))); +} } // end namespace } // end namespace format } // end namespace clang -- 2.40.0