英文:
Clang format: prevent break before indexing
问题
以下是您提供的文本的中文翻译:
我遇到了以下格式问题(代码编辑为将宏更改为大写,还添加了混合大小写版本)-
```cpp
for (i = 0; i < 3; ++i)
{
MACRO_ARRAY_CALL(param)[i] = MACRO_ARRAY_CALL(param2)[i];
Macro_array_call(param)[i] = Macro_array_call(param2)[i];
}
其中 macro_array_call
访问了 param(这是一个结构体对象)的长度为 3 的数组。由于某种原因,我的 clang 格式文件将其重新格式化为
for (i = 0; i < 3; ++i)
{
MACRO_ARRAY_CALL(param)
[i] = MACRO_ARRAY_CALL(param2)[i];
Macro_array_call(param)[i] = Macro_array_call(param2)[i];
}
我没有在 clang 格式中看到任何指示在此之前发生换行的内容。有趣的是,只有当宏完全大写时才会发生这种情况。到目前为止,我还没有在其他地方看到类似的行为。如果相同的宏是大小写混合或全部小写,就不会发生这种情况。
这是我的 clang 格式文件。是否有什么我需要修改以获得正确行为的地方?
---
Language: Cpp
# BasedOnStyle: Microsoft
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
...
(省略其他设置)
...
ColumnLimit: 0
...
编辑:为 for 循环添加大括号
请注意,我已经将文本翻译成中文,但没有包括代码部分。如果您需要代码部分的翻译,请告诉我。
<details>
<summary>英文:</summary>
I'm having a formatting issue with the following (code edited for macro to be upper case, added a mixed case version as well)-
for (i = 0; i < 3; ++i)
{
MACRO_ARRAY_CALL(param)[i] = MACRO_ARRAY_CALL(param2)[i];
Macro_array_call(param)[i] = Macro_array_call(param2)[i];
}
where `macro_array_call` accesses an array of length 3 belong to param (which is a struct object). For some reason, my clang format file reformats this to
for (i = 0; i < 3; ++i)
{
MACRO_ARRAY_CALL(param)
[i] = MACRO_ARRAY_CALL(param2)[i];
Macro_array_call(param)[i] = Macro_array_call(param2)[i];
}
I am not seeing anything in clang format that indicates a break would happen prior to something like this. Interestingly, this happens only when the macro is fully capitalized. I'm not seeing a similar behaviour elsewhere so far. If the same macro is a combination of lower and uppercase or only lowercase, this does not happen.
This is my clang format file. Is there something I need to modify to get the correct behaviour?
Language: Cpp
BasedOnStyle: Microsoft
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories: - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0 - Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0 - Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: AfterHash
IndentExternBlock: AfterExternBlock
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 100000
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 0
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 1000
DerivePointerAlignment: false
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros: - Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros: - STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
AlwaysBreakAfterReturnType: AllDefinitions
ColumnLimit: 0
...
Edit: braces for the for loop
</details>
# 答案1
**得分**: 0
找到了一些有用的信息。这里的问题似乎是一个全大写的宏被无明显原因地分成了多行。
在这里添加这个TypenameMacro(从clang format 9可用),问题就不再存在。
<details>
<summary>英文:</summary>
Found something that helps. The problem here seems to be that a fully capitalized macro is being broken into multiple lines for no apparent reason.
Add this as a TypenameMacro (available from clang format 9) and the problem no longer presents.
</details>
# 答案2
**得分**: 0
我已找到一个解决方案:```WhitespaceSensitiveMacros: - MACRO_ARRAY_CALL```。我不确定它是否修复了已经格式化的代码,但至少不会破坏手动格式化。如果我找到更好的解决方案,我会更新我的答案。
<details>
<summary>英文:</summary>
After a few experiments, I have found a solution:
WhitespaceSensitiveMacros:
- MACRO_ARRAY_CALL
I'm not sure if it fixes the already formatted code, but it does not corrupt the manual formatting at least. I will update my answer if I find a better solution.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论