[clang] [clang-format] fix aligning inheritance lists and binary operator operands with UT_AlignWithSpaces (PR #189218)
Eugene Shalygin via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 30 00:58:17 PDT 2026
https://github.com/zeule updated https://github.com/llvm/llvm-project/pull/189218
>From 66a9d17cc94a4847a9d9b04363c139d0b7878a5b Mon Sep 17 00:00:00 2001
From: Eugene Shalygin <e.shalygin at abberior-instruments.com>
Date: Fri, 27 Mar 2026 17:02:04 +0100
Subject: [PATCH 1/2] [clang-format] fix aligning inheritance lists with
UT_AlignWithSpaces
---
clang/lib/Format/ContinuationIndenter.cpp | 7 +++++++
clang/unittests/Format/FormatTest.cpp | 14 ++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index a388b74920e0b..a3e3ffedb0e32 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1237,6 +1237,13 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
Style.ContinuationIndentWidth;
}
}
+ if ((Style.BreakInheritanceList == FormatStyle::BILS_AfterComma ||
+ Style.BreakInheritanceList == FormatStyle::BILS_BeforeColon) &&
+ Previous.is(TT_InheritanceComma) &&
+ Current.isOneOf(tok::kw_private, tok::kw_protected, tok::kw_public,
+ tok::identifier)) {
+ CurrentState.IsAligned = true;
+ }
if ((PreviousNonComment &&
PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e0b2644249e76..76e9b8f6d7805 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8435,6 +8435,20 @@ TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
" : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
Style);
+ FormatStyle Tabbed = getLLVMStyleWithColumns(42);
+ Tabbed.IndentWidth = 4;
+ Tabbed.TabWidth = 4;
+ Tabbed.UseTab = FormatStyle::UT_AlignWithSpaces;
+ verifyFormat("class Foo : public aaaaaaaaa,\n"
+ " protected bbbbbbbbb,\n"
+ " private cccccccccc,\n"
+ " dddddddddd {};",
+ Tabbed);
+ verifyFormat("struct S {\n"
+ "\tclass Foo : public aaaaaaaaa,\n"
+ "\t private bbbbbbbbb {};\n"
+ "};",
+ Tabbed);
}
TEST_F(FormatTest, BreakConstructorInitializersAfterComma) {
>From b1a94bc48e98bd8b37e8528c51e0466442d0ed45 Mon Sep 17 00:00:00 2001
From: Eugene Shalygin <eugene.shalygin at gmail.com>
Date: Sun, 29 Mar 2026 22:12:46 +0200
Subject: [PATCH 2/2] [clang-format] fix aligning binary operator operands
Mark operators of binary operators as aligned.
---
clang/lib/Format/ContinuationIndenter.cpp | 4 +++-
clang/unittests/Format/FormatTest.cpp | 21 +++++++++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index a3e3ffedb0e32..e62e817beac31 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1261,8 +1261,10 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
(PreviousNonComment && PreviousNonComment->is(tok::question))) {
CurrentState.BreakBeforeParameter = true;
}
- if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
+ if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore) {
CurrentState.BreakBeforeParameter = false;
+ CurrentState.IsAligned = true;
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 76e9b8f6d7805..7d46ae2c445ae 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17251,6 +17251,27 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
Tab);
}
+TEST_F(FormatTest, AlignWithSpaces) {
+ FormatStyle Tabbed = getLLVMStyleWithColumns(42);
+ Tabbed.UseTab = FormatStyle::UT_AlignWithSpaces;
+
+ Tabbed.IndentWidth = 4;
+ Tabbed.TabWidth = 4;
+ verifyFormat("aStreamObject << aaaaaaaaaaaaa\n"
+ " << bbbbbbbbb;\n",
+ Tabbed);
+ verifyFormat("result = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " bbbbbbbbbbbbbbbbbbbb;",
+ Tabbed);
+ verifyFormat("class C {\n"
+ "\tvoid foo() {\n"
+ "\t\taStreamObject << aaaaaaaaaaaaa\n"
+ "\t\t << bbbbbbbbb;\n"
+ "\t}\n"
+ "};",
+ Tabbed);
+}
+
TEST_F(FormatTest, ZeroTabWidth) {
FormatStyle Tab = getLLVMStyleWithColumns(42);
Tab.IndentWidth = 8;
More information about the cfe-commits
mailing list