Indenting preprocessor directives with clang-format

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

By manual inspection of the various Clang-Format Style Options pages, I have determined that as of Clang-format version 9, a 3rd (and best, in my opinion) option came out, called BeforeHash.

Note: as of the time of this writing, Clang 12 is out. For the latest Clang-format options documentation for whatever version is currently out, see here: https://clang.llvm.org/docs/ClangFormatStyleOptions.html.

In your .clang-format file, you can therefore specify 3 options, as follows:

1. No indenting

IndentPPDirectives: None

Example:

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

2. Indent after the hash (#)

IndentPPDirectives: AfterHash

Example:

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

3. (Newest and best option in my opinion--available as of Clang-Format version 9) Indent before the hash (#)

IndentPPDirectives: BeforeHash

Example:

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

How to install the latest version of clang-format on Ubuntu

...so that you can get access to the version 9 or later feature just above:

See my detailed instructions here. The latest version at this moment is 14.0.0.

References

For all this documentation, as well as for the source of the exact examples I have used above, see the IndentPPDirectives section of the LLVM Clang-format Style Options official documentation here: https://clang.llvm.org/docs/ClangFormatStyleOptions.html.


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.


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

Edit: see @Gabriel Staples' answer for details on the BeforeHash option introduced in clang-format version 9.