如何更改“ clang-format”选项以防止大括号

时间:2018-11-01 02:25:47

标签: c clang clang-format

我有一个名为asdf.c的C文件(即使无法编译,您也可以重现此问题):

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},
        },
};

现在,如果我运行clang-format -i asdf.c(使用6.0.1版本,但我也可以在版本5中进行复制),我就让clang-format为我进行格式化。

但是,我希望我的代码符合某些Linux style guides(毕竟,我使用C而不是C ++进行编程),所以我同时使用checkpatch.pl检查我的代码样式。但是Checkpatch抱怨clang-format如何格式化代码:

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

我的问题是,如何配置clang-format以避免这些错误/警告?

我当前的.clang-format配置如下:

BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: false

1 个答案:

答案 0 :(得分:1)

如果您将BreakBeforeBinaryOperators: All添加到.clang-format文件中,则clang-format不会将开括号括在代码中(如果它们已经在前几行中)。例如,以下代码不会被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},
    },
};