[clang-format] Disallow breaking before/after ###200721
Merged
Merged
Conversation
|
@llvm/pr-subscribers-clang-format Author: owenca (owenca) ChangesFixes #199775 2 Files Affected:
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"
|
mydeveloperday
approved these changes
Jun 5, 2026
HazardyKnusperkeks
approved these changes
Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #199775