State.Stack.back().Indent = State.Column;
else if (Previous.opensScope()) {
// If a function has multiple parameters (including a single parameter
- // that is a binary expression) or a trailing call, indented all
+ // that is a binary expression) or a trailing call, indent all
// parameters from the opening parenthesis. This avoids confusing
// indents like:
// OuterFunction(InnerFunctionCall(
public:
AnnotatingParser(AnnotatedLine &Line, IdentifierInfo &Ident_in)
: Line(Line), CurrentToken(Line.First), KeywordVirtualFound(false),
- NameFound(false), Ident_in(Ident_in) {
+ NameFound(false), AutoFound(false), Ident_in(Ident_in) {
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
}
Contexts.back().FirstStartOfName = &Current;
Current.Type = TT_StartOfName;
NameFound = true;
+ } else if (Current.is(tok::kw_auto)) {
+ AutoFound = true;
+ } else if (Current.is(tok::arrow) && AutoFound) {
+ Current.Type = TT_TrailingReturnArrow;
} else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Current.Type =
determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
FormatToken *CurrentToken;
bool KeywordVirtualFound;
bool NameFound;
+ bool AutoFound;
IdentifierInfo &Ident_in;
};
(Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
return false;
+ if (Tok.Type == TT_TrailingReturnArrow ||
+ Tok.Previous->Type == TT_TrailingReturnArrow)
+ return true;
if (Tok.Previous->is(tok::comma))
return true;
if (Tok.is(tok::comma))
if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
Left.Previous->is(tok::kw___attribute))
return false;
- if (Left.is(tok::l_paren) && Left.Previous->Type == TT_BinaryOperator)
+ if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
+ Left.Previous->is(tok::r_paren)))
return false;
}
" const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
}
+TEST_F(FormatTest, TrailingReturnType) {
+ verifyFormat("auto foo() -> int;\n");
+ verifyFormat("struct S {\n"
+ " auto bar() const -> int;\n"
+ "};");
+ verifyFormat("template <size_t Order, typename T>\n"
+ "auto load_img(const std::string &filename)\n"
+ " -> alias::tensor<Order, T, mem::tag::cpu> {}");
+}
+
TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
verifyFormat("void someLongFunction(\n"
" int someLongParameter) const {}",