https://github.com/ilya-biryukov updated https://github.com/llvm/llvm-project/pull/169037
>From b0303c1ad87fdf729288b46450211f6af6f5036d Mon Sep 17 00:00:00 2001 From: Daniel Jasper <[email protected]> Date: Fri, 21 Nov 2025 14:15:05 +0100 Subject: [PATCH 1/2] [Format] Configure ASSIGN_OR_RETURN macros for Google style These macros are used by many of the Google projects, e.g.: - https://chromium.googlesource.com/chromium/src/+/133.0.6943.141/base/types/expected_macros.h#104 - https://github.com/protocolbuffers/protobuf/blob/1477683618b83e07bb8ec1d19b718e0d4d5c8357/src/google/protobuf/stubs/status_macros.h#L62 --- clang/lib/Format/Format.cpp | 4 ++++ clang/unittests/Format/ConfigParseTest.cpp | 1 + clang/unittests/Format/FormatTestMacroExpansion.cpp | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 9bbb33cb14502..20ea81cf429cc 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1961,6 +1961,10 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; + 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"); + if (Language == FormatStyle::LK_Java) { GoogleStyle.AlignAfterOpenBracket = false; GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index d578fa7a1a1e8..1e1621d8422e9 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -967,6 +967,7 @@ TEST(ConfigParseTest, ParsesConfiguration) { std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); CHECK_PARSE_LIST(JavaImportGroups); + Style.Macros.clear(); CHECK_PARSE_LIST(Macros); CHECK_PARSE_LIST(MacrosSkippedByRemoveParentheses); CHECK_PARSE_LIST(NamespaceMacros); diff --git a/clang/unittests/Format/FormatTestMacroExpansion.cpp b/clang/unittests/Format/FormatTestMacroExpansion.cpp index d391fe3d715c3..0b15f21be0ddf 100644 --- a/clang/unittests/Format/FormatTestMacroExpansion.cpp +++ b/clang/unittests/Format/FormatTestMacroExpansion.cpp @@ -63,6 +63,14 @@ TEST_F(FormatTestMacroExpansion, UnexpandConfiguredMacros) { "ReturnMe());", Style); + verifyFormat("void f() {\n" + " ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n" + " MySomewhatLongFunction(SomethingElse()));\n" + " ASSIGN_OR_RETURN(MySomewhatLongType* variable,\n" + " MySomewhatLongFunction(SomethingElse()), " + "ReturnMe());", + getGoogleStyle()); + verifyFormat(R"( #define MACRO(a, b) ID(a + b) )", >From 41004ab97b4c709fb4eea7a2d9baa97cdc9619a7 Mon Sep 17 00:00:00 2001 From: Ilya Biryukov <[email protected]> Date: Mon, 9 Feb 2026 10:48:35 +0100 Subject: [PATCH 2/2] fixup! [Format] Configure ASSIGN_OR_RETURN macros for Google style address comments: update tests, keep conifguration parameters sorted --- clang/lib/Format/Format.cpp | 9 ++++---- clang/unittests/Format/ConfigParseTest.cpp | 17 ++++++++++++-- .../Format/FormatTestMacroExpansion.cpp | 22 +++++++++---------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 20ea81cf429cc..903c3d740b624 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1906,6 +1906,11 @@ 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.ObjCBinPackProtocolList = FormatStyle::BPS_Never; GoogleStyle.ObjCSpaceAfterProperty = false; GoogleStyle.ObjCSpaceBeforeProtocolList = true; @@ -1961,10 +1966,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; - 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"); - if (Language == FormatStyle::LK_Java) { GoogleStyle.AlignAfterOpenBracket = false; GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 1e1621d8422e9..ae019215ffedf 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -9,6 +9,7 @@ #include "clang/Format/Format.h" #include "llvm/Support/VirtualFileSystem.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace clang { @@ -144,6 +145,12 @@ TEST(ConfigParseTest, GetsCorrectBasedOnStyle) { EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" +#define CHECK_PARSE_THAT(TEXT, FIELD, MATCHER) \ + EXPECT_THAT(Style.FIELD, ::testing::Not(MATCHER)) \ + << "Initial value already matches!"; \ + EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ + EXPECT_THAT(Style.FIELD, (MATCHER)) << "Does not match after parsing!"; + #define CHECK_PARSE_INT(FIELD) CHECK_PARSE(#FIELD ": -1234", FIELD, -1234) #define CHECK_PARSE_UNSIGNED(FIELD) CHECK_PARSE(#FIELD ": 1234", FIELD, 1234u) @@ -960,6 +967,14 @@ TEST(ConfigParseTest, ParsesConfiguration) { StatementAttributeLikeMacros, std::vector<std::string>({"emit", "Q_EMIT"})); + Style.Macros.clear(); + CHECK_PARSE("{ BasedOnStyle: LLVM, Macros: [foo]}", Macros, + std::vector<std::string>({"foo"})); + CHECK_PARSE_THAT( + "BasedOnStyle: Google", Macros, + testing::ElementsAre(testing::StartsWith("ASSIGN_OR_RETURN(a, b)="), + testing::StartsWith("ASSIGN_OR_RETURN(a, b, c)="))); + Style.StatementMacros.clear(); CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, std::vector<std::string>{"QUNUSED"}); @@ -967,8 +982,6 @@ TEST(ConfigParseTest, ParsesConfiguration) { std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); CHECK_PARSE_LIST(JavaImportGroups); - Style.Macros.clear(); - 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 0b15f21be0ddf..c00607f0b7115 100644 --- a/clang/unittests/Format/FormatTestMacroExpansion.cpp +++ b/clang/unittests/Format/FormatTestMacroExpansion.cpp @@ -58,18 +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()), " - "ReturnMe());", - getGoogleStyle()); + 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) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
