Skip to content

Conversation

@ilya-biryukov
Copy link
Contributor

Change is originally authored by Daniel Jasper, who asked me to send this PR upstream.

Change is originally authored by Daniel Jasper, who asked me to send
this PR upstream.
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2025

@llvm/pr-subscribers-clang-format

Author: Ilya Biryukov (ilya-biryukov)

Changes

Change is originally authored by Daniel Jasper, who asked me to send this PR upstream.


Full diff: https://github.com/llvm/llvm-project/pull/169037.diff

3 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+4)
  • (modified) clang/unittests/Format/ConfigParseTest.cpp (+1)
  • (modified) clang/unittests/Format/FormatTestMacroExpansion.cpp (+8)
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)
 )",

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 111355 tests passed
  • 4435 tests skipped

@HazardyKnusperkeks
Copy link
Contributor

Can you link to some documentation for these macros? And maybe adapt the commit message. If you want to attribute it to a different person, commit in their name. (git commit --author="Foo bar <foo@bar.com>")

Comment on lines +1964 to +1967
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");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this just be GoogleStyle.Macros.push_back("ASSIGN_OR_RETURN")?

std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));

CHECK_PARSE_LIST(JavaImportGroups);
Style.Macros.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would you need this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants