19

Take this example before clang-format runs:

struct ApplicationState app_state = {
    .signal = {
        .use_crash_handler = true,
        .use_abort_handler = true,
    },
    .exit_code_on_error = {
        .python = 0,
    }
};

After running, clang-format applies as follows:

struct ApplicationState app_state = {.signal =
                                             {
                                                     .use_crash_handler = true,
                                                     .use_abort_handler = true,
                                             },
                                     .exit_code_on_error = {
                                             .python = 0,
                                     }};

Is there a way to add a newline after the brace, before the struct member so it's more like the first example and not like the second?

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • 1
    The first version is **before** clang-format runs. The question is how to maintain this format and prevent the awkward second example. – ideasman42 Nov 21 '17 at 10:39
  • I don't know clang but with AStyle I'm almost sure there's a comment you can put on top of your structure to disable auto-format. Try to check if something similar exists for clang. – Tim Nov 21 '17 at 10:41
  • @TimF, There is. See [Disabling Formatting on a Piece of Code](http://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code) – ahogen Dec 29 '17 at 01:39
  • 3
    @TimF, of course formatting can be disabled as needed as a last resort. Ideally use of this is kept to a minimum though, and formatting the struct in the Q is standard C99, nothing unusual. – ideasman42 Dec 29 '17 at 09:47
  • See also: https://stackoverflow.com/questions/33656800/clang-format-line-breaks – Mateen Ulhaq Nov 10 '19 at 15:54
  • Have you found a configuration solution? I'm facing similar issues with my C++ code. Sure I could use comments and what not, but that just isn't pretty. – Arsenal Dec 03 '20 at 11:03
  • @Arsenal added an answer. While it's a bit of a workaround for this limitation in clang-format in practice it's OK. – ideasman42 Dec 03 '20 at 22:18
  • 1
    Okay, thanks for that. Maybe I should write a feature request for an option to handle this... – Arsenal Dec 04 '20 at 07:03

2 Answers2

16

Currently clang-format doesn't have a useful way of controlling this (as of version 14.0).

While BreakBeforeBinaryOperators: All does force wrapping (see @eric-backus' s answer), it impacts formatting in many other places too, unrelated to struct declaration.

You can however, workaround this simply by using a trailing comma.


Before:

struct ApplicationState app_state = {.signal =
                                             {
                                                     .use_crash_handler = true,
                                                     .use_abort_handler = true,
                                             },
                                     .exit_code_on_error = {
                                             .python = 0,
                                     }};

After:


struct ApplicationState app_state = {
    .signal = {
        .use_crash_handler = true,
        .use_abort_handler = true,
    },
    .exit_code_on_error = {
        .python = 0,
    },
};
/*   ^ notice trailing comma on the second last line! */
ideasman42
  • 42,413
  • 44
  • 197
  • 320
1

The primary thing you need is:

BreakBeforeBinaryOperators: All

You could instead set the overall style to WebKit, because that sets BreakBeforeBinaryOperators to All.

Clearly, clang-format must see the . as a binary operator. I'm not sure if it should, but that's what it seems to do.


I tested this with clang-format 6.0.0. Presumably newer versions would work the same, but I haven't tested so I can't be sure.

Eric Backus
  • 1,614
  • 1
  • 12
  • 29