From 193cdd381b77647a5a96b52b47b47cae454b29c5 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 19 Jan 2015 10:52:16 +0000 Subject: [PATCH] clang-format: Fix crasher on incomplete condition compilation. Previously crashing input: void f( #if A ); #else #endif llvm-svn: 226451 --- clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/FormatTest.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 4ba3f91..58902bf 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/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/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 548273a..f267c94 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/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) { -- 2.7.4