如何使用clang格式缩进C ++编译指示? [英] How could I indent C++ pragma using clang-format?
问题描述
我正在使用 vim-autoformat
,它使用 clang-format
作为外部格式化程序。
I am using vim-autoformat
, which uses clang-format
as external formatter.
似乎 clang格式
不会缩进C ++ #pragma
。例如:
It seems that clang-format
won't indent the C++ #pragma
. For example:
#include <omp.h>
#include <cstdio>
int main()
{
#pragma omp parallel for
for (int i = 0; i < 10; ++i)
{
puts("demo");
}
return 0;
}
我希望将其格式化为:
#include <omp.h>
#include <cstdio>
int main()
{
#pragma omp parallel for
for (int i = 0; i < 10; ++i)
{
puts("demo");
}
return 0;
}
我检查了语言格式,但是找不到我可以使用的选项。
I checked clangformat, but didn't find which option I could use.
推荐答案
来晚了,但这是您正在寻找的解决方案。
它将格式说明和代码块一起格式化。
It's been late but this is the solution you are looking for. It formats the pragma along with the code block.
https://github.com/MedicineYeh/p-clang-format
主要概念是替换字符串,以便格式化程序在这些编译指示上使用正确规则。动机示例如下。
The main concept is replacing the string so that the formatter uses the "correct" rules on these pragmas. The motivative example is as following.
# Replace "#pragma omp" by "//#pragma omp"
sed -i 's/#pragma omp/\/\/#pragma omp/g' ./main.c
# Do format
clang-format ./main.c
# Replace "// *#pragma omp" by "#pragma omp"
sed -i 's/\/\/ *#pragma omp/#pragma omp/g' ./main.c
这篇关于如何使用clang格式缩进C ++编译指示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!