From: Daniel Jasper Date: Wed, 13 Feb 2013 20:33:44 +0000 (+0000) Subject: Allow breaking after the return type in function declarations. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a03ab10f0e4d888139b3b694dd55d176982f72a4;p=clang Allow breaking after the return type in function declarations. This has so far been disabled for Google style, but should be done before breaking at nested name specifiers or in template parameters. Before (in Google style): template aaaaaaaa::aaaaa::aaaaaa aaaaaaaaaaaaaaaaaaaaaaaa< T>::aaaaaaa() {} After: template aaaaaaaa::aaaaa::aaaaaa aaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaa() {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175074 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 75b809baab..d6cc114e3c 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -77,8 +77,9 @@ struct FormatStyle { /// the next line even if \c BinPackParameters is \c false. bool AllowAllParametersOfDeclarationOnNextLine; - /// \brief Allow putting the return type of a function onto its own line. - bool AllowReturnTypeOnItsOwnLine; + /// \brief Penalty for putting the return type of a function onto its own + /// line. + unsigned PenaltyReturnTypeOnItsOwnLine; /// \brief If the constructor initializers don't fit on a line, put each /// initializer on its own line. diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index d8c02e2e18..8c2128eb01 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -43,11 +43,11 @@ FormatStyle getLLVMStyle() { LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.BinPackParameters = true; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; - LLVMStyle.AllowReturnTypeOnItsOwnLine = true; LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.ObjCSpaceBeforeProtocolList = true; LLVMStyle.PenaltyExcessCharacter = 1000000; + LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 5; return LLVMStyle; } @@ -63,11 +63,11 @@ FormatStyle getGoogleStyle() { GoogleStyle.SpacesBeforeTrailingComments = 2; GoogleStyle.BinPackParameters = false; GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; - GoogleStyle.AllowReturnTypeOnItsOwnLine = false; GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; GoogleStyle.AllowShortIfStatementsOnASingleLine = false; GoogleStyle.ObjCSpaceBeforeProtocolList = false; GoogleStyle.PenaltyExcessCharacter = 1000000; + GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 100; return GoogleStyle; } @@ -717,9 +717,10 @@ private: reconstructPath(State, Current->Previous); DEBUG({ if (Current->NewLine) { - llvm::errs() << "Penalty for splitting before " - << Current->State.NextToken->FormatTok.Tok.getName() - << ": " << Current->State.NextToken->SplitPenalty << "\n"; + llvm::errs() + << "Penalty for splitting before " + << Current->Previous->State.NextToken->FormatTok.Tok.getName() + << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n"; } }); addTokenToState(Current->NewLine, false, State); diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index d2e19af56f..4c36536104 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -847,6 +847,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Left = *Tok.Parent; const AnnotatedToken &Right = Tok; + if (Right.Type == TT_StartOfName) + return Style.PenaltyReturnTypeOnItsOwnLine; if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace)) return 50; if (Left.is(tok::equal) && Right.is(tok::l_brace)) @@ -1026,7 +1028,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right) { const AnnotatedToken &Left = *Right.Parent; - if (Right.Type == TT_StartOfName && Style.AllowReturnTypeOnItsOwnLine) + if (Right.Type == TT_StartOfName) return true; if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) return false; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8fb4b2acd5..f72aad7aad 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1763,6 +1763,14 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { verifyGoogleFormat( "TypeSpecDecl* TypeSpecDecl::Create(\n" " ASTContext& C, DeclContext* DC, SourceLocation L) {\n}"); + verifyGoogleFormat( + "some_namespace::LongReturnType\n" + "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" + " int first_long_parameter, int second_parameter) {\n}"); + + verifyGoogleFormat("template \n" + "aaaaaaaa::aaaaa::aaaaaa\n" + "aaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaa() {\n}"); } TEST_F(FormatTest, LineStartsWithSpecialCharacter) {