llvm / llvm/llvm-project

[clang-format] `AlignAfterOpenBracket: BlockIndent` fails if bracket starts before `ContinuationIndentWidth`

Open
#161,654 3 comments 2 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This is most easily seen on functions with `AlwaysBreakAfterReturnType` enabled or on constructors with short class names.

Given this config:
```yaml
AlignAfterOpenBracket: BlockIndent
AlwaysBreakAfterReturnType: TopLevel
```

This formats as expected:
```cpp
void
LogE(
std::string_view message,
std::source_location loc = std::source_location::current()
) {
std::println(std::cerr, "{}({}): {}", loc.file_name(), loc.line(), message);
}
```

With one less character in the function name, it formats like this:
```cpp
void
Log(std::string_view message,
std::source_location loc = std::source_location::current()) {
std::println(std::cerr, "{}({}): {}", loc.file_name(), loc.line(), message);
}
```

It seemingly falls back to using `AlignAfterOpenBracket: DontAlign` if the opening bracket starts before the value of `ContinuationIndentWidth` (`4` by default).

*Tested with version `18.1.3 (Ubuntu)` and `22.0.0git (commit 3c39187)`*

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the clang-format behavior with the YAML configuration and C++ examples in the issue, comparing the function names that differ by one character. Trace the formatting path for AlignAfterOpenBracket: BlockIndent and ContinuationIndentWidth, then verify that the corrected output preserves block indentation for both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.