[cfe-dev] Fwd: clang-format line breaks in nested struct initializer

BOUGET Quentin via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 25 02:17:55 PST 2019


Hello,

I tried out clang-format (version 5.0.1) today and was wondering why with this
configuration:

---
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Linux
IndentWidth: 4
UseTab: Never
...

The following formatting happens:

Input                           | Output
--------------------------------|------------------------------------
                                 |
#include <stdlib.h>             | /****************************
                                 |  *                          *
struct point {                  |  *                          *
     int x;                      |  *                          *
     int y;                      |  *                          *
};                              |  *       Same format        *
                                 |  *                          *
struct segment {                |  *                          *
     struct point start;         |  *                          *
     struct point end;           |  *                          *
};                              |  ****************************/
                                 |
int                             | int
main()                          | main()
{                               | {
     struct segment segment = {  |     struct segment segment = {
         .start = {              |         .start =
             .x = 0,             |             {
             .y = 0,             |                 .x = 0,
         },                      |                 .y = 0,
         .end = {                |             },
             .x = 0,             |         .end =
             .y = 2,             |             {
         },                      |                 .x = 0,
     };                          |                 .y = 2,
                                 |             },
     ...                         |         },
     return EXIT_SUCCESS;        |     };
}                               |
                                 |     ...
                                 |
                                 |     return EXIT_SUCCESS;
                                 | }
                                 |

(notice how braces are placed in the segment's initializer)

The part I cannot figure out is why clang-format adds a line break after
".start =" but not after "struct segment segment =".

(I mean, ideally, I would like clang-format to not add any line breaks at all,
but if it must add some, I would prefer it adds them consistently. =])

---

I do not know if this helps, but I found a post on stackoverflow that suggested
setting "BreakBeforeBinaryOperators" to "All" to fix this.
I was a bit sceptical considering what I thought the parameter controlled but
tried it anyway... turns out it works! (no line breaks added anywhere)

If someone has time, I would also like to know how allowing clang-format to
break lines before the "=" sign (which is what I understand setting
"BreakBeforeBinaryOperators" to "All" does) helps producing the result I was
looking for. If anything, I would have thought it made it more likely that
a line break would be added after "struct segment segment =".

Regards,
Quentin

PS: I already sent this email once a week ago, before I subscribed to the list.
It has been under review by an admin of the list since then. I don't really
mind, but maybe someone should check on this person.



More information about the cfe-dev mailing list