From: Daniel Jasper Date: Thu, 29 Jan 2015 10:47:14 +0000 (+0000) Subject: clang-format: Fix crasher caused by not properly setting dry-run. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2a27967cd6072877cd42dba59bea170561284e5e;p=clang clang-format: Fix crasher caused by not properly setting dry-run. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@227427 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index 6c4aadc0f1..4844b66f9f 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -409,7 +409,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl &Lines, TheLine.Type == LT_ImportStatement) { LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun); while (State.NextToken) { - formatChildren(State, /*Newline=*/false, /*DryRun=*/false, Penalty); + formatChildren(State, /*Newline=*/false, DryRun, Penalty); Indenter->addTokenToState(State, /*Newline=*/false, DryRun); } } else if (Style.ColumnLimit == 0) { @@ -657,8 +657,8 @@ void UnwrappedLineFormatter::addNextStateToQueue(unsigned Penalty, bool UnwrappedLineFormatter::formatChildren(LineState &State, bool NewLine, bool DryRun, unsigned &Penalty) { - FormatToken &Previous = *State.NextToken->Previous; const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); + FormatToken &Previous = *State.NextToken->Previous; if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind != BK_Block || Previous.Children.size() == 0) // The previous token does not open a block. Nothing to do. We don't diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index bf1207e59c..44e5f690a2 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -264,6 +264,11 @@ void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End, void WhitespaceManager::generateChanges() { for (unsigned i = 0, e = Changes.size(); i != e; ++i) { const Change &C = Changes[i]; + if (i > 0) { + assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() != + C.OriginalWhitespaceRange.getBegin() && + "Generating two replacements for the same location"); + } if (C.CreateReplacement) { std::string ReplacementText = C.PreviousLinePostfix; if (C.ContinuesPPDirective) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 65a7bff599..f099f20a6e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3081,6 +3081,8 @@ TEST_F(FormatTest, LayoutNestedBlocks) { " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" "}, a);", Style); + + verifyNoCrash("^{v^{a}}"); } TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {