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

clang-format can exceed ColumnLimit when a macro call contains adjacent string 
literals and ends in the GNU comma-elision form `, ##__VA_ARGS__`. The 
formatter currently forbids breaking on either side of `##`, which makes it 
impossible to wrap the final argument in this case.

Allow a break between the comma and `##` in a preprocessor directive. Keep `##` 
with its right operand.

The regression fails on the unmodified Windows LLVM 23.1.6 build. All 397 tests 
in the FormatTest suite pass with the patch applied to that build. The GNU 
named-varargs spelling also wraps and stays stable on a second pass.

Fixes #245620.

Assisted-by: OpenAI Codex. Draft for contributor review.

>From f2ada9fbad04ce28d8207fb38316e977555692ae Mon Sep 17 00:00:00 2001
From: 7se7en72025 <[email protected]>
Date: Fri, 27 Nov 2026 15:38:22 +0530
Subject: [PATCH] [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)");

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to