]> granicus.if.org Git - clang/commitdiff
clang-format: Fix segfault in 'incomplete' macros.
authorDaniel Jasper <djasper@google.com>
Wed, 28 Aug 2013 09:17:37 +0000 (09:17 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 28 Aug 2013 09:17:37 +0000 (09:17 +0000)
The code leading to a segfault was:
  #pragma omp threadprivate(y)), // long comment leading to a line break

This fixes llvm.org/PR16513.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189460 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 199fc02204856075e2cccbe2eaed1b260a2c1639..798e4f3aecdca89f25a0ad8b6e9b280c60be4c89 100644 (file)
@@ -536,9 +536,10 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
 
   // If we encounter a closing ), ], } or >, we can remove a level from our
   // stacks.
-  if (Current.isOneOf(tok::r_paren, tok::r_square) ||
-      (Current.is(tok::r_brace) && State.NextToken != Line.First) ||
-      State.NextToken->Type == TT_TemplateCloser) {
+  if (State.Stack.size() > 1 &&
+      (Current.isOneOf(tok::r_paren, tok::r_square) ||
+       (Current.is(tok::r_brace) && State.NextToken != Line.First) ||
+       State.NextToken->Type == TT_TemplateCloser)) {
     State.Stack.pop_back();
     --State.ParenLevel;
   }
index d80011ba1755132f71f68b037fdd07b749404b74..067a259b2ce6a3df35c1fe64c5ebfc797398cc77 100644 (file)
@@ -1942,6 +1942,9 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
   verifyFormat("#define A template <typename T>");
   verifyFormat("#define STR(x) #x\n"
                "f(STR(this_is_a_string_literal{));");
+  verifyFormat("#pragma omp threadprivate( \\\n"
+               "    y)), // expected-warning",
+               getLLVMStyleWithColumns(28));
 }
 
 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {