llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Raiyyan <3 (7se7en72025)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) clang/lib/Format/TokenAnnotator.cpp (+5-1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+13) 


``````````diff
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)");

``````````

</details>


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

Reply via email to