https://github.com/jmr updated https://github.com/llvm/llvm-project/pull/200210
>From b52835fa52219fa9de129dda786ea3f29cc6d256 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)). Assisted-by: Gemini --- clang/lib/Format/Format.cpp | 14 +++ clang/unittests/Format/ConfigParseTest.cpp | 15 ++- .../Format/FormatTestMacroExpansion.cpp | 119 +++++++++++++++++- 3 files changed, 143 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..7f420eab68d83 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -1099,6 +1099,20 @@ 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 +1120,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..1232885536b64 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,109 @@ TEST_F(FormatTestMacroExpansion, IndentChildrenWithinMacroCall) { Style); } +// clang-format off +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()); +} +// clang-format on + } // namespace } // namespace test } // namespace format _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
