From 3682fcda17f315b6c1d4eccd8e41b12de3bc45db Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 16 Dec 2013 08:36:18 +0000 Subject: [PATCH] clang-format: Fix formatting of function type parameters. Before: void f() { typedef void (*f)(int * a); } After: void f() { typedef void (*f)(int *a); } llvm-svn: 197369 --- clang/lib/Format/TokenAnnotator.cpp | 15 ++++++++++----- clang/unittests/Format/FormatTest.cpp | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 9132ba437704..19759afc8542 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -605,12 +605,17 @@ private: Previous->Type = TT_PointerOrReference; } } - } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) || - (Current.is(tok::l_paren) && !Line.MustBeDeclaration && - !Line.InPPDirective && - (!Current.Previous || - !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) { + } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) { Contexts.back().IsExpression = true; + } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration && + !Line.InPPDirective) { + bool ParametersOfFunctionType = + Current.Previous && Current.Previous->is(tok::r_paren) && + Current.Previous->MatchingParen && + Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen; + bool IsForOrCatch = Current.Previous && + Current.Previous->isOneOf(tok::kw_for, tok::kw_catch); + Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch; } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { for (FormatToken *Previous = Current.Previous; Previous && Previous->isOneOf(tok::star, tok::amp); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e34ebeb98cd0..bd9d10835765 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -4241,6 +4241,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("auto a = [](int **&, int ***) {};"); verifyFormat("auto PointerBinding = [](const char *S) {};"); verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); + verifyIndependentOfContext("typedef void (*f)(int *a);"); verifyIndependentOfContext("InvalidRegions[*R] = 0;");