llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang-format Allow for short statement braces in requires clause

Open marstaik opened this issue 3 years ago • 6 comments

With custom brace styles to always break, there is no way to get one-line requires expressions to exist. In particular I mean the ones that constrain return type via { ... } -> ...;, and I am not sure if they have a formal name.

There probably should be another option for AllowShortRequiresExpressionsOnASingleLine or something similar.

IE:

template<class T>
concept meta_named_type_ = requires( T t ) {
	meta_type_<T>;
	is_base_of_type<EMetaTag::NamedType>( t.meta_tag );

	{
		t.name
	} -> string_;
	{
		t.nested_name_specifier
	} -> string_;
	{
		t.metadata
	} -> map_;
};

should preferably be:

template<class T>
concept meta_named_type_ = requires( T t ) {
	meta_type_<T>;
	is_base_of_type<EMetaTag::NamedType>( t.meta_tag );

	{ t.name } -> string_;
	{ t.nested_name_specifier } -> string_;
	{ t.metadata } -> map_;
};

.clang-format 16 config:

StatementMacros: []

Language: Cpp
Standard: c++20
ColumnLimit: 120
IndentWidth: 4
TabWidth: 4
AccessModifierOffset: -4
UseTab: AlignWithSpaces
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4

PointerAlignment: Left
AlignAfterOpenBracket: AlwaysBreak
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AlignTrailingComments: false
PackConstructorInitializers: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortBlocksOnASingleLine: Empty
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AllowAllArgumentsOnNextLine: false
#AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false

BinPackArguments: false
BinPackParameters: false

BitFieldColonSpacing: Both

BreakBeforeBraces: Custom
BraceWrapping:
    AfterCaseLabel: false
    AfterClass: true
    AfterControlStatement: Never
    AfterEnum: true
    AfterFunction: true
    AfterNamespace: true
    AfterStruct: true
    AfterUnion: true
    AfterExternBlock: true
    BeforeCatch: false
    BeforeElse: true
    BeforeLambdaBody: true
    BeforeWhile: false
    IndentBraces: false
    SplitEmptyFunction: true
    SplitEmptyRecord: true
    SplitEmptyNamespace: true

BreakBeforeBinaryOperators: NonAssignment
BreakBeforeConceptDeclarations: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: false
CompactNamespaces: false
Cpp11BracedListStyle: true
FixNamespaceComments: true

IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: AfterExternBlock
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequires: false
IndentWrappedFunctionNames: false

SortUsingDeclarations: true

SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 3
SpacesInAngles: Never
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: true
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: false

MaxEmptyLinesToKeep: 1

SeparateDefinitionBlocks: Always

RequiresClausePosition: OwnLine
IndentRequiresClause: false
RequiresExpressionIndentation: OuterScope

marstaik avatar Dec 08 '22 23:12 marstaik

@llvm/issue-subscribers-clang-format

llvmbot avatar Dec 08 '22 23:12 llvmbot

After comment AfterFunction: true I got output below

template<class T>
concept meta_named_type_ = requires( T t ) {
        meta_type_<T>;
        is_base_of_type<EMetaTag::NamedType>( t.meta_tag );

        { t.name } -> string_;
        { t.nested_name_specifier } -> string_;
        { t.metadata } -> map_;
};

It seems like require expression is recognized as function?

Backl1ght avatar Dec 09 '22 15:12 Backl1ght

It seems like brace wrapping did not take require into consideration.

I will try implementing it.

Backl1ght avatar Dec 09 '22 16:12 Backl1ght

This looks to be fixed at trunk

template <class T>
concept meta_named_type_ = requires(T t) {
  meta_type_<T>;
  is_base_of_type<EMetaTag::NamedType>(t.meta_tag);

  { t.name } -> string_;
  { t.nested_name_specifier } -> string_;
  { t.metadata } -> map_;
};

usx95 avatar Mar 29 '23 11:03 usx95

This is still an issue if using the option BraceWrapping.AfterFunction (that option shouldn't affect requires clauses)

rymiel avatar Mar 29 '23 17:03 rymiel

https://reviews.llvm.org/D139834

rymiel avatar May 15 '23 21:05 rymiel