Skip to content

Commit 26ffc71

Browse files
authored
[clang-format] Disallow breaking before/after ## (#200721)
Fixes #199775
1 parent 11d7939 commit 26ffc71

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6495,6 +6495,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
64956495
!(Right.Next &&
64966496
Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
64976497
}
6498+
if (Left.is(tok::hashhash) || Right.is(tok::hashhash))
6499+
return false;
64986500
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
64996501
TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
65006502
return true;

clang/unittests/Format/FormatTest.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5967,22 +5967,33 @@ TEST_F(FormatTest, HashInMacroDefinition) {
59675967
verifyFormat("#define A(c) uR#c");
59685968
verifyFormat("#define A(c) UR#c");
59695969
verifyFormat("#define A(c) u8R#c");
5970-
verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5970+
5971+
auto Style = getLLVMStyleWithColumns(11);
5972+
verifyFormat("#define A \\\n b #c;", Style);
59715973
verifyFormat("#define A \\\n"
59725974
" { \\\n"
59735975
" f(#c); \\\n"
59745976
" }",
5975-
getLLVMStyleWithColumns(11));
5977+
Style);
59765978

5979+
Style.ColumnLimit = 22;
59775980
verifyFormat("#define A(X) \\\n"
59785981
" void function##X()",
5979-
getLLVMStyleWithColumns(22));
5980-
5982+
Style);
59815983
verifyFormat("#define A(a, b, c) \\\n"
59825984
" void a##b##c()",
5983-
getLLVMStyleWithColumns(22));
5985+
Style);
5986+
verifyFormat("#define A void # ## #", Style);
59845987

5985-
verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5988+
Style.ColumnLimit = 60;
5989+
Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5990+
verifyFormat(
5991+
"#define MACRO(Name) \\\n"
5992+
" struct LongPrefix##Name##LongSuffix< \\\n"
5993+
" VeryLongTemplateArgument> {};",
5994+
"#define MACRO(Name) \\\n"
5995+
" struct LongPrefix##Name##LongSuffix<VeryLongTemplateArgument> {};",
5996+
Style);
59865997

59875998
verifyFormat("{\n"
59885999
" {\n"

0 commit comments

Comments
 (0)