r185938 - Initial support for formatting trailing return types.
Daniel Jasper
djasper at google.com
Tue Jul 9 07:36:48 PDT 2013
Author: djasper
Date: Tue Jul 9 09:36:48 2013
New Revision: 185938
URL: http://llvm.org/viewvc/llvm-project?rev=185938&view=rev
Log:
Initial support for formatting trailing return types.
This fixes llvm.org/PR15170.
For now, the basic formatting rules are (based on the C++11 standard):
* Surround the "->" with spaces.
* Break before "->".
Also fix typo.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=185938&r1=185937&r2=185938&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Jul 9 09:36:48 2013
@@ -674,7 +674,7 @@ private:
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(
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=185938&r1=185937&r2=185938&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Tue Jul 9 09:36:48 2013
@@ -51,6 +51,7 @@ enum TokenType {
TT_StartOfName,
TT_TemplateCloser,
TT_TemplateOpener,
+ TT_TrailingReturnArrow,
TT_TrailingUnaryOperator,
TT_UnaryOperator,
TT_Unknown
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=185938&r1=185937&r2=185938&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Jul 9 09:36:48 2013
@@ -31,7 +31,7 @@ class AnnotatingParser {
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));
}
@@ -589,6 +589,10 @@ private:
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 &&
@@ -792,6 +796,7 @@ private:
FormatToken *CurrentToken;
bool KeywordVirtualFound;
bool NameFound;
+ bool AutoFound;
IdentifierInfo &Ident_in;
};
@@ -1165,6 +1170,9 @@ bool TokenAnnotator::spaceRequiredBefore
(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))
@@ -1234,7 +1242,8 @@ bool TokenAnnotator::canBreakBefore(cons
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;
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=185938&r1=185937&r2=185938&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jul 9 09:36:48 2013
@@ -2465,6 +2465,16 @@ TEST_F(FormatTest, BreaksFunctionDeclara
" 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 {}",
More information about the cfe-commits
mailing list