https://github.com/Arteiimis updated 
https://github.com/llvm/llvm-project/pull/242376

>From b07a2500e56808f50f6a81b41d32720c3abc2d1f Mon Sep 17 00:00:00 2001
From: Arteiimis <[email protected]>
Date: Sat, 14 Nov 2026 00:02:45 +0800
Subject: [PATCH] [clang-format] Fix nested call indentation in block indent
 mode

A line break directly after an opening bracket is supposed to indent the
contents one level relative to the enclosing indentation, so that they line
up with the closing bracket, which uses that same indentation as its base.
For a nested call that is part of a member access chain the indentation was
computed from StartOfFunctionCall instead, which can hold a column-aligned
position, so the contents ended up past the block indent while the closing
bracket stayed at it:

    auto const nowMs =
      
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
                                   
std::chrono::system_clock::now().time_since_epoch()
      )
                                   .count());

Ignore StartOfFunctionCall for a round parenthesis when the style breaks
after the opening bracket, i.e. with AlignAfterOpenBracket AlwaysBreak and
BlockIndent, so that the contents keep the block indent:

    auto const nowMs =
      
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch()
      )
                                   .count());

The trailing member access is still column-aligned; that indentation is
tracked separately in https://github.com/llvm/llvm-project/issues/242373.

Fixes #220327

Assisted-by: DeepSeek
---
 clang/lib/Format/ContinuationIndenter.cpp    | 13 +++++++++++--
 clang/unittests/Format/AlignBracketsTest.cpp | 16 ++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 384bad64923150..cf964473d6ab1c 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -2183,8 +2183,17 @@ 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)) 
+
+    // With a break after the open bracket, the contents are indented relative
+    // to the enclosing indentation, so the column in StartOfFunctionCall does
+    // not apply to them.
+    const bool BreakAfterOpenParen = Style.AlignAfterOpenBracket &&
+                                     Style.BreakAfterOpenBracketFunction &&
+                                     Current.is(tok::l_paren);
+    unsigned EnclosingIndent = CurrentState.LastSpace;
+    if (!BreakAfterOpenParen)
+      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 e7e57bf839c0e1..eb9acda462e440 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -259,6 +259,22 @@ TEST_F(AlignBracketsTest, 
AlignAfterOpenBracketBlockIndent) {
   Style.BreakAfterOpenBracketFunction = true;
   Style.BreakBeforeCloseBracketFunction = true;
 
+  Style.ColumnLimit = 50;
+  verifyFormat("void f() {\n"
+               "  auto x = functionCall(\n"
+               "      argument1.memberFunction(argument2)\n"
+               "  )\n"
+               "               .count();\n"
+               "  (void)x;\n"
+               "}",
+               "void f() {\n"
+               "  auto x = functionCall(argument1.memberFunction(argument2))\n"
+               "      .count();\n"
+               "  (void)x;\n"
+               "}",
+               Style);
+  Style.ColumnLimit = 80;
+
   verifyFormat(Short, Style);
   verifyFormat(
       "functionCall(\n"

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to