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);