[PATCH] add option AlwaysBreakAfterDefinitionReturnType

Jarkko Hietaniemi jhi at iki.fi
Fri May 9 03:48:09 PDT 2014


I wanted to get the return type of function definition on its own line

const char *
foo(...) {
   ...
}

as opposed to

const char *foo(...) {
   ...
}

But not to have that happen for function declarations:

const char *bar(...);

The PenaltyReturnTypeOnItsOwnLine didn't seem to do much useful for this 
purpose.

Named the option AlwaysBreakAfterDefinitionReturnType, which is a little 
bit on the wordy side.

Patch attached.
-------------- next part --------------
Index: tools/clang/include/clang/Format/Format.h
===================================================================
--- tools/clang/include/clang/Format/Format.h	(revision 208145)
+++ tools/clang/include/clang/Format/Format.h	(working copy)
@@ -206,6 +206,12 @@
   /// initializer lists.
   unsigned ConstructorInitializerIndentWidth;
 
+  /// \brief If \c true, always break after function defintion return types.
+  ///
+  /// More truthfully called 'break before the identifier following the type
+  /// in a function definition'.  Overrides PenaltyReturnTypeOnItsOwnLine.
+  bool AlwaysBreakAfterDefinitionReturnType;
+
   /// \brief If \c true, always break after the <tt>template<...></tt> of a
   /// template declaration.
   bool AlwaysBreakTemplateDeclarations;
@@ -341,6 +347,8 @@
            AllowShortIfStatementsOnASingleLine ==
                R.AllowShortIfStatementsOnASingleLine &&
            AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine &&
+           AlwaysBreakAfterDefinitionReturnType ==
+               R.AlwaysBreakAfterDefinitionReturnType &&
            AlwaysBreakTemplateDeclarations ==
                R.AlwaysBreakTemplateDeclarations &&
            AlwaysBreakBeforeMultilineStrings ==
Index: tools/clang/lib/Format/Format.cpp
===================================================================
--- tools/clang/lib/Format/Format.cpp	(revision 208145)
+++ tools/clang/lib/Format/Format.cpp	(working copy)
@@ -156,6 +156,8 @@
                    Style.AllowShortLoopsOnASingleLine);
     IO.mapOptional("AllowShortFunctionsOnASingleLine",
                    Style.AllowShortFunctionsOnASingleLine);
+    IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
+                   Style.AlwaysBreakAfterDefinitionReturnType);
     IO.mapOptional("AlwaysBreakTemplateDeclarations",
                    Style.AlwaysBreakTemplateDeclarations);
     IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
@@ -264,6 +266,7 @@
   LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
   LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
   LLVMStyle.AllowShortLoopsOnASingleLine = false;
+  LLVMStyle.AlwaysBreakAfterDefinitionReturnType = false;
   LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
   LLVMStyle.AlwaysBreakTemplateDeclarations = false;
   LLVMStyle.BinPackParameters = true;
Index: tools/clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- tools/clang/lib/Format/TokenAnnotator.cpp	(revision 208145)
+++ tools/clang/lib/Format/TokenAnnotator.cpp	(working copy)
@@ -1222,6 +1222,14 @@
     Current->MustBreakBefore =
         Current->MustBreakBefore || mustBreakBefore(Line, *Current);
 
+    if (Style.AlwaysBreakAfterDefinitionReturnType &&
+        InFunctionDecl &&
+        Current->Type == TT_StartOfName &&
+        Current->Next && Current->Next->is(tok::l_paren) &&
+        Line.Last->isNot(tok::semi)) {  // Not a declaration.
+      Current->MustBreakBefore = true;
+    }
+
     Current->CanBreakBefore =
         Current->MustBreakBefore || canBreakBefore(Line, *Current);
     if (Current->MustBreakBefore || !Current->Children.empty() ||
Index: tools/clang/unittests/Format/FormatTest.cpp
===================================================================
--- tools/clang/unittests/Format/FormatTest.cpp	(revision 208145)
+++ tools/clang/unittests/Format/FormatTest.cpp	(working copy)
@@ -3942,6 +3942,17 @@
                getLLVMStyleWithColumns(25));
 }
 
+TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
+  FormatStyle AfterType = getLLVMStyle();
+  AfterType.AlwaysBreakAfterDefinitionReturnType = true;
+  verifyFormat("const char *\n"
+               "f(void) {\n"  // Break here.
+               "  return \"\";\n"
+               "}\n"
+               "const char *bar(void);\n",  // No break here.
+               AfterType);
+}
+
 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
   FormatStyle NoBreak = getLLVMStyle();
   NoBreak.AlwaysBreakBeforeMultilineStrings = false;


More information about the cfe-commits mailing list