How could I indent C++ pragma using clang-format?

↘锁芯ラ 提交于 2020-01-02 01:09:17

问题


I am using vim-autoformat, which uses clang-format as external formatter.

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;
}

I would like to have it formatted into :

#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.


回答1:


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


来源:https://stackoverflow.com/questions/31353748/how-could-i-indent-c-pragma-using-clang-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!