运算符=后中断(采用clang格式



我在C++中使用LLVM 7.0.0和Windows 10中的clang格式。

我有以下类别的

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

运行clang格式后,它应该看起来像这个

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

但实际上,在运行clang格式后,它看起来是这样的

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

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
...

有人知道如何配置clang fromat以在之后创建中断吗

运算符=(

谢谢!

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

以下是如何将clang格式配置为而不是const FooooooooooooooooooC&之后放置中断。

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

PenaltyReturnTypeOnItsOwnLine: 200

将其设为110或更大的任意值,以防止在const FooooooooooooooooooC&返回类型之后线路断开。我建议200与Chromium、Google和Mozilla的预定义clang格式样式相匹配。(此外,我不知道为什么110是阈值;惩罚值相当不透明,我只是通过实验才发现这个值。(

然而,你最终得到的是:

const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) =
delete;

我不认为有任何方法可以在operator=(之后强行突破。如果你的类名比长4个字符,那么你会得到你想要的,因为delete之前的上述拆分将超过80个字符。


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

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


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

  • ReleaseNotes.html没有提到任何重要的内容
  • 与6.0.0相比,7.0.0中有几个新的clang格式样式选项(我注意到了这一点,因为clang格式6.0.0在.crang格式文件中抱怨了这些选项(,但没有一个与此问题有关

最新更新