r181700 - Implements brace breaking styles.

Manuel Klimek klimek at google.com
Tue May 14 13:01:20 PDT 2013


I thought about that, but it's really hard to a) come up with decent names
for the flags and b) there's already precedence for naming them like this


On Tue, May 14, 2013 at 9:46 PM, Alexander Kornienko <alexfh at google.com>wrote:

> How about decomposing brace breaking styles to a set of orthogonal flags
> (break before braces in: functions, class definitions, namespaces, after
> if/for/while, before else, after else, may be something else). Then it
> would make code a bit clearer and allow specifying other styles not listed
> here (e.g., the one used by Microsoft).
> On May 13, 2013 2:54 PM, "Manuel Klimek" <klimek at google.com> wrote:
>
>> Author: klimek
>> Date: Mon May 13 07:51:40 2013
>> New Revision: 181700
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=181700&view=rev
>> Log:
>> Implements brace breaking styles.
>>
>> We now support "Linux" and "Stroustrup" brace breaking styles, which
>> gets us one step closer to support formatting WebKit, KDE & Linux code.
>>
>> Linux brace breaking style:
>> namespace a
>> {
>> class A
>> {
>>   void f()
>>   {
>>     if (x) {
>>       f();
>>     } else {
>>       g();
>>     }
>>   }
>> }
>> }
>>
>> Stroustrup brace breaking style:
>> namespace a {
>> class A {
>>   void f()
>>   {
>>     if (x) {
>>       f();
>>     } else {
>>       g();
>>     }
>>   }
>> }
>> }
>>
>> Modified:
>>     cfe/trunk/include/clang/Format/Format.h
>>     cfe/trunk/lib/Format/Format.cpp
>>     cfe/trunk/lib/Format/UnwrappedLineParser.cpp
>>     cfe/trunk/unittests/Format/FormatTest.cpp
>>
>> Modified: cfe/trunk/include/clang/Format/Format.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=181700&r1=181699&r2=181700&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/include/clang/Format/Format.h (original)
>> +++ cfe/trunk/include/clang/Format/Format.h Mon May 13 07:51:40 2013
>> @@ -101,6 +101,20 @@ struct FormatStyle {
>>    /// tab characters.
>>    bool UseTab;
>>
>> +  /// \brief Different ways to attach braces to their surrounding
>> context.
>> +  enum BraceBreakingStyle {
>> +    /// Always attach braces to surrounding context.
>> +    BS_Attach,
>> +    /// Like \c Attach, but break before braces on function, namespace
>> and
>> +    /// class definitions.
>> +    BS_Linux,
>> +    /// Like \c Attach, but break before function definitions.
>> +    BS_Stroustrup
>> +  };
>> +
>> +  /// \brief The brace breaking style to use.
>> +  BraceBreakingStyle BreakBeforeBraces;
>> +
>>    bool operator==(const FormatStyle &R) const {
>>      return AccessModifierOffset == R.AccessModifierOffset &&
>>             AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
>> @@ -122,7 +136,8 @@ struct FormatStyle {
>>             SpacesBeforeTrailingComments ==
>> R.SpacesBeforeTrailingComments &&
>>             Standard == R.Standard &&
>>             IndentWidth == R.IndentWidth &&
>> -           UseTab == R.UseTab;
>> +           UseTab == R.UseTab &&
>> +           BreakBeforeBraces == R.BreakBeforeBraces;
>>    }
>>
>>  };
>>
>> Modified: cfe/trunk/lib/Format/Format.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=181700&r1=181699&r2=181700&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/Format/Format.cpp (original)
>> +++ cfe/trunk/lib/Format/Format.cpp Mon May 13 07:51:40 2013
>> @@ -36,11 +36,21 @@ namespace llvm {
>>  namespace yaml {
>>  template <>
>>  struct
>> ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
>> -  static void enumeration(IO &io,
>> -                          clang::format::FormatStyle::LanguageStandard
>> &value) {
>> -    io.enumCase(value, "C++03", clang::format::FormatStyle::LS_Cpp03);
>> -    io.enumCase(value, "C++11", clang::format::FormatStyle::LS_Cpp11);
>> -    io.enumCase(value, "Auto", clang::format::FormatStyle::LS_Auto);
>> +  static void enumeration(IO &IO,
>> +                          clang::format::FormatStyle::LanguageStandard
>> &Value) {
>> +    IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
>> +    IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
>> +    IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
>> +  }
>> +};
>> +
>> +template<>
>> +struct
>> ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
>> +  static void
>> +  enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle
>> &Value) {
>> +    IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
>> +    IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
>> +    IO.enumCase(Value, "Stroustrup",
>> clang::format::FormatStyle::BS_Stroustrup);
>>    }
>>  };
>>
>> @@ -87,6 +97,7 @@ template <> struct MappingTraits<clang::
>>      IO.mapOptional("Standard", Style.Standard);
>>      IO.mapOptional("IndentWidth", Style.IndentWidth);
>>      IO.mapOptional("UseTab", Style.UseTab);
>> +    IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
>>    }
>>  };
>>  }
>> @@ -115,6 +126,7 @@ FormatStyle getLLVMStyle() {
>>    LLVMStyle.Standard = FormatStyle::LS_Cpp03;
>>    LLVMStyle.IndentWidth = 2;
>>    LLVMStyle.UseTab = false;
>> +  LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
>>    return LLVMStyle;
>>  }
>>
>> @@ -138,6 +150,7 @@ FormatStyle getGoogleStyle() {
>>    GoogleStyle.Standard = FormatStyle::LS_Auto;
>>    GoogleStyle.IndentWidth = 2;
>>    GoogleStyle.UseTab = false;
>> +  GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
>>    return GoogleStyle;
>>  }
>>
>>
>> Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=181700&r1=181699&r2=181700&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
>> +++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon May 13 07:51:40 2013
>> @@ -402,6 +402,10 @@ void UnwrappedLineParser::parseStructura
>>        // structural element.
>>        // FIXME: Figure out cases where this is not true, and add
>> projections for
>>        // them (the one we know is missing are lambdas).
>> +      if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
>> +          Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
>> +        addUnwrappedLine();
>> +
>>        parseBlock(/*MustBeDeclaration=*/ false);
>>        addUnwrappedLine();
>>        return;
>> @@ -577,6 +581,9 @@ void UnwrappedLineParser::parseNamespace
>>    if (FormatTok.Tok.is(tok::identifier))
>>      nextToken();
>>    if (FormatTok.Tok.is(tok::l_brace)) {
>> +    if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
>> +      addUnwrappedLine();
>> +
>>      parseBlock(/*MustBeDeclaration=*/ true, 0);
>>      // Munch the semicolon after a namespace. This is more common than
>> one would
>>      // think. Puttin the semicolon into its own line is very ugly.
>> @@ -751,8 +758,12 @@ void UnwrappedLineParser::parseRecord()
>>        }
>>      }
>>    }
>> -  if (FormatTok.Tok.is(tok::l_brace))
>> +  if (FormatTok.Tok.is(tok::l_brace)) {
>> +    if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
>> +      addUnwrappedLine();
>> +
>>      parseBlock(/*MustBeDeclaration=*/ true);
>> +  }
>>    // We fall through to parsing a structural element afterwards, so
>>    // class A {} n, m;
>>    // will end up in one unwrapped line.
>>
>> Modified: cfe/trunk/unittests/Format/FormatTest.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=181700&r1=181699&r2=181700&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/unittests/Format/FormatTest.cpp (original)
>> +++ cfe/trunk/unittests/Format/FormatTest.cpp Mon May 13 07:51:40 2013
>> @@ -4037,6 +4037,42 @@ TEST_F(FormatTest, ConfigurableUseOfTab)
>>                 Tab);
>>  }
>>
>> +TEST_F(FormatTest, LinuxBraceBreaking) {
>> +  FormatStyle BreakBeforeBrace = getLLVMStyle();
>> +  BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
>> +  verifyFormat("namespace a\n"
>> +               "{\n"
>> +               "class A\n"
>> +               "{\n"
>> +               "  void f()\n"
>> +               "  {\n"
>> +               "    if (true) {\n"
>> +               "      a();\n"
>> +               "      b();\n"
>> +               "    }\n"
>> +               "  }\n"
>> +               "}\n"
>> +               "}",
>> +               BreakBeforeBrace);
>> +}
>> +
>> +TEST_F(FormatTest, StroustrupBraceBreaking) {
>> +  FormatStyle BreakBeforeBrace = getLLVMStyle();
>> +  BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
>> +  verifyFormat("namespace a {\n"
>> +               "class A {\n"
>> +               "  void f()\n"
>> +               "  {\n"
>> +               "    if (true) {\n"
>> +               "      a();\n"
>> +               "      b();\n"
>> +               "    }\n"
>> +               "  }\n"
>> +               "}\n"
>> +               "}",
>> +               BreakBeforeBrace);
>> +}
>> +
>>  bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
>>    for (size_t i = 1; i < Styles.size(); ++i)
>>      if (!(Styles[0] == Styles[i]))
>> @@ -4113,6 +4149,14 @@ TEST_F(FormatTest, ParsesConfiguration)
>>    CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
>>    CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit,
>> 1234u);
>>
>> +  Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
>> +  CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
>> +              FormatStyle::BS_Attach);
>> +  CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
>> +              FormatStyle::BS_Linux);
>> +  CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
>> +              FormatStyle::BS_Stroustrup);
>> +
>>  #undef CHECK_PARSE
>>  #undef CHECK_PARSE_BOOL
>>  }
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130514/e8c5d8d0/attachment.html>


More information about the cfe-commits mailing list