使 clang-format 命令以不区分大小写的方式包含

Make clang-format order includes in a case-insensitive way

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

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

#include "A1.h"
#include "B2.h"
#include "a2.h"
#include "b1.h"

这就是我想要实现的目标

#include "A1.h"
#include "a2.h"
#include "b1.h"
#include "B2.h"

我使用的是 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'

...

谢谢!

这是从 LLVM 12 开始添加的(它将在 Clang 13 中发布?有点混乱);引用 the in-progress release notes of LLVM 12/Clang 13 [强调 我的]:

clang-format

Option SortIncludes has been updated from a bool to an enum with backwards compatibility. In addition to the previous true/false states (now CaseInsensitive/Never), a third state has been added (CaseSensitive) which causes an alphabetical sort with case used as a tie-breaker.

// 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"

most recent Clang Format docs 已使用 CaseSensitive 的示例更新:

IncludeCategories (std::vector<IncludeCategory>)

[...]

Each regular expression can be marked as case sensitive with the field CaseSensitive, per default it is not.

To configure this in the .clang-format file, use:

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