1
votes

We have include mechanics that protects some definitions when including external libraries, we would like to keep them as they are when formatting code, eventually ordering alphabetically the contents of the block.

e.g:

#include <ExternalIncludeBegin.h>
#    include <somelib/someheader.h>
#    include <somelib/anotherheader.h>
#include <ExternalIncludeEnd.h>

Right now, clang-format transforms this block in

#include <ExternalIncludeBegin.h>
#include <somelib/someheader.h>
#include <somelib/anotherheader.h>
#include <ExternalIncludeEnd.h>

but I would like to keep the original indentation, if possible without having to encapsulate everything with new code (the codebase we would like to format is pretty old and big), is there anything clang-format can do for me here ?

1

1 Answers

0
votes

Take a look at "Disabling format" feature

Disabling Formatting on a Piece of Code
Clang-format understands also special comments that switch formatting in a delimited range. The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted. The comments themselves will be formatted (aligned) normally.

int formatted_code;
// clang-format off
    void    unformatted_code  ;
// clang-format on
void formatted_code_again;

more details: https://clang.llvm.org/docs/ClangFormatStyleOptions.html

So, you also could use “sed” or other tool in order to inject clang-specific comments in the whole project.