【问题标题】:Unable to disable extern indentation with clang-format无法使用 clang 格式禁用外部缩进
【发布时间】:2021-04-28 04:12:25
【问题描述】:

我不确定这是否是 clang 格式的限制或错误(从源代码构建,clang-format version 12.0.0 (git@github.com:llvm/llvm-project.git d4ce062340064c3f73b8f6136c7350a5abe83cac)),但我无法在 extern "C" { 之后禁用缩进。

我现在的.clang-format

Language: Cpp
Standard: Latest

UseTab: Never
IndentWidth: 4
AccessModifierOffset: -2
SpaceBeforeParens: Never
ColumnLimit: 80
SpacesBeforeTrailingComments: 1
BreakBeforeBraces: Allman

FixNamespaceComments: true
SortIncludes: true
SortUsingDeclarations: true
IncludeBlocks: Regroup

IndentPPDirectives: AfterHash
IndentCaseLabels: true
IndentExternBlock: NoIndent
NamespaceIndentation: All

AlignTrailingComments: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignConsecutiveBitFields: true
AlignConsecutiveMacros: true
AlignAfterOpenBracket: AlwaysBreak

AllowShortFunctionsOnASingleLine: None
AllowShortBlocksOnASingleLine: Never
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortLambdasOnASingleLine: None
AlwaysBreakTemplateDeclarations: Yes

BinPackArguments: false
BinPackParameters: false
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
PenaltyBreakBeforeFirstCallParameter: 500 # basically always break

# Note: must have my custom build (https://github.com/jasperswallen/llvm-project/tree/pointeralignment-right) to properly enable these options
BreakBeforeConceptDeclarations: true
PointerAlignment: Right

这个示例文件是“格式化”的结果:

#ifdef __cplusplus
extern "C"
{
#endif

    void example(void);

#ifdef __cplusplus
}
#endif

但我想要类似的东西:

#ifdef __cplusplus
extern "C"
{
#endif

void example(void);

#ifdef __cplusplus
}
#endif

似乎IndentExternBlock: NoIndent 应该是强制执行此操作的正确选项,但即使使用该设置,它仍会缩进。

BreakBeforeBraces: Allman 是否有可能覆盖此设置?我假设IndentExternBlock 会覆盖大括号,除非设置为AfterExternBlock

【问题讨论】:

    标签: c++ clang-format


    【解决方案1】:

    这是一个bug 37272,但它修复的可能性很小,因为此选项 (IndentExternBlock) 是由 expand preset function 强制执行的。请查看以下problematic commit

    我们可以看到 llvm 开发人员破坏了配置继承,并且没有人关心一年。这意味着我们必须以一种不依赖于 llvm 开发人员的方式来修复配置继承。请查看以下解决方案:

    scripts/clang/config-format/config-source.yml

    Language: Cpp
    BreakBeforeBraces: Allman
    

    scripts/clang/config-format/config-updates.yml

    BraceWrapping:
      AfterExternBlock: false
    BreakBeforeBraces: Custom
    IndentExternBlock: false
    

    scripts/clang/config-format/update.sh

    #!/bin/bash
    set -e
    
    DIR=$(dirname "${BASH_SOURCE[0]}")
    cd "$DIR"
    
    SOURCE="config-source.yml"
    UPDATES="config-updates.yml"
    DESTINATION="../../../.clang-format"
    
    echo "# This file has been auto generated, please don't edit directly." \
      > "$DESTINATION"
    
    SOURCE_STYLE=$(yq -cs ".[0]" < "$SOURCE")
    
    clang-format -style="$SOURCE_STYLE" --dump-config | \
      yq -sy ".[0] * .[1]" - "$UPDATES" >> "$DESTINATION"
    

    请安装yq。运行./scripts/clang/config-format/update.sh 后,您将收到适合您要求的.clang-config

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-20
      • 2017-02-08
      • 2020-04-04
      • 2020-02-02
      • 2021-11-07
      • 1970-01-01
      • 2020-03-02
      • 2019-06-18
      相关资源
      最近更新 更多