【发布时间】:2019-04-22 09:42:13
【问题描述】:
我想配置 clang-format 以在 C++ 中对包含的标头进行如下排序:
- 主标头(与当前 cpp 文件关联),
- 通过“”包含的本地标头,
- 通过 , 包含的其他标头
- 来自特定外部库(例如 boost、catch2)的标头,
- 系统/标准头文件。
我在 macOS 上使用 clang-format 8.0.0。 我目前的配置(sn-p只和include相关)如下:
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
在此配置中,我假设系统/标准标头没有扩展名。它不适用于 UNIX/POSIX 标头。主标头被自动检测并分配优先级 0。到目前为止,一切似乎都按预期工作,除了外部库的类别。看起来 clang-format 将其分配给优先级 2。
预期结果:
#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>
实际结果:
#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>
如何配置优先级 3 以获得预期结果?
【问题讨论】:
标签: c++ regex clang llvm-clang clang-format