[clang-format] Allow indenting wrapped braces for functions, records, and other brace categories
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
clang-format currently cannot express a style where a wrapped opening brace is indented by one level and the contents of the block are indented by another level, consistently for functions, records, and control statements.
For example, I would like to format code like this:
```cpp
int foo( int x )
{
if ( x > 0 )
{
++x;
}
return x;
}
struct S
{
int value;
bool enabled;
};
```
The important part is the indentation hierarchy:
```cpp
function / control statement
{
block contents
}
```
Currently, the existing styles only partially support this.
With BreakBeforeBraces: GNU, control statements can be formatted as desired:
```cpp
if ( x > 0 )
{
++x;
}
```
but functions and records are formatted without the extra brace indentation:
```cpp
int foo( int x )
{
return x;
}
struct S
{
int value;
};
```
Whitesmiths can indent the braces themselves, but aligns the contents with the braces:
```cpp
int foo( int x )
{
return x;
}
```
so it cannot produce the desired form either.
Using BreakBeforeBraces: Custom together with:
BraceWrapping:
AfterFunction: true
AfterClass: true
AfterStruct: true
AfterControlStatement: Always
IndentBraces: true
also does not provide independent control over the indentation of the wrapped brace and the block contents for all brace categories.
It would be useful if clang-format allowed brace indentation to be controlled independently for categories such as:
functions
classes / structs / unions
namespaces
enums
control statements
lambdas
For example, conceptually something like:
BraceWrapping:
IndentBraces:
AfterFunction: true
AfterClass: true
AfterStruct: true
AfterControlStatement: true
This is only an example of possible configuration syntax; I am not proposing that this exact API is required.
The important capability is to make this formatting possible:
```cpp
void function()
{
statement;
while ( condition )
{
statement;
}
}
class C
{
int member;
};
```
while preserving the current behavior as the default for existing configurations.
This appears related to the work/discussion around:
#143248 — more control of wrapped lambda brace indentation
#143249 — corresponding clang-format changes for lambda brace indentation
#143663 — proposal to make IndentBraces more extensible
The lambda indentation work seems especially close conceptually, because the desired result similarly requires the brace to receive an additional indentation level while the block contents remain one level deeper than the brace.
Would extending this mechanism to functions, records, namespaces, and other brace categories be considered acceptable?
Contributor guide
Research direction
Start by reviewing the existing BreakBeforeBraces and BraceWrapping/IndentBraces behavior described in the issue, then read the related discussions in issues #143248, #143249, and #143663. The work is done when the requested brace hierarchy can be configured across the listed brace categories while existing configurations retain their current behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100