From: Daniel Jasper Date: Mon, 19 Jan 2015 10:52:16 +0000 (+0000) Subject: clang-format: Fix crasher on incomplete condition compilation. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f02caed59b8ed98994afaced1abcf816b9356b0a;p=clang clang-format: Fix crasher on incomplete condition compilation. Previously crashing input: void f( #if A ); #else #endif git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226451 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 4ba3f91969..58902bfbd6 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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)) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 548273ade0..f267c94b6d 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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) {