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({"emit", "Q_EMIT"})); + Style.Macros.clear(); + CHECK_PARSE("{Macros: [foo]}", Macros, std::vector({"foo"})); + std::vector 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{"QUNUSED"}); @@ -1106,7 +1120,6 @@ TEST(ConfigParseTest, ParsesConfiguration) { std::vector({"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