【问题标题】:clang-format not sorting main fileclang-format 不排序主文件
【发布时间】:2020-09-09 13:28:18
【问题描述】:

我的.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,扩展名为 Clang-Format xaverclang-format 可执行文件的版本为 10.0.0。

有人有什么建议吗?

【问题讨论】:

  • 您是否使用 .hpp 作为头文件?我不认为 ClangFormat 可以检测到除了 .h 之外的任何东西...

标签: c++ visual-studio-code llvm clang-format


【解决方案1】:
  1. 您的.clang-format 文件缺少IncludeIsMainRegex 设置。通常我希望它默认为一个合理的设置,一切都会正常工作。但鉴于您的问题,您应该确保它没有设置在某个地方(也许您使用的 .clang-format 文件与您认为的不同?或者您的 clang-format 可执行文件已被修改?)。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 2021-11-02
    • 2020-06-09
    • 2013-09-11
    • 2021-09-02
    • 1970-01-01
    • 2020-12-13
    • 2020-07-27
    相关资源
    最近更新 更多