This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Always break line after enum opening brace
ClosedPublic

Authored by osandov on Apr 7 2020, 2:55 PM.

Details

Summary

clang-format currently puts the first enumerator on the same line as the
enum keyword and opening brace if it fits (for example, for anonymous
enums if IndentWidth is 8):

$ echo "enum { RED, GREEN, BLUE };" | clang-format -style="{BasedOnStyle: llvm, ColumnLimit: 15, IndentWidth: 8}"
enum { RED,
       GREEN,
       BLUE };

This doesn't seem to be intentional, as I can't find any style guide that
suggests wrapping enums this way. Always force the enumerator to be on a new
line, which gets us the desired result:

$ echo "enum { RED, GREEN, BLUE };" | ./bin/clang-format -style="{BasedOnStyle: llvm, ColumnLimit: 15, IndentWidth: 8}"
enum {
        RED,
        GREEN,
        BLUE
};

Test Plan:

New test added. Confirmed test failed without change and passed with change by
running:

$ ninja FormatTests && ./tools/clang/unittests/Format/FormatTests

Diff Detail

Event Timeline

osandov created this revision.Apr 7 2020, 2:55 PM
MyDeveloperDay added a comment.EditedApr 8 2020, 2:59 AM

Did you try:

BreakBeforeBraces: Custom
BraceWrapping:
  AfterEnum: true

with AfterEnum:true

$ clang-format enum.cpp
enum
{
  A,
};

enum
{
  B,
  C,
  D
};

enum
{
  E
};

with AfterEnum:false

$ clang-format enum.cpp
enum {
  A,
};

enum { B, C, D };

enum { E };

I believe example is not following the style of BraceWrapping.AfterEnum by virtue of the trailing ,

I think I'd expect the correct behaviour to be

enum { A, };

or

enum { 
   A, 
};

depending on that setting

MyDeveloperDay added a comment.EditedApr 8 2020, 3:05 AM

I think what is interesting is

BreakBeforeBraces: Custom
BraceWrapping:
    AfterEnum: false

gets messed up completely by the , and that IS a bug in my view

$ clang-format enum.cpp
enum {
  A,
};

enum { B, C, D };

enum {
  B,
  C,
  D,
};

enum { E };

The style guide I'm following (the Linux kernel style) wants AfterEnum: false. A cursory search suggests that people treat this trailing comma behavior as a feature (https://stackoverflow.com/questions/23072223/clang-format-style-options-for-enums). However, I think this is separate from my issue. Consider the following example which doesn't have a trailing comma and doesn't fit on one line:

$ cat enum.cpp
enum { EOF, VOID, CHAR, SHORT, INT, LONG, SIGNED, UNSIGNED, BOOL, FLOAT, DOUBLE, COMPLEX, CONST, RESTRICT, VOLATILE, ATOMIC, STRUCT, UNION, ENUM, LPAREN, RPAREN, LBRACKET, RBRACKET, ASTERISK, DOT, NUMBER, IDENTIFIER };

The current code formats this very strangely:

$ clang-format -style="{BasedOnStyle: llvm, IndentWidth: 8}" enum.cpp            
enum { EOF,
       VOID,
       CHAR,
       SHORT,
       INT,
       LONG,
       SIGNED,
       UNSIGNED,
       BOOL,
       FLOAT,
       DOUBLE,
       COMPLEX,
       CONST,
       RESTRICT,
       VOLATILE,
       ATOMIC,
       STRUCT,
       UNION,
       ENUM,
       LPAREN,
       RPAREN,
       LBRACKET,
       RBRACKET,
       ASTERISK,
       DOT,
       NUMBER,
       IDENTIFIER };

With this change, I get what I expect:

$ ./bin/clang-format -style="{BasedOnStyle: llvm, IndentWidth: 8}" enum.cpp
enum {
        EOF,
        VOID,
        CHAR,
        SHORT,
        INT,
        LONG,
        SIGNED,
        UNSIGNED,
        BOOL,
        FLOAT,
        DOUBLE,
        COMPLEX,
        CONST,
        RESTRICT,
        VOLATILE,
        ATOMIC,
        STRUCT,
        UNION,
        ENUM,
        LPAREN,
        RPAREN,
        LBRACKET,
        RBRACKET,
        ASTERISK,
        DOT,
        NUMBER,
        IDENTIFIER
};

I understand now what you are trying to achieve, could you capture something more like that in your tests too.. (just so we don't break this change later)

osandov updated this revision to Diff 256352.Apr 9 2020, 12:10 PM
osandov edited the summary of this revision. (Show Details)

Update summary and test case to better reflect the issue

This revision is now accepted and ready to land.Apr 10 2020, 12:44 AM

Thank you! I don't have commit access. How can I get this committed?

This revision was automatically updated to reflect the committed changes.