Skip to content

Commit

Permalink
[clang-format] Add option for aligning requires clause body
Browse files Browse the repository at this point in the history
Adds an option whether requires clause body should be aligned with
the `requires` keyword.
This option is now the default, both without configuration and in LLVM
style.

Fixes #56283

Differential Revision: https://reviews.llvm.org/D129443

Co-authored-by: Emilia Dreamer <emilia@rymiel.space>
  • Loading branch information
eoan-ermine and rymiel committed Oct 21, 2022
1 parent 1809414 commit 9c422ab
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 21 deletions.
29 changes: 29 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Expand Up @@ -3861,6 +3861,35 @@ the configuration (without a prefix: ``Auto``).



**RequiresExpressionIndentation** (``RequiresExpressionIndentationKind``) :versionbadge:`clang-format 16`
The indentation used for requires expression bodies.

Possible values:

* ``REI_OuterScope`` (in configuration: ``OuterScope``)
Align requires expression body relative to the indentation level of the
outer scope the requires expression resides in.
This is the default.

.. code-block:: c++

template <typename T>
concept C = requires(T t) {
...
}

* ``REI_Keyword`` (in configuration: ``Keyword``)
Align requires expression body relative to the `requires` keyword.

.. code-block:: c++

template <typename T>
concept C = requires(T t) {
...
}



**SeparateDefinitionBlocks** (``SeparateDefinitionStyle``) :versionbadge:`clang-format 14`
Specifies the use of empty lines to separate definition blocks, including
classes, structs, enums, and functions.
Expand Down
5 changes: 4 additions & 1 deletion clang/docs/ReleaseNotes.rst
Expand Up @@ -629,7 +629,10 @@ AST Matchers

clang-format
------------
- Add `RemoveSemicolon` option for removing `;` after a non-empty function definition.
- Add ``RemoveSemicolon`` option for removing ``;`` after a non-empty function definition.
- Add ``RequiresExpressionIndentation`` option for configuring the alignment of requires-expressions.
The default value of this option is ``OuterScope``, which differs in behavior from clang-format 15.
To match the default behavior of clang-format 15, use the ``Keyword`` value.

clang-extdef-mapping
--------------------
Expand Down
27 changes: 27 additions & 0 deletions clang/include/clang/Format/Format.h
Expand Up @@ -3153,6 +3153,32 @@ struct FormatStyle {
/// \version 15
RequiresClausePositionStyle RequiresClausePosition;

/// Indentation logic for requires expression bodies.
enum RequiresExpressionIndentationKind : int8_t {
/// Align requires expression body relative to the indentation level of the
/// outer scope the requires expression resides in.
/// This is the default.
/// \code
/// template <typename T>
/// concept C = requires(T t) {
/// ...
/// }
/// \endcode
REI_OuterScope,
/// Align requires expression body relative to the `requires` keyword.
/// \code
/// template <typename T>
/// concept C = requires(T t) {
/// ...
/// }
/// \endcode
REI_Keyword,
};

/// The indentation used for requires expression bodies.
/// \version 16
RequiresExpressionIndentationKind RequiresExpressionIndentation;

/// \brief The style if definition blocks should be separated.
enum SeparateDefinitionStyle : int8_t {
/// Leave definition blocks as they are.
Expand Down Expand Up @@ -3988,6 +4014,7 @@ struct FormatStyle {
RemoveBracesLLVM == R.RemoveBracesLLVM &&
RemoveSemicolon == R.RemoveSemicolon &&
RequiresClausePosition == R.RequiresClausePosition &&
RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
ShortNamespaceLines == R.ShortNamespaceLines &&
SortIncludes == R.SortIncludes &&
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -1406,8 +1406,10 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
CurrentState.NestedBlockIndent = State.Column + Current.ColumnWidth + 1;
if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow))
CurrentState.LastSpace = State.Column;
if (Current.is(TT_RequiresExpression))
if (Current.is(TT_RequiresExpression) &&
Style.RequiresExpressionIndentation == FormatStyle::REI_Keyword) {
CurrentState.NestedBlockIndent = State.Column;
}

// Insert scopes created by fake parenthesis.
const FormatToken *Previous = Current.getPreviousNonComment();
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Format/Format.cpp
Expand Up @@ -99,6 +99,15 @@ struct ScalarEnumerationTraits<FormatStyle::LambdaBodyIndentationKind> {
}
};

template <>
struct ScalarEnumerationTraits<FormatStyle::RequiresExpressionIndentationKind> {
static void
enumeration(IO &IO, FormatStyle::RequiresExpressionIndentationKind &Value) {
IO.enumCase(Value, "Keyword", FormatStyle::REI_Keyword);
IO.enumCase(Value, "OuterScope", FormatStyle::REI_OuterScope);
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
IO.enumCase(Value, "Never", FormatStyle::UT_Never);
Expand Down Expand Up @@ -854,6 +863,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("RemoveBracesLLVM", Style.RemoveBracesLLVM);
IO.mapOptional("RemoveSemicolon", Style.RemoveSemicolon);
IO.mapOptional("RequiresClausePosition", Style.RequiresClausePosition);
IO.mapOptional("RequiresExpressionIndentation",
Style.RequiresExpressionIndentation);
IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
IO.mapOptional("SortIncludes", Style.SortIncludes);
Expand Down Expand Up @@ -1290,6 +1301,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
LLVMStyle.RequiresClausePosition = FormatStyle::RCPS_OwnLine;
LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
LLVMStyle.ShortNamespaceLines = 1;
LLVMStyle.SpacesBeforeTrailingComments = 1;
Expand Down

0 comments on commit 9c422ab

Please sign in to comment.