1

I have a C file named asdf.c (you can reproduce the problem with this, even if it does not compile):

struct turn_parameters turns[][NUM_MODES] =
    {
    [MOVE_LEFT] =
        {
        {0.02, .448, 166, 260, -3. * PI},
        {0.02, .448, 166, 260, -3. * PI},
        {0.02, .448, 166, 260, -3. * PI},
        },
    [MOVE_RIGHT] =
        {
        {0.02, .448, 166, 260, 3. * PI},
        {0.02, .448, 166, 260, 3. * PI},
        {0.02, .448, 166, 260, 3. * PI},
        },
    [MOVE_LEFT_90] =
        {
        {-0.05, .8, 219, 291, -2.3 * PI},
        {-0.05, .8, 219, 291, -2.3 * PI},
        {-0.05, .8, 219, 291, -2.3 * PI},
        },
    [MOVE_RIGHT_90] =
        {
        {-0.05, .8, 219, 291, 2.3 * PI},
        {-0.05, .8, 219, 291, 2.3 * PI},
        {-0.05, .8, 219, 291, 2.3 * PI},
        },
    [MOVE_LEFT_180] =
        {
        {-0.04, .7, 400, 479, -2.5 * PI},
        {-0.04, .7, 400, 479, -2.5 * PI},
        {-0.04, .7, 400, 479, -2.5 * PI},
        },
    [MOVE_RIGHT_180] =
        {
        {-0.04, .7, 400, 479, 2.5 * PI},
        {-0.04, .7, 400, 479, 2.5 * PI},
        {-0.04, .7, 400, 479, 2.5 * PI},
        },
};

Now, if I run clang-format -i asdf.c (using version 6.0.1, but I can reproduce it with version 5 too), I let clang-format do the formatting for me.

However, I would like my code to comply with some Linux style guides (after all, I am programming in C, not C++), so I am using checkpatch.pl at the same time to check my code style. Checkpatch, however, is complaining about how clang-format formatted the code:

src/asdf.c:2: ERROR: that open brace { should be on the previous line
src/asdf.c:4: WARNING: Statements should start on a tabstop
src/asdf.c:4: ERROR: that open brace { should be on the previous line
src/asdf.c:10: WARNING: Statements should start on a tabstop
src/asdf.c:10: ERROR: that open brace { should be on the previous line
src/asdf.c:16: WARNING: Statements should start on a tabstop
src/asdf.c:16: ERROR: that open brace { should be on the previous line
src/asdf.c:22: WARNING: Statements should start on a tabstop
src/asdf.c:22: ERROR: that open brace { should be on the previous line
src/asdf.c:28: WARNING: Statements should start on a tabstop
src/asdf.c:28: ERROR: that open brace { should be on the previous line
src/asdf.c:34: WARNING: Statements should start on a tabstop
src/asdf.c:34: ERROR: that open brace { should be on the previous line

My question is, how can I configure clang-format to avoid those errors/warnings?

My current .clang-format configuration is as follows:

BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
4
  • 1
    Did you review this example or read the Linux clang-format guide.
    – Myst
    Commented Nov 1, 2018 at 2:47
  • @Myst Yeah, as you can see, my current .clang-format configuration matches the one provided in the example (plus some rules to prevent short functions/loop on a single line). Also, even if I use the .clang-format configuration file from the Linux kernel, the resulting style conflicts with the checkpatch.pl expected format (i.e.: open braces { should be on the previous line).
    – Peque
    Commented Nov 1, 2018 at 16:38
  • 1
    According to the guide, Sometimes "[clang-format] is not perfect nor covers every single case, but it is good enough to be helpful"... consider adding // clang-format off before the troublesome section and formatting that part by hand (than use // clang-format on to reactivate).
    – Myst
    Commented Nov 1, 2018 at 17:08
  • @Myst Maybe not the answer I was looking for, but that actually works as a workaround. :-)
    – Peque
    Commented Nov 4, 2018 at 15:12

1 Answer 1

1

If you add BreakBeforeBinaryOperators: All to your .clang-format file, clang-format will not wrap the opening braces in your code if they are already on the previous lines. For example, the following code will not be ill-formatted by clang-format:

struct turn_parameters turns[][NUM_MODES] = {
    [MOVE_LEFT] = {
        {0.02, .448, 166, 260, -3. * PI},
        {0.02, .448, 166, 260, -3. * PI},
        {0.02, .448, 166, 260, -3. * PI},
    },
    [MOVE_RIGHT] = {
        {0.02, .448, 166, 260, 3. * PI},
        {0.02, .448, 166, 260, 3. * PI},
        {0.02, .448, 166, 260, 3. * PI},
    },
};
1
  • Thanks. That actually solved the issue with the provided source code example. Sadly, this had some unintended side effects on the rest of the source code (many changes required). I wish there was an option to apply those breaks only for the braces "{", but left all the binary operators' style as they were before.
    – Peque
    Commented Nov 4, 2018 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.