]> granicus.if.org Git - clang/commitdiff
clang-format: Fix clang-format crash on invalid code.
authorDaniel Jasper <djasper@google.com>
Mon, 19 Jan 2015 10:51:23 +0000 (10:51 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 19 Jan 2015 10:51:23 +0000 (10:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226449 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTest.cpp

index 4b5ad599734ff4486a583d6259413004f5edac50..7cf1c844678552f51206bf04b9a2583853f2111c 100644 (file)
@@ -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 &&
index 7987e58790a737b59b40a82eee6e9a2629346ec6..1352615814518475d3cff974c767468f898b521f 100644 (file)
@@ -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) {