I want to configure clang-format to sort in C++ the included headers as follows:
- main header (associated with the current cpp file),
- local headers included via "",
- other headers included via <>,
- headers from specific external libraries (e.g. boost, catch2),
- system/standard headers.
I'm using clang-format 8.0.0 on macOS. My current configuration (snippet related only to includes) is as follows:
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# Headers in <> without extension.
- Regex: '<([A-Za-z0-9\/-_])+>'
Priority: 4
# Headers in <> from specific external libraries.
- Regex: '<((\bboost\b)|(\bcatch2\b))\/([A-Za-z0-9.\/-_])+>'
Priority: 3
# Headers in <> with extension.
- Regex: '<([A-Za-z0-9.\/-_])+>'
Priority: 2
# Headers in "" with extension.
- Regex: '"([A-Za-z0-9.\/-_])+"'
Priority: 1
In this configuration I assume, that system/standard headers are without extension. It will not work for UNIX/POSIX headers. Main header is automatically detected and assigned the priority 0. So far, all seems working as expected, except for the category for external libraries. It looks like clang-format is assigning it to the priority 2.
Expected result:
#include "test.h"
#include <allocator/region.hpp>
#include <page.hpp>
#include <page_allocator.hpp>
#include <test_utils.hpp>
#include <utils.hpp>
#include <zone_allocator.hpp>
#include <catch2/catch.hpp> // <--------
#include <array>
#include <cmath>
#include <cstring>
#include <map>
Actual result:
#include "test.h"
#include <allocator/region.hpp>
#include <catch2/catch.hpp> // <--------
#include <page.hpp>
#include <page_allocator.hpp>
#include <test_utils.hpp>
#include <utils.hpp>
#include <zone_allocator.hpp>
#include <array>
#include <cmath>
#include <cstring>
#include <map>
How to configure priority 3 to have the expected result?