clang 格式总是缩进结构初始值设定项

问题描述 投票:0回答:1

我当前的

.clang-format
文件有一个非常奇怪的行为。

这里是文件:

---
BasedOnStyle: LLVM
ColumnLimit: 200
IndentWidth: 4
UseTab: Always
TabWidth: 4
BreakBeforeBraces: Linux

考虑这个示例 C 代码,它将按预期格式化它:

struct foo {
    char a;
    int b;
};

static const struct foo data[] = {
    {'a', 1},
    {'b', 2},
    {'c', 3},
};

但是,如果我在我的数组中添加一个空的

{}
元素,clang-format 会将完整的数组初始化放在一行中:

struct foo {
    char a;
    int b;
};

static const struct foo data[] = {{'a', 1}, {'b', 2}, {'c', 3}, {}};

有没有办法告诉

clang-format
允许一行中有一个空元素并像第一个示例一样正确格式化代码?

编辑1:

作为 Nullndr 建议添加一个跟踪

,
将获得 clang-format 以创建正确的格式。

然而,让我们重新表述这个问题:有没有办法告诉 clang-format 即使没有尾随也能正确格式化

,

编辑2: 这似乎是 clang 格式的错误。我在这里报告:https://github.com/llvm/llvm-project/issues/61585

c code-formatting clang-format
1个回答
1
投票

可悲的是,即使是版本 15,这仍然是不可能的,docs.

这不仅仅是数组的问题,结构也是如此,看一看:

static const struct foo data = {'a', 1}; // see the missing `,`

如果你放一个逗号,它会正确地缩进结构:

static const struct foo data = {
  'a',
  1, // see the trailing `,` 
};

这里有很多关于这个的回应,例如见这里

另一个技巧是在行尾添加评论:

static const struct foo data[] = {
  {'a', 1}, //
  {'b', 2}, //
  {'c', 3}  //
};

我从来没有用过这个,因为我觉得它很丑

© www.soinside.com 2019 - 2024. All rights reserved.