【问题标题】:How to change `clang-format` options to prevent braking braces如何更改“clang-format”选项以防止制动支架
【发布时间】:2018-11-01 02:25:47
【问题描述】:

我有一个名为 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

【问题讨论】:

  • 您是否查看过this example 或阅读过Linux clang-format guide
  • @Myst 是的,如您所见,我当前的.clang-format 配置与示例中提供的配置相匹配(加上一些规则以防止单行出现短函数/循环)。此外,即使我使用 Linux 内核中的 .clang-format 配置文件,生成的样式也会与 checkpatch.pl 预期格式冲突(即:大括号 { 应该在前一行)。
  • 根据指南,有时“[clang-format] 并不完美,也不涵盖所有情况,但足以提供帮助”...考虑在麻烦部分之前添加// clang-format off并手动格式化该部分(而不是使用// clang-format on 重新激活)。
  • @Myst 也许不是我正在寻找的答案,但这实际上是一种解决方法。 :-)

标签: c clang clang-format


【解决方案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},
    },
};

【讨论】:

  • 谢谢。这实际上解决了提供的源代码示例的问题。遗憾的是,这对源代码的其余部分产生了一些意想不到的副作用(需要进行许多更改)。我希望有一个选项可以仅对大括号“{”应用这些中断,但保留所有二元运算符的样式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
相关资源
最近更新 更多