From 9b623a9ba964c636e2fb865dc5d8b18a76f07ca8 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 19 Jan 2015 10:51:23 +0000 Subject: [PATCH] clang-format: Fix clang-format crash on invalid code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226449 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 2 +- unittests/Format/FormatTest.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 4b5ad59973..7cf1c84467 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 7987e58790..1352615814 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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) { -- 2.40.0