From: Daniel Jasper Date: Mon, 19 Jan 2015 10:51:23 +0000 (+0000) Subject: clang-format: Fix clang-format crash on invalid code. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=675b4f8350a868081a5a33f03f49fbe7c5f56b31;p=platform%2Fupstream%2Fllvm.git clang-format: Fix clang-format crash on invalid code. llvm-svn: 226449 --- diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 4b5ad59..7cf1c84 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -516,7 +516,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block) return Current.NestingLevel == 0 ? State.FirstIndent : State.Stack.back().Indent; - if (Current.isOneOf(tok::r_brace, tok::r_square)) { + if (Current.isOneOf(tok::r_brace, tok::r_square) && State.Stack.size() > 1) { if (Current.closesBlockTypeList(Style)) return State.Stack[State.Stack.size() - 2].NestedBlockIndent; if (Current.MatchingParen && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 7987e58..1352615 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -67,6 +67,12 @@ protected: verifyFormat(llvm::Twine("void f() { " + text + " }").str()); } + /// \brief Verify that clang-format does not crash on the given input. + void verifyNoCrash(llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle()) { + format(Code, Style); + } + int ReplacementCount; }; @@ -5622,6 +5628,8 @@ TEST_F(FormatTest, FormatsArrays) { "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" " .aaaaaaa[0]\n" " .aaaaaaaaaaaaaaaaaaaaaa();"); + + verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); } TEST_F(FormatTest, LineStartsWithSpecialCharacter) {