[clang] [clang-format] fix incorrectly indents lambda trailing return (PR #94560)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 9 18:27:44 PDT 2024


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/94560

>From 624e74b3066930a5a2bb15d6c0b2ddb4f94d3b79 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 6 Jun 2024 10:44:57 +0800
Subject: [PATCH 1/4] fix incorrectly indents lambda trailing return

---
 clang/lib/Format/ContinuationIndenter.cpp |  5 +++++
 clang/unittests/Format/FormatTest.cpp     | 25 +++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 6b9fbfe0ebf53..630a4ebff9843 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1457,6 +1457,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
       !Current.isOneOf(tok::colon, tok::comment)) {
     return ContinuationIndent;
   }
+  if (Style.isCpp() && Current.is(tok::arrow) &&
+      Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
+                       tok::kw_consteval, tok::kw_static)) {
+    return ContinuationIndent;
+  }
   if (Current.is(TT_ProtoExtensionLSquare))
     return CurrentState.Indent;
   if (Current.isBinaryOperator() && CurrentState.UnindentOperator) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4e427268fb82a..d117efc06c2a7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22858,6 +22858,31 @@ TEST_F(FormatTest, FormatsLambdas) {
       "      //\n"
       "    });");
 
+  verifyFormat("int main() {\n"
+               "  very_long_function_name_yes_it_is_really_long(\n"
+               "      [](auto n)\n"
+               "          -> std::unordered_map<very_long_type_name_A, "
+               "very_long_type_name_B> {\n"
+               "        really_do_something();\n"
+               "      });\n"
+               "}");
+  verifyFormat("int main() {\n"
+               "  very_long_function_name_yes_it_is_really_long(\n"
+               "      [](auto n) noexcept\n"
+               "          -> std::unordered_map<very_long_type_name_A, "
+               "very_long_type_name_B> {\n"
+               "        really_do_something();\n"
+               "      });\n"
+               "}");
+  verifyFormat("int main() {\n"
+               "  very_long_function_name_yes_it_is_really_long(\n"
+               "      [](auto n) constexpr\n"
+               "          -> std::unordered_map<very_long_type_name_A, "
+               "very_long_type_name_B> {\n"
+               "        really_do_something();\n"
+               "      });\n"
+               "}");
+
   FormatStyle DoNotMerge = getLLVMStyle();
   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
   verifyFormat("auto c = []() {\n"

>From ec319435d82f1a815206eeb51a8e6e09c5d811fe Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 7 Jun 2024 09:18:26 +0800
Subject: [PATCH 2/4] address CR issue

---
 clang/lib/Format/ContinuationIndenter.cpp |  2 +-
 clang/unittests/Format/FormatTest.cpp     | 21 ++++++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 630a4ebff9843..9206344490b53 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1457,7 +1457,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
       !Current.isOneOf(tok::colon, tok::comment)) {
     return ContinuationIndent;
   }
-  if (Style.isCpp() && Current.is(tok::arrow) &&
+  if (Style.isCpp() && Current.is(TT_TrailingReturnArrow) &&
       Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
                        tok::kw_consteval, tok::kw_static)) {
     return ContinuationIndent;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d117efc06c2a7..9759d58c718b5 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22861,27 +22861,30 @@ TEST_F(FormatTest, FormatsLambdas) {
   verifyFormat("int main() {\n"
                "  very_long_function_name_yes_it_is_really_long(\n"
                "      [](auto n)\n"
-               "          -> std::unordered_map<very_long_type_name_A, "
-               "very_long_type_name_B> {\n"
+               "          -> std::unordered_map<very_long_type_name_A,\n"
+               "                                very_long_type_name_B> {\n"
                "        really_do_something();\n"
                "      });\n"
-               "}");
+               "}",
+               getLLVMStyleWithColumns(60));
   verifyFormat("int main() {\n"
                "  very_long_function_name_yes_it_is_really_long(\n"
                "      [](auto n) noexcept\n"
-               "          -> std::unordered_map<very_long_type_name_A, "
-               "very_long_type_name_B> {\n"
+               "          -> std::unordered_map<very_long_type_name_A,\n"
+               "                                very_long_type_name_B> {\n"
                "        really_do_something();\n"
                "      });\n"
-               "}");
+               "}",
+               getLLVMStyleWithColumns(60));
   verifyFormat("int main() {\n"
                "  very_long_function_name_yes_it_is_really_long(\n"
                "      [](auto n) constexpr\n"
-               "          -> std::unordered_map<very_long_type_name_A, "
-               "very_long_type_name_B> {\n"
+               "          -> std::unordered_map<very_long_type_name_A,\n"
+               "                                very_long_type_name_B> {\n"
                "        really_do_something();\n"
                "      });\n"
-               "}");
+               "}",
+               getLLVMStyleWithColumns(60));
 
   FormatStyle DoNotMerge = getLLVMStyle();
   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;

>From 26243542cc192eb22c647087573638e79f14b29f Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 8 Jun 2024 15:48:10 +0800
Subject: [PATCH 3/4] address CR comment

---
 clang/lib/Format/ContinuationIndenter.cpp | 2 +-
 clang/unittests/Format/FormatTest.cpp     | 8 +++++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 9206344490b53..7a262563d018f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1457,7 +1457,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
       !Current.isOneOf(tok::colon, tok::comment)) {
     return ContinuationIndent;
   }
-  if (Style.isCpp() && Current.is(TT_TrailingReturnArrow) &&
+  if (Current.is(TT_TrailingReturnArrow) &&
       Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
                        tok::kw_consteval, tok::kw_static)) {
     return ContinuationIndent;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 9759d58c718b5..8af26177db28c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22858,6 +22858,8 @@ TEST_F(FormatTest, FormatsLambdas) {
       "      //\n"
       "    });");
 
+  FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
+
   verifyFormat("int main() {\n"
                "  very_long_function_name_yes_it_is_really_long(\n"
                "      [](auto n)\n"
@@ -22866,7 +22868,7 @@ TEST_F(FormatTest, FormatsLambdas) {
                "        really_do_something();\n"
                "      });\n"
                "}",
-               getLLVMStyleWithColumns(60));
+               LLVMStyle);
   verifyFormat("int main() {\n"
                "  very_long_function_name_yes_it_is_really_long(\n"
                "      [](auto n) noexcept\n"
@@ -22875,7 +22877,7 @@ TEST_F(FormatTest, FormatsLambdas) {
                "        really_do_something();\n"
                "      });\n"
                "}",
-               getLLVMStyleWithColumns(60));
+               LLVMStyle);
   verifyFormat("int main() {\n"
                "  very_long_function_name_yes_it_is_really_long(\n"
                "      [](auto n) constexpr\n"
@@ -22884,7 +22886,7 @@ TEST_F(FormatTest, FormatsLambdas) {
                "        really_do_something();\n"
                "      });\n"
                "}",
-               getLLVMStyleWithColumns(60));
+               LLVMStyle);
 
   FormatStyle DoNotMerge = getLLVMStyle();
   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;

>From 87b0444b6c87f8df1ea64ed9485289353e0505aa Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 10 Jun 2024 09:27:25 +0800
Subject: [PATCH 4/4] address CR comment

---
 clang/lib/Format/ContinuationIndenter.cpp | 10 +++---
 clang/unittests/Format/FormatTest.cpp     | 38 +++++++----------------
 2 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 7a262563d018f..d2a09802a1580 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1257,6 +1257,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
     }
     return CurrentState.Indent;
   }
+  if (Current.is(TT_TrailingReturnArrow) &&
+      Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
+                       tok::kw_consteval, tok::kw_static, TT_AttributeSquare)) {
+    return ContinuationIndent;
+  }
   if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
        (Current.is(tok::greater) && (Style.isProto() || Style.isTableGen()))) &&
       State.Stack.size() > 1) {
@@ -1457,11 +1462,6 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
       !Current.isOneOf(tok::colon, tok::comment)) {
     return ContinuationIndent;
   }
-  if (Current.is(TT_TrailingReturnArrow) &&
-      Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
-                       tok::kw_consteval, tok::kw_static)) {
-    return ContinuationIndent;
-  }
   if (Current.is(TT_ProtoExtensionLSquare))
     return CurrentState.Indent;
   if (Current.isBinaryOperator() && CurrentState.UnindentOperator) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8af26177db28c..9bfb2fb728cd6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22859,33 +22859,19 @@ TEST_F(FormatTest, FormatsLambdas) {
       "    });");
 
   FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
-
-  verifyFormat("int main() {\n"
-               "  very_long_function_name_yes_it_is_really_long(\n"
-               "      [](auto n)\n"
-               "          -> std::unordered_map<very_long_type_name_A,\n"
-               "                                very_long_type_name_B> {\n"
-               "        really_do_something();\n"
-               "      });\n"
-               "}",
-               LLVMStyle);
-  verifyFormat("int main() {\n"
-               "  very_long_function_name_yes_it_is_really_long(\n"
-               "      [](auto n) noexcept\n"
-               "          -> std::unordered_map<very_long_type_name_A,\n"
-               "                                very_long_type_name_B> {\n"
-               "        really_do_something();\n"
-               "      });\n"
-               "}",
+  verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
+               "    [](auto n) noexcept [[back_attr]]\n"
+               "        -> std::unordered_map<very_long_type_name_A,\n"
+               "                              very_long_type_name_B> {\n"
+               "      really_do_something();\n"
+               "    });",
                LLVMStyle);
-  verifyFormat("int main() {\n"
-               "  very_long_function_name_yes_it_is_really_long(\n"
-               "      [](auto n) constexpr\n"
-               "          -> std::unordered_map<very_long_type_name_A,\n"
-               "                                very_long_type_name_B> {\n"
-               "        really_do_something();\n"
-               "      });\n"
-               "}",
+  verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
+               "    [](auto n) constexpr\n"
+               "        -> std::unordered_map<very_long_type_name_A,\n"
+               "                              very_long_type_name_B> {\n"
+               "      really_do_something();\n"
+               "    });",
                LLVMStyle);
 
   FormatStyle DoNotMerge = getLLVMStyle();



More information about the cfe-commits mailing list