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<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index d8784c1..60fe957 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -258,12 +258,6 @@
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 @@
((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 @@
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 @@
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;