[clang] [clang-format] Disallow breaking before/after ## (PR #200721)
via cfe-commits
cfe-commits at lists.llvm.org
Sun May 31 22:41:40 PDT 2026
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/200721
Fixes #199775
>From 9e0ddd426e9f74cbf0b439d9c402208aa5cb717f Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 31 May 2026 22:40:43 -0700
Subject: [PATCH] [clang-format] Disallow breaking before/after ##
Fixes #199775
---
clang/lib/Format/TokenAnnotator.cpp | 2 ++
clang/unittests/Format/FormatTest.cpp | 23 +++++++++++++++++------
2 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 43e4f6796b6dd..cc49be513fa0c 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6498,6 +6498,8 @@ 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))
+ return false;
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 83e2c5b38ceaf..5fd9a7a13f0bb 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5967,22 +5967,33 @@ TEST_F(FormatTest, HashInMacroDefinition) {
verifyFormat("#define A(c) uR#c");
verifyFormat("#define A(c) UR#c");
verifyFormat("#define A(c) u8R#c");
- verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
+
+ auto Style = getLLVMStyleWithColumns(11);
+ verifyFormat("#define A \\\n b #c;", Style);
verifyFormat("#define A \\\n"
" { \\\n"
" f(#c); \\\n"
" }",
- getLLVMStyleWithColumns(11));
+ Style);
+ Style.ColumnLimit = 22;
verifyFormat("#define A(X) \\\n"
" void function##X()",
- getLLVMStyleWithColumns(22));
-
+ Style);
verifyFormat("#define A(a, b, c) \\\n"
" void a##b##c()",
- getLLVMStyleWithColumns(22));
+ Style);
+ verifyFormat("#define A void # ## #", Style);
- verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
+ Style.ColumnLimit = 60;
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
+ verifyFormat(
+ "#define MACRO(Name) \\\n"
+ " struct LongPrefix##Name##LongSuffix< \\\n"
+ " VeryLongTemplateArgument> {};",
+ "#define MACRO(Name) \\\n"
+ " struct LongPrefix##Name##LongSuffix<VeryLongTemplateArgument> {};",
+ Style);
verifyFormat("{\n"
" {\n"
More information about the cfe-commits
mailing list