CLANG 格式展开空初始值设定项

  • 本文关键字:格式 CLANG clang-format
  • 更新时间 :
  • 英文 :


我有一个简单的结构变量声明: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
...

AllowShortBlocksOnASingleLine选项不会影响结构声明,它会影响 while 语句(请参阅文档(。也许你想要的是"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影响结构声明。

相关内容

  • 没有找到相关文章

最新更新