clang-format 展开空初始化程序

Posted

技术标签:

【中文标题】clang-format 展开空初始化程序【英文标题】:clang-format unrolls empty initializer 【发布时间】:2020-05-22 19:31:10 【问题描述】:

我有一个简单的结构变量声明:struct sigaction act;

clang-format(10.0 版)将其展开为:

struct sigaction act
    
    ;

我确实设置了AllowShortBlocksOnASingleLine: Empty,但仍然缺少一些东西。 还可以设置什么来避免这种转变? .clang-format 文件如下:

---
Language:        Cpp
BasedOnStyle:  Google
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Left
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true # false
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel:  true
AfterClass:      true # false
AfterControlStatement: MultiLine # false
AfterEnum:       false
AfterFunction:   true # false
AfterNamespace:  true # false
AfterObjCDeclaration: true # false
AfterStruct:     true # false
AfterUnion:      true # false
AfterExternBlock: true # false
BeforeCatch:     true # false
BeforeElse:      true # false
IndentBraces:    true # false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman # Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     120
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth:   4  #2
IndentWrappedFunctionNames: false
javascriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Middle # Right
ReflowComments:  true
SortIncludes:    true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...

【问题讨论】:

【参考方案1】:

AllowShortBlocksOnASingleLine 选项不会影响结构声明,它会影响 while 语句(参见 documentation)。也许您想要的是“AllowShortStructsOnASingleLine”之类的东西,但它不存在。

问题的根源是BreakBeforeBraces: Allman。将其更改为 Attach 将解决您的问题。但在大多数情况下,也许你喜欢Allman。然后,您可以改为使用Custom,如下所示:

BraceWrapping:
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     false # <-- You need this
  AfterUnion:      false
  AfterExternBlock: true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: false # <-- And this
  SplitEmptyNamespace: true
BreakBeforeBraces: Custom # <-- And this

BraceWrapping 的其余设置可以保留BreakBeforeBreaces: Allman 的设置方式;只有AfterStructSplitEmptyRecord 会影响您的结构声明。

【讨论】:

我已经尝试过你的建议 @Eric,但是一旦我将 BreakBeforeBraces 设置为 Custom,格式化的现在会将 if 语句设置为看起来像 BreakBeforeBraces: AttachBreakBeforeBraces: Linux . 尝试设置上面列出的所有BraceWrapping 设置。如果您不更改它们,它们可能会默认为 LLVM 设置,这将匹配 Attach。但是我上面列出的应该与Allman 匹配,除了两个更改为在一行上获取结构声明。 工作,谢谢。必须添加 AfterCaseLabel 才能完成。

以上是关于clang-format 展开空初始化程序的主要内容,如果未能解决你的问题,请参考以下文章

clang 格式,数组初始化器

如何以 clang 格式包装结构初始化程序?

Clang 格式:逗号前的空格?

如何使用 clang 格式控制数组初始值设定项的缩进?

用inno setup打包安装工具生成的EXE程序,在安装的时出现【试图在初始化之前展开“app”常量】怎么解决?

Swift:致命错误:在初始化 UIlabel 值时展开可选值时意外发现 nil