From 291f64fd03a8057d4b260a3b74a7d109c017111a Mon Sep 17 00:00:00 2001 From: Roman Kashitsyn Date: Mon, 10 Aug 2015 13:43:19 +0000 Subject: [PATCH] Add WebKit brace style configuration option. Summary: Add brace style `BS_WebKit` as described on https://www.webkit.org/coding/coding-style.html: * Function definitions: place each brace on its own line. * Other braces: place the open brace on the line preceding the code block; place the close brace on its own line. Set brace style used in `getWebKitStyle()` to the newly added `BS_WebKit`. Reviewers: djasper, klimek Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D11837 llvm-svn: 244446 --- clang/docs/ClangFormatStyleOptions.rst | 4 ++- clang/include/clang/Format/Format.h | 6 ++-- clang/lib/Format/Format.cpp | 3 +- clang/unittests/Format/FormatTest.cpp | 57 +++++++++++++++++++++++++++++----- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 031daee..1d61197 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -279,13 +279,15 @@ the configuration (without a prefix: ``Auto``). 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'. + Like ``Attach``, but break before function definitions, 'catch', and 'else'. * ``BS_Allman`` (in configuration: ``Allman``) Always break before braces. * ``BS_GNU`` (in configuration: ``GNU``) Always break before braces and add an extra level of indentation to braces of control statements, not to those of class, function or other definitions. + * ``BS_WebKit`` (in configuration: ``WebKit``) + Like ``Attach``, but break before functions. **BreakBeforeTernaryOperators** (``bool``) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f8c8c37..50faa8a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -169,14 +169,16 @@ struct FormatStyle { /// Like ``Attach``, but break before braces on enum, function, and record /// definitions. BS_Mozilla, - /// Like \c Attach, but break before function definitions, and 'else'. + /// Like \c Attach, but break before function definitions, 'catch', and 'else'. BS_Stroustrup, /// Always break before braces. BS_Allman, /// Always break before braces and add an extra level of indentation to /// braces of control statements, not to those of class, function /// or other definitions. - BS_GNU + BS_GNU, + /// Like ``Attach``, but break before functions. + BS_WebKit }; /// \brief The brace breaking style to use. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 5c23906..55f9080 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -98,6 +98,7 @@ template <> struct ScalarEnumerationTraits { IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); + IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit); } }; @@ -504,7 +505,7 @@ FormatStyle getWebKitStyle() { Style.AlignOperands = false; Style.AlignTrailingComments = false; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; - Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; + Style.BreakBeforeBraces = FormatStyle::BS_WebKit; Style.BreakConstructorInitializersBeforeComma = true; Style.Cpp11BracedListStyle = false; Style.ColumnLimit = 0; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index ce7754d..f71f63c 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2329,13 +2329,16 @@ TEST_F(FormatTest, IncompleteTryCatchBlocks) { TEST_F(FormatTest, FormatTryCatchBraceStyles) { FormatStyle Style = getLLVMStyle(); - Style.BreakBeforeBraces = FormatStyle::BS_Attach; - verifyFormat("try {\n" - " // something\n" - "} catch (...) {\n" - " // something\n" - "}", - Style); + for (auto BraceStyle : + {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, FormatStyle::BS_WebKit}) { + Style.BreakBeforeBraces = BraceStyle; + verifyFormat("try {\n" + " // something\n" + "} catch (...) {\n" + " // something\n" + "}", + Style); + } Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; verifyFormat("try {\n" " // something\n" @@ -9060,6 +9063,45 @@ TEST_F(FormatTest, GNUBraceBreaking) { "#endif", GNUBraceStyle); } + +TEST_F(FormatTest, WebKitBraceBreaking) { + FormatStyle WebKitBraceStyle = getLLVMStyle(); + WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; + verifyFormat("namespace a {\n" + "class A {\n" + " void f()\n" + " {\n" + " if (true) {\n" + " a();\n" + " b();\n" + " }\n" + " }\n" + " void g() { return; }\n" + "};\n" + "enum E {\n" + " A,\n" + " // foo\n" + " B,\n" + " C\n" + "};\n" + "struct B {\n" + " int x;\n" + "};\n" + "}\n", + WebKitBraceStyle); + verifyFormat("struct S {\n" + " int Type;\n" + " union {\n" + " int x;\n" + " double y;\n" + " } Value;\n" + " class C {\n" + " MyFavoriteType Value;\n" + " } Class;\n" + "};\n", + WebKitBraceStyle); +} + TEST_F(FormatTest, CatchExceptionReferenceBinding) { verifyFormat("void f() {\n" " try {\n" @@ -9336,6 +9378,7 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, FormatStyle::BS_Allman); CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); + CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, FormatStyle::BS_WebKit); Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", -- 2.7.4