【发布时间】: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