https://github.com/jmr created https://github.com/llvm/llvm-project/pull/200210

Add macros that wrap assignment to GoogleStyle.
* ASSIGN_OR_RETURN/ABSL_ASSIGN_OR_RETURN
* RETURN_IF_ERROR/ABSL_RETURN_IF_ERROR
* ASSERT_OK_AND_ASSIGN/ABSL_ASSERT_OK_AND_ASSIGN

`ASSIGN_OR_RETURN(T* p, f())` will be formatted as a multiplication without 
these macros definitions.

This is a resubmission of https://github.com/llvm/llvm-project/pull/169037 with 
fixes for the reported regressions caused by not having RETURN_IF_ERROR Macros 
defined.

https://github.com/llvm/llvm-project/pull/169037#issuecomment-4056423543

Reverts 7e51783 (Revert "[Format] Configure ASSIGN_OR_RETURN macros for Google 
style" (#186445)).

>From d88150f818a8abd89562d496e47d88bfaa2e1173 Mon Sep 17 00:00:00 2001
From: Jesse Rosenstock <[email protected]>
Date: Thu, 28 May 2026 15:09:08 +0200
Subject: [PATCH] [Format] Configure ASSIGN_OR_RETURN macros for Google style

Add macros that wrap assignment to GoogleStyle.
* ASSIGN_OR_RETURN/ABSL_ASSIGN_OR_RETURN
* RETURN_IF_ERROR/ABSL_RETURN_IF_ERROR
* ASSERT_OK_AND_ASSIGN/ABSL_ASSERT_OK_AND_ASSIGN

`ASSIGN_OR_RETURN(T* p, f())` will be formatted as a multiplication
without these macros definitions.

This is a resubmission of https://github.com/llvm/llvm-project/pull/169037
with fixes for the reported regressions caused by not having
RETURN_IF_ERROR Macros defined.

https://github.com/llvm/llvm-project/pull/169037#issuecomment-4056423543

Reverts 7e51783 (Revert "[Format] Configure ASSIGN_OR_RETURN macros for
Google style" (#186445)).
---
 clang/lib/Format/Format.cpp                   |  14 +++
 clang/unittests/Format/ConfigParseTest.cpp    |  14 ++-
 .../Format/FormatTestMacroExpansion.cpp       | 117 +++++++++++++++++-
 3 files changed, 140 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index a29d62c99bb95..fbc514d36b9a2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2101,6 +2101,20 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind 
Language) {
   GoogleStyle.IncludeStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
   GoogleStyle.IndentCaseLabels = true;
   GoogleStyle.KeepEmptyLines.AtStartOfBlock = false;
+
+  GoogleStyle.Macros.push_back("ASSIGN_OR_RETURN(a, b)=a = (b)");
+  GoogleStyle.Macros.push_back(
+      "ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) return c");
+  GoogleStyle.Macros.push_back("RETURN_IF_ERROR(expr)=if (x) return expr");
+  GoogleStyle.Macros.push_back(
+      "ASSERT_OK_AND_ASSIGN(lhs, rexpr)=lhs = (rexpr)");
+  GoogleStyle.Macros.push_back("ABSL_ASSIGN_OR_RETURN(a, b)=a = (b)");
+  GoogleStyle.Macros.push_back(
+      "ABSL_ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) return c");
+  GoogleStyle.Macros.push_back("ABSL_RETURN_IF_ERROR(expr)=if (x) return 
expr");
+  GoogleStyle.Macros.push_back(
+      "ABSL_ASSERT_OK_AND_ASSIGN(lhs, rexpr)=lhs = (rexpr)");
+
   GoogleStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
   GoogleStyle.ObjCSpaceAfterProperty = false;
   GoogleStyle.ObjCSpaceBeforeProtocolList = true;
diff --git a/clang/unittests/Format/ConfigParseTest.cpp 
b/clang/unittests/Format/ConfigParseTest.cpp
index eeaf5d3f66d96..20beed07b5075 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1099,6 +1099,19 @@ TEST(ConfigParseTest, ParsesConfiguration) {
               StatementAttributeLikeMacros,
               std::vector<std::string>({"emit", "Q_EMIT"}));
 
+  Style.Macros.clear();
+  CHECK_PARSE("{Macros: [foo]}", Macros, std::vector<std::string>({"foo"}));
+  std::vector<std::string> GoogleMacros;
+  GoogleMacros.push_back("ASSIGN_OR_RETURN(a, b)=a = (b)");
+  GoogleMacros.push_back("ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) return c");
+  GoogleMacros.push_back("RETURN_IF_ERROR(expr)=if (x) return expr");
+  GoogleMacros.push_back("ASSERT_OK_AND_ASSIGN(lhs, rexpr)=lhs = (rexpr)");
+  GoogleMacros.push_back("ABSL_ASSIGN_OR_RETURN(a, b)=a = (b)");
+  GoogleMacros.push_back("ABSL_ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) 
return c");
+  GoogleMacros.push_back("ABSL_RETURN_IF_ERROR(expr)=if (x) return expr");
+  GoogleMacros.push_back("ABSL_ASSERT_OK_AND_ASSIGN(lhs, rexpr)=lhs = 
(rexpr)");
+  CHECK_PARSE("BasedOnStyle: Google", Macros, GoogleMacros);
+
   Style.StatementMacros.clear();
   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
               std::vector<std::string>{"QUNUSED"});
@@ -1106,7 +1119,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
 
   CHECK_PARSE_LIST(JavaImportGroups);
-  CHECK_PARSE_LIST(Macros);
   CHECK_PARSE_LIST(MacrosSkippedByRemoveParentheses);
   CHECK_PARSE_LIST(NamespaceMacros);
   CHECK_PARSE_LIST(ObjCPropertyAttributeOrder);
diff --git a/clang/unittests/Format/FormatTestMacroExpansion.cpp 
b/clang/unittests/Format/FormatTestMacroExpansion.cpp
index d391fe3d715c3..2708173771bb0 100644
--- a/clang/unittests/Format/FormatTestMacroExpansion.cpp
+++ b/clang/unittests/Format/FormatTestMacroExpansion.cpp
@@ -58,10 +58,18 @@ TEST_F(FormatTestMacroExpansion, UnexpandConfiguredMacros) {
   verifyFormat("ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
                "                 MySomewhatLongFunction(SomethingElse()));",
                Style);
-  verifyFormat("ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
-               "                 MySomewhatLongFunction(SomethingElse()), "
-               "ReturnMe());",
-               Style);
+  verifyFormat(
+      "ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
+      "                 MySomewhatLongFunction(SomethingElse()), RetMe());",
+      Style);
+
+  verifyFormat(
+      "void f() {\n"
+      "  ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n"
+      "                   MySomewhatLongFunction(SomethingElse()));\n"
+      "  ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n"
+      "                   MySomewhatLongFunction(SomethingElse()), RetMe());",
+      getGoogleStyle());
 
   verifyFormat(R"(
 #define MACRO(a, b) ID(a + b)
@@ -301,6 +309,107 @@ TEST_F(FormatTestMacroExpansion, 
IndentChildrenWithinMacroCall) {
                Style);
 }
 
+TEST_F(FormatTestMacroExpansion, NestedMacrosInLambdas) {
+  // These are tests for regressions reported in
+  // https://github.com/llvm/llvm-project/pull/169037#issuecomment-4056423543
+  verifyFormat(
+      "void f() {\n"
+      "  
RETURN_IF_ERROR(Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+      "      a,\n"
+      "      []() {\n"
+      "        if (z()) {\n"
+      "          ASSIGN_OR_RETURN(w, Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww());\n"
+      "        }\n"
+      "      }));\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void g() {\n"
+      "  RETURN_IF_ERROR(q(\n"
+      "      w,\n"
+      "      []() {\n"
+      "        if (z()) {\n"
+      "          ASSIGN_OR_RETURN(w, Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww());\n"
+      "        }\n"
+      "      }));\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void f() {\n"
+      "  ABSL_RETURN_IF_ERROR(\n"
+      "      Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+      "          a,\n"
+      "          []() {\n"
+      "            if (z()) {\n"
+      "              ABSL_ASSIGN_OR_RETURN(w, 
Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww());\n"
+      "            }\n"
+      "          }));\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void g() {\n"
+      "  ABSL_RETURN_IF_ERROR(q(\n"
+      "      w,\n"
+      "      []() {\n"
+      "        if (z()) {\n"
+      "          ABSL_ASSIGN_OR_RETURN(w, 
Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww());\n"
+      "        }\n"
+      "      }));\n"
+      "}",
+      getGoogleStyle());
+}
+
+TEST_F(FormatTestMacroExpansion, PredefinedGoogleMacros) {
+  verifyFormat(
+      "void f() {\n"
+      "  ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n"
+      "                   MySomewhatLongFunction(SomethingElse()));\n"
+      "  ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n"
+      "                   MySomewhatLongFunction(SomethingElse()), RetMe());\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void f() {\n"
+      "  ABSL_ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n"
+      "                        MySomewhatLongFunction(SomethingElse()));\n"
+      "  ABSL_ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n"
+      "                        MySomewhatLongFunction(SomethingElse()), 
RetMe());\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void f() {\n"
+      "  RETURN_IF_ERROR(\n"
+      "      MySomewhatLongFunction(SomethingElse(WithManyArguments, 
AndSomeMore)));\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void f() {\n"
+      "  ABSL_RETURN_IF_ERROR(\n"
+      "      MySomewhatLongFunction(SomethingElse(WithManyArguments, 
AndSomeMore)));\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void f() {\n"
+      "  ASSERT_OK_AND_ASSIGN(MySomewhatLongType* variable,\n"
+      "                       MySomewhatLongFunction(SomethingElse()));\n"
+      "}",
+      getGoogleStyle());
+
+  verifyFormat(
+      "void f() {\n"
+      "  ABSL_ASSERT_OK_AND_ASSIGN(MySomewhatLongType* variable,\n"
+      "                            MySomewhatLongFunction(SomethingElse()));\n"
+      "}",
+      getGoogleStyle());
+}
+
 } // namespace
 } // namespace test
 } // namespace format

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

Reply via email to