From 26d1b1db23babd59003f7c79219894b07fac32bb Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Sun, 24 Feb 2013 18:54:32 +0000 Subject: Allow breaking between a type and name in variable declarations. This fixes llvm.org/PR14967 and is generall necessary to avoid situations where the column limit is exceeded. The challenge is restricting such lines splits, otherwise clang-format suddenly starts breaking at bad places. Before: ReallyLongReturnType ReallyReallyLongFunctionName( const std::string &SomeParameter, const SomeType &ReallyReallyLongParameterName, const SomeType &AnotherLongParameterName) {} After: ReallyLongReturnType ReallyReallyLongFunctionName( const std::string &SomeParameter, const SomeType & ReallyReallyLongParameterName, const SomeType & AnotherLongParameterName) {} llvm-svn: 175999 --- clang/lib/Format/Format.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'clang/lib/Format/Format.cpp') diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index d8784c126e6..60fe957a126 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -258,12 +258,6 @@ private: tooling::Replacements Replaces; }; -static bool isVarDeclName(const AnnotatedToken &Tok) { - return Tok.Parent != NULL && Tok.is(tok::identifier) && - (Tok.Parent->Type == TT_PointerOrReference || - Tok.Parent->is(tok::identifier)); -} - class UnwrappedLineFormatter { public: UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, @@ -498,8 +492,8 @@ private: ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) || State.ParenLevel == 0)) { State.Column = State.VariablePos; - } else if (State.NextToken->Parent->ClosesTemplateDeclaration || - Current.Type == TT_StartOfName) { + } else if (Previous.ClosesTemplateDeclaration || + (Current.Type == TT_StartOfName && State.ParenLevel == 0)) { State.Column = State.Stack.back().Indent - 4; } else if (Current.Type == TT_ObjCSelectorName) { if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) { @@ -510,7 +504,8 @@ private: State.Stack.back().ColonPos = State.Column + Current.FormatTok.TokenLength; } - } else if (Previous.Type == TT_ObjCMethodExpr || isVarDeclName(Current)) { + } else if (Previous.Type == TT_ObjCMethodExpr || + Current.Type == TT_StartOfName) { State.Column = State.Stack.back().Indent + 4; } else { State.Column = State.Stack.back().Indent; @@ -551,8 +546,7 @@ private: if (State.Stack.back().AvoidBinPacking) { // If we are breaking after '(', '{', '<', this is not bin packing // unless AllowAllParametersOfDeclarationOnNextLine is false. - if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) && - Previous.Type != TT_TemplateOpener) || + if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace)) || (!Style.AllowAllParametersOfDeclarationOnNextLine && Line.MustBeDeclaration)) State.Stack.back().BreakBeforeParameter = true; -- cgit v1.2.3