需要帮助找出一些clang格式选项

时间:2019-06-30 18:46:16

标签: llvm-clang clang-format

使用clang-format(8.0.0)时,我遇到奇怪的格式化问题,我认为这可能是bug。我不想误以为是错误,却不知道我做错了什么还是错过了配置设置。我有一些示例,说明我觉得自己的设置未得到遵守,而格式化程序在某种意义上正在“流氓”。

这是我的.clang格式设置文件:

---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterClass: false
  AfterControlStatement: true
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterObjCDeclaration: true
  AfterStruct: true
  AfterUnion: true
  AfterExternBlock: true
  BeforeCatch: true
  BeforeElse: true
  IndentBraces: false
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: true
BreakInheritanceList: BeforeComma
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: true
BreakStringLiterals: true
ColumnLimit: 0
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
IncludeBlocks: Preserve
IncludeCategories: 
  - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
    Priority: 2
  - Regex: '^(<|"(gtest|gmock|isl|json)/)'
    Priority: 3
  - Regex: '.*'
    Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...

示例1-具有多个括号的单行分配:

格式化之前:

extern "C"
{
    HBITMAP     (WINAPI *Real_LoadBitmapW)(HINSTANCE, LPCWSTR) = LoadBitmapW;
    HANDLE      (WINAPI *Real_OpenMutexA)(DWORD, BOOL, LPCSTR) = OpenMutexA;
}

格式化后:

    HBITMAP(WINAPI* Real_LoadBitmapW)
    (HINSTANCE, LPCWSTR) = LoadBitmapW;
    HANDLE(WINAPI* Real_OpenMutexA)
    (DWORD, BOOL, LPCSTR)                                                        = OpenMutexA;

预期结果:

    HBITMAP (WINAPI* Real_LoadBitmapW)(HINSTANCE, LPCWSTR)                                                                       = LoadBitmapW;
    HANDLE (WINAPI* Real_OpenMutexA)(DWORD, BOOL, LPCSTR)                                                                        = OpenMutexA;

在这里,格式化程序将它的分配随机地分成新行。此处的ColumnWidth设置似乎不被接受。如果我将ColumnWidth设置为实际数字(例如200),则这些行不会中断,但格式仍然不正确,因为现在在返回类型之后放置了空格。但是使用ColumnWidth会破坏其他格式。我想将ColumnWidth整体设置为0。我不确定是什么规则将其强制为换行并在返回类型后删除空格。


示例2-不遵循C样式的投射空间设置:

格式化之前:

    auto prodVer = (LPVOID)nullptr;
    auto prodVerSize = (UINT)0;

格式化后:

    auto prodVer     = (LPVOID) nullptr;
    auto prodVerSize = (UINT)0;

预期结果:

    auto prodVer     = (LPVOID)nullptr;
    auto prodVerSize = (UINT)0;

在此处,在nullptr之前添加了一个空格。这似乎仅在将nullptr用作强制转换值时发生。如果切换为数字或变量,则可以正常工作。同样,如果将LPVOID切换为void *,它也可以解决此问题。同样,对于此问题,将“标准”设置从Cpp11更改为Cpp03也可以解决此问题,但随后由于标准更改而引入了其他我不希望使用的格式。我的代码使用C ++ 11、14、17、20,因此此处需要使用Cpp11标准设置。


昨晚我才刚开始使用clang格式,所以我对此很陌生,并试图了解规则之间的相互影响。我已经阅读了涵盖每条规则及其作用的文档页面,似乎在某些时候它们像上面一样被忽略了。

我还有其他案件/问题,但我认为它们可能会影响上述两个方面。如果我先解决了这些问题,其他问题可能也已经解决,因此如果仍有更多问题,我可以稍后再提出一个问题。

0 个答案:

没有答案