首页 > 解决方案 > 无法使用 clang 格式禁用外部缩进

问题描述

我不确定这是否是 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

解决方案


这是一个错误 37272,但它修复的可能性很小,因为此选项 ( IndentExternBlock) 是由扩展预设功能强制执行的。请查看以下有问题的提交

我们可以看到 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您的要求。


推荐阅读