llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Arteiimis
<details>
<summary>Changes</summary>
## Summary
For a line break directly after an opening bracket, `clang-format` indents the
contents one level relative to the enclosing indentation, and the closing
bracket uses that same indentation as its base (`getNewLineColumn()`'s
`BreakBeforeCloseBracket*` handling). In block indentation mode -- i.e. when
`AlignAfterOpenBracket` is `true` and `BreakAfterOpenBracketFunction` is set,
which is what `AlignAfterOpenBracket: BlockIndent` maps to -- a nested call
that is part of a member access chain instead derived the contents indentation
from `StartOfFunctionCall`, which can hold a column-aligned position. The
contents were then indented to that column while the closing bracket stayed at
the block indent, so a single expression followed two different rules:
```cpp
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch() // aligned
)
// block indent
```
The change ignores `StartOfFunctionCall` in `moveStatePastScopeOpener()` when
the style breaks after the opening bracket and that bracket is a round
parenthesis:
```diff
- NewIndent = IndentationAndAlignment(std::max(
- CurrentState.LastSpace, CurrentState.StartOfFunctionCall))
+
- Style.ContinuationIndentWidth;
+ unsigned EnclosingIndent = CurrentState.LastSpace;
+ if (!(Style.AlignAfterOpenBracket &&
Style.BreakAfterOpenBracketFunction &&
+ Current.is(tok::l_paren)))
+ EnclosingIndent =
+ std::max(EnclosingIndent, CurrentState.StartOfFunctionCall);
+ NewIndent =
+ IndentationAndAlignment(EnclosingIndent) +
Style.ContinuationIndentWidth;
```
## Evidence
Input from #<!-- -->220327 (`ColumnLimit: 90`, `IndentWidth: 2`,
`ContinuationIndentWidth: 2`, `AlignAfterOpenBracket: BlockIndent`).
Before:
```cpp
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()
)
.count());
(void)nowMs;
}
```
After (the argument is indented one level relative to the enclosing
indentation, matching the closing bracket):
```cpp
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()
)
.count());
(void)nowMs;
}
```
Not specific to casts: the same input without a cast is fixed the same way.
## Scope
- Only block indentation mode is affected: `Align`, `DontAlign` and `Google`
styles produce byte-identical output (checked by formatting the 47
`clang/lib/Format/*.cpp` and `*.h` files with each style before and after).
- The output of block indentation changes as intended: the arguments of a call
that is broken right after its `(` are now indented at the block indent instead
of at a column-aligned position.
- The trailing member access in the example (`.count()`) is still
column-aligned. That indentation goes through `getNewLineColumn()`'s
`ContinuationIndent` and the member access handling, i.e. a different code
path, and is tracked in #<!-- -->242373 (marked `FIXME` in the new test). It is
not a regression of this change: `Align` and `BlockIndent` both format it that
way today, and 19.1.7 behaves the same.
## Merge danger
Two-way door: a formatting-only change, no API or config change, revertible by
reverting the commit. Blast radius: the argument indentation of calls broken
directly after the opening bracket, limited to block indentation styles.
## Tests
- `clang/unittests/Format/FormatTests`: 1317/1317 pass, including the new
`AlignBracketsTest.AlignAfterOpenBracketBlockIndentNestedCall`.
- New test covers the reported cast case, the same break inside a plain call,
and `ContinuationIndentWidth: 0`.
- `clang/test/Format` (lit): 33/33 pass, including `docs_updated.test` (the
generated style documentation is still in sync). This build configuration has
no `clang` binary, so lit's `%clang` substitution resolves to a stub; no test
in that directory invokes `%clang`.
- No documentation change: no option is added or changed.
Fixes #<!-- -->220327
This pull request description was drafted with AI assistance and has been
reviewed and edited by me.
---
Full diff: https://github.com/llvm/llvm-project/pull/242376.diff
2 Files Affected:
- (modified) clang/lib/Format/ContinuationIndenter.cpp (+12-2)
- (modified) clang/unittests/Format/AlignBracketsTest.cpp (+70)
``````````diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index 384bad6492315..bf167a785a902 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -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) +
Style.ContinuationIndentWidth;
if (Style.isTableGen() && Current.is(TT_TableGenDAGArgOpenerToBreak) &&
diff --git a/clang/unittests/Format/AlignBracketsTest.cpp
b/clang/unittests/Format/AlignBracketsTest.cpp
index e7e57bf839c0e..fa470e99181f6 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -366,6 +366,76 @@ TEST_F(AlignBracketsTest,
AlignAfterOpenBracketBlockIndent) {
Style);
}
+TEST_F(AlignBracketsTest, AlignAfterOpenBracketBlockIndentNestedCall) {
+ auto Style = getLLVMStyle();
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
+ Style.ColumnLimit = 90;
+ Style.ContinuationIndentWidth = 2;
+
+ // A break directly after an opening bracket indents the contents one level
+ // relative to the enclosing indentation, so that they line up with the
+ // closing bracket. The contents used to be indented to a column-aligned
+ // position instead, once the nested call was part of a member access chain.
+ // https://github.com/llvm/llvm-project/issues/220327
+ // FIXME: the trailing `.count()` still lines up with the opening parenthesis
+ // of the cast, see https://github.com/llvm/llvm-project/issues/242373.
+ verifyFormat("void f() {\n"
+ " auto const nowMs =\n"
+ "
static_cast<std::uint64_t>(std::chrono::duration_cast<std::"
+ "chrono::milliseconds>(\n"
+ " std::chrono::system_clock::now().time_since_epoch()\n"
+ " )\n"
+ " .count());\n"
+ " (void)nowMs;\n"
+ "}",
+ "void f() {\n"
+ " auto const nowMs = static_cast<std::uint64_t>(std::chrono::"
+ "duration_cast<std::chrono::milliseconds>(std::chrono::"
+ "system_clock::now().time_since_epoch()).count());\n"
+ " (void)nowMs;\n"
+ "}",
+ Style);
+
+ // The same break inside a plain call, without a cast.
+ Style.ColumnLimit = 80;
+ verifyFormat("void f() {\n"
+ " auto const nowMs = std::chrono::duration_cast<std::chrono::"
+ "milliseconds>(\n"
+ " std::chrono::system_clock::now().time_since_epoch()\n"
+ " )\n"
+ " .count();\n"
+ " (void)nowMs;\n"
+ "}",
+ "void f() {\n"
+ " auto const nowMs = std::chrono::duration_cast<std::chrono::"
+ "milliseconds>(std::chrono::system_clock::now()."
+ "time_since_epoch()).count();\n"
+ " (void)nowMs;\n"
+ "}",
+ Style);
+
+ // A zero continuation indent must not produce a negative indent.
+ Style.ColumnLimit = 90;
+ Style.ContinuationIndentWidth = 0;
+ verifyFormat("void f() {\n"
+ " auto const nowMs =\n"
+ " static_cast<std::uint64_t>(std::chrono::duration_cast<std::"
+ "chrono::milliseconds>(\n"
+ " std::chrono::system_clock::now().time_since_epoch()\n"
+ " )\n"
+ " .count());\n"
+ " (void)nowMs;\n"
+ "}",
+ "void f() {\n"
+ " auto const nowMs = static_cast<std::uint64_t>(std::chrono::"
+ "duration_cast<std::chrono::milliseconds>(std::chrono::"
+ "system_clock::now().time_since_epoch()).count());\n"
+ " (void)nowMs;\n"
+ "}",
+ Style);
+}
+
TEST_F(AlignBracketsTest, AlignAfterOpenBracketBlockIndentIfStatement) {
auto Style = getLLVMStyle();
``````````
</details>
https://github.com/llvm/llvm-project/pull/242376
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits