user7431005
user7431005

Reputation: 4539

clang format: disable ordering includes

In our C++ project, the order of our includes is regularly changed. This is a problem since we are using some third-party libraries which require a specific include order to avoid problems.

I know, this is bad but we have to deal with it.

Unfortunately, the order of our includes is regularly changed and I suppose that this is due to clang-format. I found a page where you can specify a variable includeCategories. However, I do not fully understand how it works. I simply want to completely disable the ordering of includes. How can I do this?

Upvotes: 32

Views: 30742

Answers (4)

trojanfoe
trojanfoe

Reputation: 122391

With more recent versions of clang-format it seems you need both:

IncludeBlocks: Preserve
SortIncludes: Never

Upvotes: 5

Delta
Delta

Reputation: 148

SortIncludes: Never to .clang-format

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

SortIncludes [Possible values]: Never , CaseSensitive , CaseInsensitive

Upvotes: 1

Martin Morterol
Martin Morterol

Reputation: 2870

Have you tried: SortIncludes: false?

You can generate a .clang-format with preview here: https://zed0.co.uk/clang-format-configurator/

Upvotes: 49

Tarek Dakhran
Tarek Dakhran

Reputation: 2161

To disable sorting for the whole project use SortIncludes:false in .clang-format.

To disable clang-format only for a specific file region, use // clang-format off/on comments.

// clang-format off
#include <b.h>
#include <a.h>
#include <c.h>
// clang-format on
#include <d.h>
#include <e.h>

Upvotes: 36

Related Questions