在operator =之后中断(以clang格式表示

ge4*_*mue 6 c++ llvm operator-keyword clang-format

我在LLVM 7.0.0中使用clang格式,在C++中使用Windows 10.

我有下课

class FooooooooooooooooooC
{
public:
   FooooooooooooooooooC() = default;
   const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) = delete;

};
Run Code Online (Sandbox Code Playgroud)

并且在运行clang-format之后它应该是这样的

class FooooooooooooooooooC
{
public:
   FooooooooooooooooooC() = default;
   const FooooooooooooooooooC& operator=(
      const FooooooooooooooooooC& ) = delete;

};
Run Code Online (Sandbox Code Playgroud)

但实际上它在运行clang-format后看起来像这样

class FooooooooooooooooooC
{
public:
   FooooooooooooooooooC() = default;
   const FooooooooooooooooooC& 
   operator=( const FooooooooooooooooooC& ) = delete;

};
Run Code Online (Sandbox Code Playgroud)

我的.clang格式的clang-fromat设置是

---
AccessModifierOffset: -3
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:   
  AfterClass:            true
  AfterControlStatement: true
  AfterEnum:             true
  AfterFunction:         true
  AfterNamespace:        true
  AfterObjCDeclaration:  true
  AfterStruct:           true
  AfterUnion:            true
  AfterExternBlock:      true
  BeforeCatch:           true
  BeforeElse:            true
  IndentBraces:          true
  SplitEmptyFunction:    true
  SplitEmptyRecord:      true
  SplitEmptyNamespace:   true
BreakAfterJavaFieldAnnotations: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
CommentPragmas:  '^ IWYU pragma:'
ColumnLimit: 80
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 3
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories: 
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 3
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 1000000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: true
Standard: Cpp11
TabWidth: 3
UseTab: Never
...
Run Code Online (Sandbox Code Playgroud)

有人知道如何配置clang-fromat来创建一个中断

运算符=(

谢谢!

Eri*_*kus 1

总而言之,你不能完全做你想做的事。

以下是如何配置 clang-format以不在const FooooooooooooooooooC&.

在决定如何分解行时,clang-format 使用几个名称均以 开头的权重因子Penalty。在这种情况下,您希望返回类型与函数名称保持在同一行,因此您需要调整PenaltyReturnTypeOnItsOwnLine. .clang-format 中的值为60. 相反,使用:

PenaltyReturnTypeOnItsOwnLine: 200
Run Code Online (Sandbox Code Playgroud)

将其设置为任意值110或更大,以防止返回类型后的行被破坏const FooooooooooooooooooC&。我建议200匹配 Chromium、Google 和 Mozilla 的预定义 clang-format 样式。(另外,我不知道为什么110是阈值;惩罚值相当不透明,我只是通过实验发现了该值。)

然而,你最终得到的是这样的:

   const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) =
      delete;
Run Code Online (Sandbox Code Playgroud)

我不相信有任何方法可以强制中断之后operator=(。如果你的类名了 4 个字符,那么你就会得到你所要求的,因为上面的分割之前会delete超过 80 个字符。


  • 上面的评论提到了ColumnLimit。即使您被允许增加列数限制,它也只允许您将operator=声明保留在一行上。它不允许您强制它在 后分割线operator=(

  • 上面的评论提到了AllowAllParametersOfDeclarationOnNextLine: false。正如您所发现的,这并不能解决问题。当有多个参数时,这会影响是否将所有参数放在单独的行上的决定。但你只有一个参数。(请参阅文档。)


最后,需要注意的是,与您的 7.0.0 相比,我使用的是 clang-format 6.0.0。但 clang-format 似乎没有任何差异,这会在这里产生任何差异:

  • ReleaseNotes.html没有提到任何重要的事情
  • 与 6.0.0 相比,7.0.0 中有几个新的 clang-format 样式选项(我注意到这是因为 clang-format 6.0.0 在 .clang-format 文件中抱怨它们),但没有一个与此问题相关。