[clang] clang-format: ensure ternary operands are aligned (PR #196697)
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
HazardyKnusperkeks wrote:
What happens if you ignore the previous value of `AlignedTo`?
https://github.com/llvm/llvm-project/pull/196697
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
zeule wrote: Thanks! https://github.com/llvm/llvm-project/pull/196697 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/HazardyKnusperkeks closed https://github.com/llvm/llvm-project/pull/196697 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
zeule wrote:
I simply want to avoid repeatedly setting it. If it is set at this point, it
was set by this very code fragment during the previous invocation of
`addTokensOnNewLine()`. I think there is no harm to leave the old value,
because the token stream isn't changing.
https://github.com/llvm/llvm-project/pull/196697
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
HazardyKnusperkeks wrote:
The assignment of `AlignedTo` is always unconditional in this function, I think
we should keep it that way for consistency.
https://github.com/llvm/llvm-project/pull/196697
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/HazardyKnusperkeks auto_merge_enabled https://github.com/llvm/llvm-project/pull/196697 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/HazardyKnusperkeks approved this pull request. https://github.com/llvm/llvm-project/pull/196697 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/zeule edited https://github.com/llvm/llvm-project/pull/196697 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
zeule wrote:
That certainly makes reading simpler. Updated the code.
https://github.com/llvm/llvm-project/pull/196697
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/zeule updated
https://github.com/llvm/llvm-project/pull/196697
>From 04b0990d623ea5785b83e9ea836104560b7325ea Mon Sep 17 00:00:00 2001
From: Eugene Shalygin
Date: Sat, 9 May 2026 09:06:41 +0200
Subject: [PATCH] clang-format: ensure ternary operands are aligned
Set ParentState::AlignedTo for ternary operands.
---
clang/lib/Format/ContinuationIndenter.cpp | 16
clang/unittests/Format/AlignmentTest.cpp | 10 ++
2 files changed, 26 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index b2f799bb33b01..485fe382bda3a 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1276,6 +1276,22 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (Style.AlignOperands != FormatStyle::OAS_DontAlign &&
+ Current.is(TT_ConditionalExpr)) {
+switch (Style.AlignOperands) {
+case FormatStyle::OAS_Align:
+ CurrentState.AlignedTo = Current.is(tok::question)
+ ? Current.getPrevious(tok::equal)
+ : Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_AlignAfterOperator:
+ if (Current.is(tok::colon))
+CurrentState.AlignedTo = Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_DontAlign:
+ break;
+}
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/AlignmentTest.cpp
b/clang/unittests/Format/AlignmentTest.cpp
index 971ceeefef582..9421a4c933b9e 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -3599,6 +3599,16 @@ TEST_F(AlignmentTest, ContinuedAligned) {
"\t},\n"
"\tvariant);",
Style);
+
+ Style.ColumnLimit = 40;
+ Style.IndentWidth = Style.TabWidth = Style.ContinuationIndentWidth = 8;
+
+ verifyFormat("void f() {\n"
+ "\tint =\n"
+ "\t\t01 ? 2\n"
+ "\t\t : 3;\n"
+ "}",
+ Style);
}
} // namespace
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang-format
Author: Eugene Shalygin (zeule)
Changes
Set ParentState::AlignedTo for ternary operands.
---
Full diff: https://github.com/llvm/llvm-project/pull/196697.diff
2 Files Affected:
- (modified) clang/lib/Format/ContinuationIndenter.cpp (+15)
- (modified) clang/unittests/Format/AlignmentTest.cpp (+10)
``diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index b2f799bb33b01..7b3bf98670c1b 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
+switch (Style.AlignOperands) {
+case FormatStyle::OAS_DontAlign:
+ break;
+case FormatStyle::OAS_Align:
+CurrentState.AlignedTo = Current.is(tok::question)
+ ? Current.getPrevious(tok::equal)
+ : Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_AlignAfterOperator:
+ if (Current.is(tok::colon))
+CurrentState.AlignedTo = Current.getPrevious(tok::question);
+ break;
+}
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/AlignmentTest.cpp
b/clang/unittests/Format/AlignmentTest.cpp
index 971ceeefef582..9421a4c933b9e 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -3599,6 +3599,16 @@ TEST_F(AlignmentTest, ContinuedAligned) {
"\t},\n"
"\tvariant);",
Style);
+
+ Style.ColumnLimit = 40;
+ Style.IndentWidth = Style.TabWidth = Style.ContinuationIndentWidth = 8;
+
+ verifyFormat("void f() {\n"
+ "\tint =\n"
+ "\t\t01 ? 2\n"
+ "\t\t : 3;\n"
+ "}",
+ Style);
}
} // namespace
``
https://github.com/llvm/llvm-project/pull/196697
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/zeule updated
https://github.com/llvm/llvm-project/pull/196697
>From bae26699d60e6abb50ce4c97357dab3056920ece Mon Sep 17 00:00:00 2001
From: Eugene Shalygin
Date: Sat, 9 May 2026 09:06:41 +0200
Subject: [PATCH] clang-format: ensure ternary operands are aligned
Set ParentState::AlignedTo for ternary operands.
---
clang/lib/Format/ContinuationIndenter.cpp | 15 +++
clang/unittests/Format/AlignmentTest.cpp | 10 ++
2 files changed, 25 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index b2f799bb33b01..5cd771cacc193 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
+switch (Style.AlignOperands) {
+case FormatStyle::OAS_DontAlign:
+ break;
+case FormatStyle::OAS_Align:
+ CurrentState.AlignedTo = Current.is(tok::question)
+ ? Current.getPrevious(tok::equal)
+ : Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_AlignAfterOperator:
+ if (Current.is(tok::colon))
+CurrentState.AlignedTo = Current.getPrevious(tok::question);
+ break;
+}
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/AlignmentTest.cpp
b/clang/unittests/Format/AlignmentTest.cpp
index 971ceeefef582..9421a4c933b9e 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -3599,6 +3599,16 @@ TEST_F(AlignmentTest, ContinuedAligned) {
"\t},\n"
"\tvariant);",
Style);
+
+ Style.ColumnLimit = 40;
+ Style.IndentWidth = Style.TabWidth = Style.ContinuationIndentWidth = 8;
+
+ verifyFormat("void f() {\n"
+ "\tint =\n"
+ "\t\t01 ? 2\n"
+ "\t\t : 3;\n"
+ "}",
+ Style);
}
} // namespace
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff origin/main HEAD --extensions cpp -- clang/lib/Format/ContinuationIndenter.cpp clang/unittests/Format/AlignmentTest.cpp --diff_from_common_commit `` :warning: The reproduction instructions above might return results for more than one PR in a stack if you are using a stacked PR workflow. You can limit the results by changing `origin/main` to the base branch/commit you want to compare against. :warning: View the diff from clang-format here. ``diff diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 7b3bf9867..5cd771cac 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1281,9 +1281,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, case FormatStyle::OAS_DontAlign: break; case FormatStyle::OAS_Align: -CurrentState.AlignedTo = Current.is(tok::question) - ? Current.getPrevious(tok::equal) - : Current.getPrevious(tok::question); + CurrentState.AlignedTo = Current.is(tok::question) + ? Current.getPrevious(tok::equal) + : Current.getPrevious(tok::question); break; case FormatStyle::OAS_AlignAfterOperator: if (Current.is(tok::colon)) `` https://github.com/llvm/llvm-project/pull/196697 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/zeule created
https://github.com/llvm/llvm-project/pull/196697
Set ParentState::AlignedTo for ternary operands.
>From ea2ca73b6d8488359e14403856f38a58163bd1f4 Mon Sep 17 00:00:00 2001
From: Eugene Shalygin
Date: Sat, 9 May 2026 09:06:41 +0200
Subject: [PATCH] clang-format: ensure ternary operands are aligned
Set ParentState::AlignedTo for ternary operands.
---
clang/lib/Format/ContinuationIndenter.cpp | 15 +++
clang/unittests/Format/AlignmentTest.cpp | 10 ++
2 files changed, 25 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index b2f799bb33b01..7b3bf98670c1b 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
+switch (Style.AlignOperands) {
+case FormatStyle::OAS_DontAlign:
+ break;
+case FormatStyle::OAS_Align:
+CurrentState.AlignedTo = Current.is(tok::question)
+ ? Current.getPrevious(tok::equal)
+ : Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_AlignAfterOperator:
+ if (Current.is(tok::colon))
+CurrentState.AlignedTo = Current.getPrevious(tok::question);
+ break;
+}
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/AlignmentTest.cpp
b/clang/unittests/Format/AlignmentTest.cpp
index 971ceeefef582..9421a4c933b9e 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -3599,6 +3599,16 @@ TEST_F(AlignmentTest, ContinuedAligned) {
"\t},\n"
"\tvariant);",
Style);
+
+ Style.ColumnLimit = 40;
+ Style.IndentWidth = Style.TabWidth = Style.ContinuationIndentWidth = 8;
+
+ verifyFormat("void f() {\n"
+ "\tint =\n"
+ "\t\t01 ? 2\n"
+ "\t\t : 3;\n"
+ "}",
+ Style);
}
} // namespace
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/zeule updated
https://github.com/llvm/llvm-project/pull/196697
>From bae26699d60e6abb50ce4c97357dab3056920ece Mon Sep 17 00:00:00 2001
From: Eugene Shalygin
Date: Sat, 9 May 2026 09:06:41 +0200
Subject: [PATCH] clang-format: ensure ternary operands are aligned
Set ParentState::AlignedTo for ternary operands.
---
clang/lib/Format/ContinuationIndenter.cpp | 15 +++
clang/unittests/Format/AlignmentTest.cpp | 10 ++
2 files changed, 25 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index b2f799bb33b01..5cd771cacc193 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
+switch (Style.AlignOperands) {
+case FormatStyle::OAS_DontAlign:
+ break;
+case FormatStyle::OAS_Align:
+ CurrentState.AlignedTo = Current.is(tok::question)
+ ? Current.getPrevious(tok::equal)
+ : Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_AlignAfterOperator:
+ if (Current.is(tok::colon))
+CurrentState.AlignedTo = Current.getPrevious(tok::question);
+ break;
+}
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/AlignmentTest.cpp
b/clang/unittests/Format/AlignmentTest.cpp
index 971ceeefef582..9421a4c933b9e 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -3599,6 +3599,16 @@ TEST_F(AlignmentTest, ContinuedAligned) {
"\t},\n"
"\tvariant);",
Style);
+
+ Style.ColumnLimit = 40;
+ Style.IndentWidth = Style.TabWidth = Style.ContinuationIndentWidth = 8;
+
+ verifyFormat("void f() {\n"
+ "\tint =\n"
+ "\t\t01 ? 2\n"
+ "\t\t : 3;\n"
+ "}",
+ Style);
}
} // namespace
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: ensure ternary operands are aligned (PR #196697)
https://github.com/zeule updated
https://github.com/llvm/llvm-project/pull/196697
>From a0d72baa701df3dee6364bd3948b8d9877d1bd24 Mon Sep 17 00:00:00 2001
From: Eugene Shalygin
Date: Sat, 9 May 2026 09:06:41 +0200
Subject: [PATCH] clang-format: ensure ternary operands are aligned
Set ParentState::AlignedTo for ternary operands.
---
clang/lib/Format/ContinuationIndenter.cpp | 15 +++
clang/unittests/Format/AlignmentTest.cpp | 10 ++
2 files changed, 25 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index b2f799bb33b01..5cd771cacc193 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1276,6 +1276,21 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
CurrentState.BreakBeforeParameter = false;
CurrentState.AlignedTo = &Current;
}
+ if (!CurrentState.AlignedTo && Current.is(TT_ConditionalExpr)) {
+switch (Style.AlignOperands) {
+case FormatStyle::OAS_DontAlign:
+ break;
+case FormatStyle::OAS_Align:
+ CurrentState.AlignedTo = Current.is(tok::question)
+ ? Current.getPrevious(tok::equal)
+ : Current.getPrevious(tok::question);
+ break;
+case FormatStyle::OAS_AlignAfterOperator:
+ if (Current.is(tok::colon))
+CurrentState.AlignedTo = Current.getPrevious(tok::question);
+ break;
+}
+ }
if (!DryRun) {
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
diff --git a/clang/unittests/Format/AlignmentTest.cpp
b/clang/unittests/Format/AlignmentTest.cpp
index 971ceeefef582..9421a4c933b9e 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -3599,6 +3599,16 @@ TEST_F(AlignmentTest, ContinuedAligned) {
"\t},\n"
"\tvariant);",
Style);
+
+ Style.ColumnLimit = 40;
+ Style.IndentWidth = Style.TabWidth = Style.ContinuationIndentWidth = 8;
+
+ verifyFormat("void f() {\n"
+ "\tint =\n"
+ "\t\t01 ? 2\n"
+ "\t\t : 3;\n"
+ "}",
+ Style);
}
} // namespace
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
