clang-format 当前将所有编译指示移动到第一列。 clang-format 之前的一个例子:
for (int i = 0; i < 4; ++i) {
#pragma UNROLL
// ...some code...
}
clang-format 后的相同代码:
for (int i = 0; i < 4; ++i) {
#pragma UNROLL
// ...some code...
}
有没有办法让 clang-format 完全忽略 pragma 行而不更改源代码(即不用
// clang-format off
弄乱源代码)?例如使用正则表达式?
这与这个问题有关(我想避免安装第三方工具),并希望通过这个错误报告来解决。
此外,虽然
clang-format off
对于带有编译指示的行受到尊重,但注释行本身将缩进到编译指示本应缩进的内容(使用clang-format 6.0.0):
for (int i = 0; i < 4; ++i) {
// clang-format off
#pragma UNROLL
// clang-format on
// ...some code...
}
如果您只使用很少的编译指示,您可以尝试将它们伪装成常规命令。根据compiler explorer,以下内容对编译器有正确的效果,同时格式不会被clang-format破坏。
另一个好处是,您可以使用不同的定义,具体取决于编译器(如这个问题)
#define UNROLL _Pragma("unroll")
void f()
{
volatile int x = 0;
UNROLL for (int i = 0; i < 100; ++i)
{
x = x + i;
}
}