clang-format: Fix crasher on incomplete condition compilation.
authorDaniel Jasper <djasper@google.com>
Mon, 19 Jan 2015 10:52:16 +0000 (10:52 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 19 Jan 2015 10:52:16 +0000 (10:52 +0000)
Previously crashing input:
  void f(
  #if A
      );
  #else
  #endif

llvm-svn: 226451

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index 4ba3f91..58902bf 100644 (file)
@@ -1361,7 +1361,7 @@ static bool isFunctionDeclarationName(const FormatToken &Current) {
   assert(Next->is(tok::l_paren));
   if (Next->Next == Next->MatchingParen)
     return true;
-  for (const FormatToken *Tok = Next->Next; Tok != Next->MatchingParen;
+  for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
        Tok = Tok->Next) {
     if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
         Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
index 548273a..f267c94 100644 (file)
@@ -2930,6 +2930,12 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
                "#if a\n"
                "#else\n"
                "#endif");
+
+  verifyFormat("void f(\n"
+               "#if A\n"
+               "    );\n"
+               "#else\n"
+               "#endif");
 }
 
 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {