[PATCH] Update Mozilla Style

Anthony Jones ajones at mozilla.com
Thu Jan 23 17:58:00 PST 2014


  This allows breaking after the type in top level function definitions.

  int
  MyClass::MyFunction();

  I tried a few different ways to implement this feature but this is what it took to make it work. I tried implementing it in ContinuationIndenter::mustBreak but State.Line->MightBeFunctionDecl seems to be false for functions with no arguments.

Hi djasper,

http://llvm-reviews.chandlerc.com/D2610

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2610?vs=6627&id=6628#toc

Files:
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp

Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -247,6 +247,10 @@
   /// are not also definitions after the type.
   bool IndentFunctionDeclarationAfterType;
 
+  /// \brief If \c true, breaks top function definitions after the type
+  /// declaration.
+  bool AlwaysBreakTopLevelFunctionsAfterType;
+
   /// \brief If \c true, spaces will be inserted after '(' and before ')'.
   bool SpacesInParentheses;
 
@@ -300,10 +304,12 @@
            AllowShortIfStatementsOnASingleLine ==
                R.AllowShortIfStatementsOnASingleLine &&
            AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine &&
-           AlwaysBreakTemplateDeclarations ==
-               R.AlwaysBreakTemplateDeclarations &&
            AlwaysBreakBeforeMultilineStrings ==
                R.AlwaysBreakBeforeMultilineStrings &&
+           AlwaysBreakTopLevelFunctionsAfterType ==
+               R.AlwaysBreakTopLevelFunctionsAfterType &&
+           AlwaysBreakTemplateDeclarations ==
+               R.AlwaysBreakTemplateDeclarations &&
            BinPackParameters == R.BinPackParameters &&
            BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
            BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators &&
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -143,6 +143,8 @@
                    Style.AllowShortLoopsOnASingleLine);
     IO.mapOptional("AllowShortFunctionsOnASingleLine",
                    Style.AllowShortFunctionsOnASingleLine);
+    IO.mapOptional("AlwaysBreakTopLevelFunctionsAfterType",
+                   Style.AlwaysBreakTopLevelFunctionsAfterType);
     IO.mapOptional("AlwaysBreakTemplateDeclarations",
                    Style.AlwaysBreakTemplateDeclarations);
     IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
@@ -246,6 +248,7 @@
   LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
   LLVMStyle.AllowShortLoopsOnASingleLine = false;
   LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
+  LLVMStyle.AlwaysBreakTopLevelFunctionsAfterType = false;
   LLVMStyle.AlwaysBreakTemplateDeclarations = false;
   LLVMStyle.BinPackParameters = true;
   LLVMStyle.BreakBeforeBinaryOperators = false;
@@ -336,6 +339,7 @@
 FormatStyle getMozillaStyle() {
   FormatStyle MozillaStyle = getLLVMStyle();
   MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
+  MozillaStyle.AlwaysBreakTopLevelFunctionsAfterType = true;
   MozillaStyle.BreakConstructorInitializersBeforeComma = true;
   MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   MozillaStyle.ConstructorInitializerIndentWidth = 2;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1084,6 +1084,7 @@
     return;
   FormatToken *Current = Line.First->Next;
   bool InFunctionDecl = Line.MightBeFunctionDecl;
+  bool FirstFunctionDecl = Line.MightBeFunctionDecl && Style.AlwaysBreakTopLevelFunctionsAfterType;
   while (Current != NULL) {
     if (Current->Type == TT_LineComment) {
       if (Current->Previous->BlockKind == BK_BracedInit)
@@ -1098,6 +1099,15 @@
     Current->MustBreakBefore =
         Current->MustBreakBefore || mustBreakBefore(Line, *Current);
 
+    if (FirstFunctionDecl) {
+      if (Current->Type == TT_StartOfName) {
+        Current->MustBreakBefore = Line.Level == 0;
+      }
+      else if (Current->is(tok::l_paren)) {
+        FirstFunctionDecl = false;
+      }
+    }
+
     Current->CanBreakBefore =
         Current->MustBreakBefore || canBreakBefore(Line, *Current);
     if (Current->MustBreakBefore || !Current->Children.empty() ||
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -7285,6 +7285,7 @@
   CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
   CHECK_PARSE_BOOL(SpacesInParentheses);
   CHECK_PARSE_BOOL(SpacesInAngles);
+  CHECK_PARSE_BOOL(AlwaysBreakTopLevelFunctionsAfterType);
   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2610.2.patch
Type: text/x-patch
Size: 4428 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140123/7beda847/attachment.bin>


More information about the cfe-commits mailing list