[clang] [clang-format] Allow wrapping GNU variadic macro arguments (PR #246602)

Raiyyan via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 22:32:50 PST 2026


https://github.com/7se7en72025 updated https://github.com/llvm/llvm-project/pull/246602

>From f2ada9fbad04ce28d8207fb38316e977555692ae Mon Sep 17 00:00:00 2001
From: 7se7en72025 <f20241312 at pilani.bits-pilani.ac.in>
Date: Fri, 27 Nov 2026 15:38:22 +0530
Subject: [PATCH 1/2] [clang-format] Allow wrapping GNU variadic macro
 arguments

Assisted-by: OpenAI Codex
---
 clang/lib/Format/TokenAnnotator.cpp   |  6 +++++-
 clang/unittests/Format/FormatTest.cpp | 13 +++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3241ad59aca231..2a3132f229582b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6551,8 +6551,12 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
             !(Right.Next &&
               Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
   }
-  if (Left.is(tok::hashhash) || Right.is(tok::hashhash))
+  if (Left.is(tok::hashhash))
     return false;
+  if (Right.is(tok::hashhash)) {
+    // GNU variadic macros can use `, ##__VA_ARGS__` to elide the comma.
+    return Line.InPPDirective && Left.is(tok::comma);
+  }
   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
                     TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
     return true;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index be4936e9a8d95e..7d97b551e52433 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6184,6 +6184,19 @@ TEST_F(FormatTest, HashInMacroDefinition) {
                "}");
 }
 
+TEST_F(FormatTest, WrapsGNUVariadicMacroArguments) {
+  auto Style = getLLVMStyleWithColumns(40);
+  Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
+  verifyFormat("#define F(...) \\\n"
+               "  f(long_argument_name, \\\n"
+               "    \"first string\" \\\n"
+               "    \"second string\", \\\n"
+               "    ##__VA_ARGS__)",
+               "#define F(...) f(long_argument_name, "
+               "\"first string\" \"second string\", ##__VA_ARGS__)",
+               Style);
+}
+
 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
   verifyFormat("#define A (x)");
   verifyFormat("#define A(x)");

>From b5b6ffd9093caae73d8f77c4386e3813c0206ffc Mon Sep 17 00:00:00 2001
From: 7se7en72025 <f20241312 at pilani.bits-pilani.ac.in>
Date: Sun, 6 Dec 2026 12:02:38 +0530
Subject: [PATCH 2/2] [clang-format] normal wrapping to be used before
 ##__VA_ARGS__

now comma-breaking rules only decide to whether wrap before ##__VA_ARGS__ or not.
Also added a reg test for short variadic macro call.
---
 clang/lib/Format/TokenAnnotator.cpp   |  6 ++----
 clang/unittests/Format/FormatTest.cpp | 10 +++++++++-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 2a3132f229582b..5cc85fc2fd47c5 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6551,11 +6551,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
             !(Right.Next &&
               Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
   }
-  if (Left.is(tok::hashhash))
+  if (Left.is(tok::hashhash) ||
+      (Right.is(tok::hashhash) && Left.isNot(tok::comma))) {
     return false;
-  if (Right.is(tok::hashhash)) {
-    // GNU variadic macros can use `, ##__VA_ARGS__` to elide the comma.
-    return Line.InPPDirective && Left.is(tok::comma);
   }
   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
                     TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 65f2c537b2fa16..8cf36f7464968b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6188,8 +6188,16 @@ TEST_F(FormatTest, HashInMacroDefinition) {
                Style);
   verifyFormat("#define A void # ## #", Style);
 
-  Style.ColumnLimit = 60;
+  Style.ColumnLimit = 42;
   Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
+  verifyFormat("#define M(f, ...) \\\n"
+               "  auto f = call(firstArgument, \\\n"
+               "                ##__VA_ARGS__);",
+               "#define M(f, ...) \\\n"
+               "  auto f = call(firstArgument, ##__VA_ARGS__);",
+               Style);
+
+  Style.ColumnLimit = 60;
   verifyFormat(
       "#define MACRO(Name) \\\n"
       "  struct LongPrefix##Name##LongSuffix< \\\n"



More information about the cfe-commits mailing list