I want to configure clang-format to sort in C++ the included headers as follows:
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?
I got it working by using and modifying an example from clang-format docs for this option:
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# Headers in <> without extension.
- Regex: '<([A-Za-z0-9\Q/-_\E])+>'
Priority: 4
# Headers in <> from specific external libraries.
- Regex: '<(catch2|boost)\/'
Priority: 3
# Headers in <> with extension.
- Regex: '<([A-Za-z0-9.\Q/-_\E])+>'
Priority: 2
# Headers in "" with extension.
- Regex: '"([A-Za-z0-9.\Q/-_\E])+"'
Priority: 1
In particular, I changed the priority 3 regex to be more like from the original example:
'^(<|"(gtest|gmock|isl|json)/)'
Also, I added the \Q and \E modifiers to avoid the problem mentioned by the Julio. Now everything works as expected. However I still don't know why the solution from the question post doesn't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With