r175074 - Allow breaking after the return type in function declarations.

Daniel Jasper djasper at google.com
Wed Feb 13 12:33:44 PST 2013


Author: djasper
Date: Wed Feb 13 14:33:44 2013
New Revision: 175074

URL: http://llvm.org/viewvc/llvm-project?rev=175074&view=rev
Log:
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 <typename T>
aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaaaaaaaa<
    T>::aaaaaaa() {}

After:
template <typename T>
aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>
aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}

Modified:
    cfe/trunk/include/clang/Format/Format.h
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=175074&r1=175073&r2=175074&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Wed Feb 13 14:33:44 2013
@@ -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.

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=175074&r1=175073&r2=175074&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Feb 13 14:33:44 2013
@@ -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);

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=175074&r1=175073&r2=175074&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Feb 13 14:33:44 2013
@@ -847,6 +847,8 @@ unsigned TokenAnnotator::splitPenalty(co
   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
 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;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=175074&r1=175073&r2=175074&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Feb 13 14:33:44 2013
@@ -1763,6 +1763,14 @@ TEST_F(FormatTest, BreaksFunctionDeclara
   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 <typename T>\n"
+                     "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
+                     "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {\n}");
 }
 
 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {





More information about the cfe-commits mailing list