格式化用于以 clang 格式生成 for 循环的宏

Formatting macros that are used to generate for loops in clang format

提问人:Cherry Toska 提问时间:8/29/2022 更新时间:8/29/2022 访问量:399

问:

我正在为一个不属于我的 c++ 代码库编写一个 clang 格式文件(我提到这一点是因为我不能任意更改以前的设计选择、命名等)。我想更改当前 clang-format 文件创建的格式:

dfor2(k) fineGridVertices[kScalar].setAdjacentRanks(TwoPowerD - kScalar - 1, newId);
enddforx

到,这个:

dfor2(k) 
    fineGridVertices[kScalar].setAdjacentRanks(TwoPowerD - kScalar - 1, newId);
enddforx

dfor2(k) 是一个定义 for 循环的宏,它以以下形式扩展为以下形式:

for (...) {

而 enddforx 只是一个:

}

为此,我尝试了以下方法:

StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
  - dfor2
  - enddforx
MacroBlockBegin: "dfor2\(.+\)|dfor2"
MacroBlockEnd: "enddforx"

我的论点是,使这些宏语句宏将在它们后面强制执行一个新行,然后将它们定义为 MacroBlockBegin 和 MacroBlockEnd 将实现我想要的格式。但它的结果是:

dfor2(k) fineGridVertices[kScalar].setAdjacentRanks(TwoPowerD - kScalar - 1, newId);
enddforx

我还尝试使用一个名为 dfor2(而不是 dfor2(k))的宏来检查我是否写错了正则表达式,但它也将新变体的格式设置为:

dfor2 fineGridVertices[kScalar].setAdjacentRanks(TwoPowerD - kScalar - 1, newId);
enddforx

对于为什么这些更改不会产生我想要的格式,任何帮助都会很好。

我完整的 clang 格式文件在这里:


---
Language:        Cpp
# BasedOnStyle:  LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: true
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: DontAlign
AlignOperands:   Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
  - __capability
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
  AfterCaseLabel:  false
  AfterClass:      false
  AfterControlStatement: Never
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  AfterExternBlock: false
  BeforeCatch:     false
  BeforeElse:      false
  BeforeLambdaBody: false
  BeforeWhile:     false
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: AfterColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     120
CommentPragmas:  '^ IWYU pragma:'
QualifierAlignment: Left
CompactNamespaces: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat:   false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: Never
BasedOnStyle:    ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IfMacros:
  - KJ_IF_MAYBE
IncludeBlocks:   Regroup
IncludeCategories:
  - Regex:           '<[^/]*>' # 0 /
    Priority:        1
    SortPriority:    1
    CaseSensitive:   false
  - Regex:           '<[^/]+/[^/]+>' # 1 /
    Priority:        1
    SortPriority:    2
    CaseSensitive:   false
  - Regex:           '<([^/]+/){2}[^/]+>' # 2 /
    Priority:        1
    SortPriority:    3
    CaseSensitive:   false
  - Regex:           '<([^/]+/){3}[^/]+>' # 3 /
    Priority:        1
    SortPriority:    4
    CaseSensitive:   false
  - Regex:           '<([^/]+/){4}[^/]+>' # 4 /
    Priority:        1
    SortPriority:    5
    CaseSensitive:   false
  - Regex:           '<([^/]+/){5}[^/]+>' # 5 /
    Priority:        1
    SortPriority:    6
    CaseSensitive:   false
  - Regex:           '<([^/]+/){6,}[^/]+>' # 6 and up /
    Priority:        1
    SortPriority:    7
    CaseSensitive:   false
  - Regex:           '"[^/]*"' # 0 / 
    Priority:        2
    SortPriority:    8
    CaseSensitive:   false
  - Regex:           '"[^/]+/[^/]+"' # 1 /
    Priority:        2
    SortPriority:    9
    CaseSensitive:   false
  - Regex:           '"([^/]+/){2}[^/]+"' # 2 /
    Priority:        2
    SortPriority:    10
  - Regex:           '"([^/]+/){3}[^/]+"' # 3 /
    Priority:        2
    SortPriority:    11
  - Regex:           '"([^/]+/){4}[^/]+"' # 4 /
    Priority:        2
    SortPriority:    12
  - Regex:           '"([^/]+/){5}[^/]+"' # 5 /
    Priority:        2
    SortPriority:    13
  - Regex:           '"([^/]+/){6,}[^/]+"' # 6 and up /
    Priority:        2
    SortPriority:    14
IncludeIsMainRegex: '(_test)?$'
#IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires:  false
IndentWidth:     2
IndentWrappedFunctionNames: true
#Needs clang format 15
#IndentRequiresClause: true
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 10000
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 100
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 100
PenaltyBreakTemplateDeclaration: 100
PenaltyExcessCharacter: 1000
PenaltyReturnTypeOnItsOwnLine: 10000
PenaltyIndentedWhitespace: 100
PointerAlignment: Left
PPIndentWidth:   -1
ReferenceAlignment: Pointer
ReflowComments:  true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 0
SortIncludes:    CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
  AfterControlStatements: true
  AfterForeachMacros: true
  AfterFunctionDefinitionName: false
  AfterFunctionDeclarationName: false
  AfterIfMacros:   true
  AfterOverloadedOperator: false
  BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInLineCommentPrefix:
  Minimum:         1
  Maximum:         -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard:        Latest
StatementAttributeLikeMacros:
  - Q_EMIT
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
  - dfor2
  - enddforx
TabWidth:        4
UseCRLF:         false
UseTab:          Never
WhitespaceSensitiveMacros:
  - STRINGIZE
  - PP_STRINGIZE
  - BOOST_PP_STRINGIZE
  - NS_SWIFT_NAME
  - CF_SWIFT_NAME
MacroBlockBegin: "dfor2\(.+\)|dfor2"
MacroBlockEnd: "enddforx"
...

PS:我尝试了ForEachMacros,但没有成功。

c++ clang 格式

评论


答:

0赞 Cherry Toska 8/29/2022 #1

基本上,我使用了错误的文件来格式化,但对于遇到这个问题的人来说:

只有以下选项:

MacroBlockBegin: "dfor2"
MacroBlockEnd: "enddforx"

收益 率:

dfor2(k) 
   fineGridVertices[kScalar].setAdjacentRanks(TwoPowerD - kScalar - 1, newId);
enddforx

结合 2 个选项:

MacroBlockBegin: "dfor2"
MacroBlockEnd: "enddforx"
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
  - dfor2
  - enddforx

收益 率:

dfor2(k) 
fineGridVertices[kScalar].setAdjacentRanks(TwoPowerD - kScalar - 1, newId);
enddforx

对我来说,匹配正则表达式中的参数是没有必要的。