[clang] [clang-format] Fix nested call indentation in block indent mode (PR #242376)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 21 03:21:06 PST 2026
================
@@ -2183,8 +2183,18 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
if (Current.ParameterCount > 1)
NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
} else {
- NewIndent = IndentationAndAlignment(std::max(
- CurrentState.LastSpace, CurrentState.StartOfFunctionCall)) +
+ // In block indentation mode, a break directly after the opening bracket
+ // indents the contents one level relative to the enclosing indentation, so
+ // that they line up with the closing bracket (see the
+ // BreakBeforeCloseBracket* handling in getNewLineColumn()).
+ // StartOfFunctionCall can be a column-aligned position, which would indent
+ // the contents past that level.
+ unsigned EnclosingIndent = CurrentState.LastSpace;
+ if (!(Style.AlignAfterOpenBracket && Style.BreakAfterOpenBracketFunction &&
+ Current.is(tok::l_paren)))
+ EnclosingIndent =
+ std::max(EnclosingIndent, CurrentState.StartOfFunctionCall);
+ NewIndent = IndentationAndAlignment(EnclosingIndent) +
----------------
Arteiimis wrote:
Yes, dropping it is the fix. `StartOfFunctionCall` holds an absolute column produced by the align-style logic, one of its two writers is guarded by `Style.AlignAfterOpenBracket`, which `BlockIndent` also satisfies since that option was split into booleans. For the input in #220327 only two lines are at stake:
```cpp
// Before: the contents of `duration_cast<...>(` are indented to
// max(CurrentState.LastSpace, CurrentState.StartOfFunctionCall)
// + Style.ContinuationIndentWidth = max(4, 31) + 2 = 33
// while the matching `)` uses State.Stack[size - 2].LastSpace = 4.
void f() {
auto const nowMs =
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch() // 33
) // 4
.count());
(void)nowMs;
}
// After: the contents start one continuation level inside the base of
// `)`, i.e. 4 + 2 = 6.
void f() {
auto const nowMs =
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch() // 6
) // 4
.count());
(void)nowMs;
}
```
so the contents and the closing bracket followed two different bases, which is the reported output. `LastSpace` tracks the line that is actually being laid out, while `StartOfFunctionCall` keeps a stale column, so under a break after the open bracket the latter has to be ignored.
https://github.com/llvm/llvm-project/pull/242376
More information about the cfe-commits
mailing list