diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index e80cfd545b072131be0ad733d1f1f3acb92a7f33..031daeeaa123908e1f252f44f70e37eccf6befd0 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -275,6 +275,9 @@ the configuration (without a prefix: ``Auto``). * ``BS_Linux`` (in configuration: ``Linux``) Like ``Attach``, but break before braces on function, namespace and class definitions. + * ``BS_Mozilla`` (in configuration: ``Mozilla``) + Like ``Attach``, but break before braces on enum, function, and record + definitions. * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) Like ``Attach``, but break before function definitions, and 'else'. * ``BS_Allman`` (in configuration: ``Allman``) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index b234403bebc5aa15a77fad16f47153372c752f45..f8c8c373e143c04d6236cdd044f9dcebefef29f1 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -166,6 +166,9 @@ struct FormatStyle { /// Like \c Attach, but break before braces on function, namespace and /// class definitions. BS_Linux, + /// Like ``Attach``, but break before braces on enum, function, and record + /// definitions. + BS_Mozilla, /// Like \c Attach, but break before function definitions, and 'else'. BS_Stroustrup, /// Always break before braces. diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 1e7996e31ae6909e60af8fa2f0b28f1e5fd8b207..382ae819ebfdc8327767b687a58f296e92840142 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -94,6 +94,7 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); + IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla); IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); @@ -483,6 +484,7 @@ FormatStyle getMozillaStyle() { MozillaStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel; MozillaStyle.AlwaysBreakTemplateDeclarations = true; + MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; MozillaStyle.BreakConstructorInitializersBeforeComma = true; MozillaStyle.ConstructorInitializerIndentWidth = 2; MozillaStyle.ContinuationIndentWidth = 2; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 4734ff350d72ceb9a10efe43963e5c6530715702..1e0c979d91df3739bd1995721154ad502f6ce111 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2108,7 +2108,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, return Right.HasUnescapedNewline; if (isAllmanBrace(Left) || isAllmanBrace(Right)) return Style.BreakBeforeBraces == FormatStyle::BS_Allman || - Style.BreakBeforeBraces == FormatStyle::BS_GNU; + Style.BreakBeforeBraces == FormatStyle::BS_GNU || + (Style.BreakBeforeBraces == FormatStyle::BS_Mozilla && + Line.startsWith(tok::kw_enum)); if (Style.Language == FormatStyle::LK_Proto && Left.isNot(tok::l_brace) && Right.is(TT_SelectorName)) return true; diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 345e109be8f715ff5eaa48e2b101b2515375df8a..97fd98ecb9479d151130feddf62a2c4233f8f9b4 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -457,6 +457,8 @@ static bool ShouldBreakBeforeBrace(const FormatStyle &Style, switch (Style.BreakBeforeBraces) { case FormatStyle::BS_Linux: return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class); + case FormatStyle::BS_Mozilla: + return InitialToken.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union); case FormatStyle::BS_Allman: case FormatStyle::BS_GNU: return true; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index c9f63cef6649d6082251660e5f4b66d4910e1ff9..6aa7681ac59c3681bb6c152e647bcaf4037df192 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -8579,6 +8579,50 @@ TEST_F(FormatTest, LinuxBraceBreaking) { LinuxBraceStyle); } +TEST_F(FormatTest, MozillaBraceBreaking) { + FormatStyle MozillaBraceStyle = getLLVMStyle(); + MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; + verifyFormat("namespace a {\n" + "class A\n" + "{\n" + " void f()\n" + " {\n" + " if (true) {\n" + " a();\n" + " b();\n" + " }\n" + " }\n" + " void g() { return; }\n" + "};\n" + "enum E\n" + "{\n" + " A,\n" + " // foo\n" + " B,\n" + " C\n" + "};\n" + "struct B\n" + "{\n" + " int x;\n" + "};\n" + "}\n", + MozillaBraceStyle); + verifyFormat("struct S\n" + "{\n" + " int Type;\n" + " union\n" + " {\n" + " int x;\n" + " double y;\n" + " } Value;\n" + " class C\n" + " {\n" + " MyFavoriteType Value;\n" + " } Class;\n" + "}\n", + MozillaBraceStyle); +} + TEST_F(FormatTest, StroustrupBraceBreaking) { FormatStyle StroustrupBraceStyle = getLLVMStyle(); StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; @@ -9219,6 +9263,8 @@ TEST_F(FormatTest, ParsesConfiguration) { FormatStyle::BS_Attach); CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, FormatStyle::BS_Linux); + CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, + FormatStyle::BS_Mozilla); CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, FormatStyle::BS_Stroustrup); CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,