[clang] [clang-format] Allow line breaking with PointerAlignment configured (PR #164686)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 23 06:04:29 PDT 2025
https://github.com/sstwcw updated https://github.com/llvm/llvm-project/pull/164686
>From 1d9d3965790d48078a0312498e093fa2fc7538f7 Mon Sep 17 00:00:00 2001
From: sstwcw <su3e8a96kzlver at posteo.net>
Date: Wed, 22 Oct 2025 18:57:43 +0000
Subject: [PATCH 1/2] [clang-format] Allow line breaking with PointerAlignment
configured
Fixes #139514.
Within a long declaration involving the pointer, the program has allowed
breaking between the type and the star when the configuration specified
that the star should stick to the variable. Now it also allows breaking
between the star and the variable when the configuration specified that
the star should stick to the type.
---
clang/lib/Format/ContinuationIndenter.cpp | 3 ++-
clang/lib/Format/TokenAnnotator.cpp | 6 +++--
clang/unittests/Format/FormatTest.cpp | 32 +++++++++++++++++++++++
3 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index e5abf833194d4..88e80349ad31c 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1566,7 +1566,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
}
if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
- Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) {
+ Previous.isOneOf(TT_PointerOrReference, tok::coloncolon, tok::equal,
+ TT_JsTypeColon)) {
return ContinuationIndent;
}
if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 25971d2497f97..3ff5587ab2232 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4343,7 +4343,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
return Style.PenaltyReturnTypeOnItsOwnLine;
return 200;
}
- if (Right.is(TT_PointerOrReference))
+ if (Left.is(TT_PointerOrReference) || Right.is(TT_PointerOrReference))
return 190;
if (Right.is(TT_LambdaArrow))
return 110;
@@ -6262,8 +6262,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
return true;
}
+ // It is fine to break the line when the * or & should be separate from the
+ // name according to the PointerAlignment config.
if (Left.is(TT_PointerOrReference))
- return false;
+ return Right.SpacesRequiredBefore;
if (Right.isTrailingComment()) {
// We rely on MustBreakBefore being set correctly here as we should not
// change the "binding" behavior of a comment.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ce68f91bef02a..e9a95be9a5358 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8644,6 +8644,38 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
Style);
+ Style.ColumnLimit = 70;
+ verifyFormat(
+ "void foo( //\n"
+ " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
+ " const my_super_super_super_super_long_variable_name) {}",
+ Style);
+ verifyFormat(
+ "void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
+ " my_super_super_super_super_long_variable_name) {}",
+ Style);
+ verifyFormat(
+ "void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
+ " const my_super_super_super_super_long_variable_name) {}",
+ Style);
+
+ Style.PointerAlignment = FormatStyle::PAS_Middle;
+ verifyFormat(
+ "void foo( //\n"
+ " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
+ " const my_super_super_super_super_long_variable_name) {}",
+ Style);
+ verifyFormat(
+ "void foo(\n"
+ " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
+ " my_super_super_super_super_long_variable_name) {}",
+ Style);
+ verifyFormat(
+ "void foo(\n"
+ " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
+ " const my_super_super_super_super_long_variable_name) {}",
+ Style);
+
Style = getLLVMStyleWithColumns(45);
Style.PenaltyReturnTypeOnItsOwnLine = 400;
verifyFormat("template <bool abool, // a comment\n"
>From 3ccc39e86ac140d07b13394811fcf4c4f030afe7 Mon Sep 17 00:00:00 2001
From: sstwcw <su3e8a96kzlver at posteo.net>
Date: Thu, 23 Oct 2025 13:03:18 +0000
Subject: [PATCH 2/2] Remove unnecessary test
---
clang/unittests/Format/FormatTest.cpp | 10 ----------
1 file changed, 10 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e9a95be9a5358..83b13e805210e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8645,11 +8645,6 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
Style);
Style.ColumnLimit = 70;
- verifyFormat(
- "void foo( //\n"
- " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
- " const my_super_super_super_super_long_variable_name) {}",
- Style);
verifyFormat(
"void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
" my_super_super_super_super_long_variable_name) {}",
@@ -8660,11 +8655,6 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
Style);
Style.PointerAlignment = FormatStyle::PAS_Middle;
- verifyFormat(
- "void foo( //\n"
- " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
- " const my_super_super_super_super_long_variable_name) {}",
- Style);
verifyFormat(
"void foo(\n"
" const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
More information about the cfe-commits
mailing list