Indenting preprocessor directives with clang-format

[亡魂溺海] 提交于 2020-11-30 17:36:38

问题


I am working on a c++ project where I am using a lot of #pragma omp. I use the wonderful clang-format for tidiness but it always deletes the indentation for all preprocessor directives. Is there a way to change that behavior? Or is there another formatting tool that is more recommendable? Or should I avoid using these tools at all?


回答1:


You might want to just patch it yourself and make a pull request.

It's not that hard, I made a similarly mundane pull request once. The clang-format code is pretty tidy. Clang-format already handles code comments in the way that you want, aligning them to the surrounding code (at least it has an option to enable this) so making a patch to treat certain PP directives the same way should be straightforward.

Alternatively, you can just write the patch yourself and compile clang yourself from source with the extra option, for use in your project. I also did this before I decided to send them the patch.

It seriously took me only a few hours to figure out how to do this, their code is much cleaner than the code of many other open source projects.




回答2:


As of version 6.0, the option IndentPPDirectives can be used. Usage is described in this review.

Using IndentPPDirectives: None results in:

#if FOO
#if BAR
#include <foo>
#endif
#endif

While IndentPPDirectives: AfterHash gives:

#if FOO
#  if BAR
#    include <foo>
#  endif
#endif



回答3:


It's been late but this is the solution you are looking for. It formats the pragma along with the code block. You can use this before they finally support the pragma indentation.

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



回答4:


astyle (Artistic Style) indents #pragma omp nicely with the code, out-of-the-box. There doesn't even seem to be an option to change the behavior. Only line continuations aren't indented, as shown in the example—I would prefer line continuations were indented, perhaps 8 spaces, under omp. Other pragmas are aligned left.

void foo()
{
        #pragma omp parallel
#pragma omp master
    {
#pragma omp task depend( some_expression ) \
            depend( other_expression ) \
    priority(1)
        {
            code();
        }
    }

    #pragma other
    code();
}

becomes

void foo()
{
    #pragma omp parallel
    #pragma omp master
    {
        #pragma omp task depend( some_expression ) \
        depend( other_expression ) \
        priority(1)
        {
            code();
        }
    }

#pragma other
    code();
}

There is an Astyle Visual Studio extension.



来源:https://stackoverflow.com/questions/24476165/indenting-preprocessor-directives-with-clang-format

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