使 clang 格式的订单以不区分大小写的方式包含

Joe*_*ico 5 c++ clang-format

我正在尝试配置 .clang-format。我的目的是让它保留我的包含组,因为它们在代码中,但按字母顺序对它们进行排序。我设法接近我想要的IncludeBlocks: Preserve,但它以区分大小写的方式包含在每个块中:

这是我应用格式后得到的结果

#include "A1.h"
#include "B2.h"
#include "a2.h"
#include "b1.h"
Run Code Online (Sandbox Code Playgroud)

这就是我想要实现的目标

#include "A1.h"
#include "a2.h"
#include "b1.h"
#include "B2.h"
Run Code Online (Sandbox Code Playgroud)

我正在使用 clang-format 版本 10.0。我的 .clang 格式文件,以防相关:

---
# Brompton's Clang Format file v0.0.1
#
# Changelog:
# v0.0.1 (25/9/2020):
# - First version of this file


# Base it on Google's Standard, with 4 spaces as indentation
BasedOnStyle: Google
IndentWidth: '4'
Standard: Cpp11
UseTab: Never

# Settings for C/C++
Language: Cpp
# 120 chars per line, reflow comments so they stay within limits
ColumnLimit: '120'
ReflowComments: 'true'

# Short includes alphabetically. Will respect "include groups"
SortIncludes: 'true'
IncludeBlocks: Preserve

# These control when to align text under certain conditions
# Align arguments after an open bracket ( '(', '[', '{'), on statements that are too long for a single line
AlignAfterOpenBracket: Align
# Align adjacent macros and variables, for enhanced readability.
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
# Align the '\' on scaped newlines, because it looks neater
AlignEscapedNewlines: Left

# These keep statements that could go on a single line to be collapsed. They take more space, but are more readable
# ... Code Blocks { ... }
AllowShortBlocksOnASingleLine: 'false'
# ... Switch cases
AllowShortCaseLabelsOnASingleLine: 'false'
# ... short functions, like getters and setters
AllowShortFunctionsOnASingleLine: Empty
# ... single line ifs
AllowShortIfStatementsOnASingleLine: Never
# ... single line loops
AllowShortLoopsOnASingleLine: 'false'

# Provides a more compact view when a function parameters or arguments take more than one line
BinPackArguments: 'true'
BinPackParameters: 'true'

# Indent cases on a switch, instead of leaving them at the same level than the switch statement.
IndentCaseLabels: 'true'
# Indent preprocessor the same way than code
IndentPPDirectives: BeforeHash
IndentWrappedFunctionNames: 'true'
NamespaceIndentation: All

# Put the pointer operator next to the type, instead of next to the variable name:
PointerAlignment: Left

# All about those extra spaces...
# ... remove spaces after an open-curly brace and before a close-curly brace
Cpp11BracedListStyle: 'true'
# ... space before a list, when used to initialise an object
SpaceBeforeCpp11BracedList: 'true'
# ... logical not (!) next to expression
SpaceAfterLogicalNot: 'false'
# ... space between the '=' on assignments
SpaceBeforeAssignmentOperators: 'true'
# ... space after and before the parentheses in a C-style cast.
SpacesInCStyleCastParentheses: 'false'
# ... spaces inside () and []
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
# ... spaces before a ( or a [, but only on control statements.
SpaceBeforeParens: 'ControlStatements'

...
Run Code Online (Sandbox Code Playgroud)

谢谢!

dfr*_*fri 3

这是从 LLVM 12 开始添加的(将在 Clang 13 中发布?有点令人困惑);引用LLVM 12/Clang 13 正在进行的发行说明[重点是我的]:

clang 格式

选项SortIncludes已从布尔更新为具有向后兼容性的枚举。除了之前的true/false 状态(现在CaseInsensitive/ Never)之外,还添加了第三个状态 ( CaseSensitive),这会导致按字母顺序排序,并使用大小写作为决胜局。

// Never (previously false)
#include "B/A.h"
#include "A/B.h"
#include "a/b.h"
#include "A/b.h"
#include "B/a.h"

// CaseInsensitive (previously true)
#include "A/B.h"
#include "A/b.h"
#include "B/A.h"
#include "B/a.h"
#include "a/b.h"

// CaseSensitive
#include "A/B.h"
#include "A/b.h"
#include "a/b.h"
#include "B/A.h"
#include "B/a.h"
Run Code Online (Sandbox Code Playgroud)

最新的 Clang Format 文档已更新为以下示例CaseSensitive

IncludeCategories (std::vector<IncludeCategory>)

[...]

每个正则表达式都可以使用 field 标记为区分大小写 CaseSensitive,但默认情况下并非如此。

要在 .clang-format 文件中进行配置,请使用:

IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
    SortPriority:    2
    CaseSensitive:   true
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '<[[:alnum:].]+>'
    Priority:        4
  - Regex:           '.*'
    Priority:        1
    SortPriority:    0
Run Code Online (Sandbox Code Playgroud)