CLANG 格式不对主文件进行排序

clang-format not sorting main file

本文关键字:排序 主文件 格式 CLANG      更新时间:2023-10-16

我对我的.clang-format文件有以下定义:

---
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
BreakInheritanceList: AfterColon
BreakStringLiterals: true
ColumnLimit: 0
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
DerivePointerAlignment: false
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex:           '^<'
Priority:        -1
- Regex:           '^"(gtest|gmock)/'
Priority:        1
- Regex:           '^"boost/'
Priority:        2
# Other rules ...
- Regex:           '^"tools/'
Priority:        14
- Regex:           '^"[w]+/'
Priority:        15
- Regex:           '.*'
Priority:        16
IndentCaseLabels: true
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
Language: Cpp
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...

头文件的排序效果很好,除了文件到主包含映射。主文件按规则 16 排序,因此始终出现在末尾。

我尝试删除包含文件的排序(即注释掉参数IncludeBlocksIncludeCategories,但它仍然不起作用。我认为由于回退样式是LLVM,因此它至少在这种情况下可以工作。

如果我不使用我的文件,而是在命令行中指定-style=llvm,则主文件的排序有效。

我在 Debian 10 上使用 VS Code 1.45.1,扩展名为xaver的 Clang-Format。可执行文件clang-format版本为 10.0.0。

有人有什么建议吗?

  1. 您的.clang-format文件缺少IncludeIsMainRegex设置。通常我希望它默认为合理的设置,并且一切都会正常工作。但是考虑到你的问题,你应该确保它没有设置在某个地方(也许你正在使用与你想象的不同.clang-format文件?或者你的clang-format可执行文件以某种方式被修改了?(。

    默认设置为IncludeIsMainRegex: '(Test)?$'。其他合理的值可能是'([-_](test|unittest))?$'(与预定义的Google和Chromium样式使用的值相匹配(,'$'(表示没有额外的文件名后缀(或''(表示任何文件名后缀(。有关详细信息,请参阅文档。

  2. xaverClang-Format扩展名运行clang-format可执行文件时,它是否在命令行上提供文件名?或者,也许,它将文件流式传输到stdin?如果是后者,那么clang-format将不知道文件名,因此无法检测到哪个包含文件是"主"包含文件。可以处理的一种方法是使用-assume-filename命令行选项(在此处记录(,但当然这取决于扩展。

  3. 最后,请记住,"主"包含文件按优先级排序0。您的IncludeCategories设置包含所有以 开头的包含文件<按优先级-1排序,因此它们将在"主"包含文件之前排序。