c++ - 无法使用 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
.
解决方案
这是一个错误 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
您的要求。
推荐阅读
- java - 在理解 JButton 的 getInsets() 方法返回的值方面需要帮助
- image - 如何上传图像并插入数据库到codeigniter 3
- c - gdb 附加后继续
- php - 不上传文件时违反完整性约束 (skipOnEmpty)
- javascript - 使用 wordpress API 和纯 JavaScript 更新 wordpress 数据库
- regex - 在scala中搜索和替换字符串中的特殊字符
- node.js - 如何在mongodb中进行不区分大小写的查询
- html - 我无法让我的 CSS 代码显示我在浏览器中所做的更改
- javascript - Twig 字符串变量包含由 VueJS 编译的 Javascript 代码
- haskell - 如何理解 `MonadUnliftIO` 的“无状态单子”要求?