[clang] e958faf - [clang-format] Handle TT_EnumEqual in expression precedence and RHS indentation (#232918)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 23 11:53:48 PDT 2026
Author: Andy Lim
Date: 2026-10-23T20:53:42+02:00
New Revision: e958fafc2ee3d4e0738d3449e6e43ca353b87cf0
URL: https://github.com/llvm/llvm-project/commit/e958fafc2ee3d4e0738d3449e6e43ca353b87cf0
DIFF: https://github.com/llvm/llvm-project/commit/e958fafc2ee3d4e0738d3449e6e43ca353b87cf0.diff
LOG: [clang-format] Handle TT_EnumEqual in expression precedence and RHS indentation (#232918)
Follow-up to #223011 and #223435; Fixes #231986
When #194154 changed enum assignment `=` from `TT_BinaryOperator` to
`TT_EnumEqual`, `ExpressionParser::getCurrentPrecedence()` and
`ContinuationIndenter::addTokenOnCurrentLine()` were not updated to
check `TT_EnumEqual`:
1. Because `ExpressionParser::getCurrentPrecedence()` ignored
`TT_EnumEqual`, an enum initializer with a binary expression (such as
`FLAG = 1 << 21`) was not split at `=`. Instead, `FakeLParens` for the
RHS operator was attached to the enum identifier itself, causing
`moveStatePastFakeLParens()` and `getNewLineColumn()` to apply
`ContinuationIndentWidth` twice (+8 spaces instead of +4) when breaking
after `=`.
2. When `<EnumIdentifier> = <FirstOperand>` fit within the column limit,
both the double indentation penalty after `=` and the missing
`TT_EnumEqual` check in `ContinuationIndenter::addTokenOnCurrentLine()`
caused `clang-format` to break before the binary operator (`FLAG = 1\n
<< 21`) instead of after `=`.
Include `TT_EnumEqual` in `ExpressionParser::getCurrentPrecedence()` and
`ContinuationIndenter::addTokenOnCurrentLine()` so enum initializers
with binary expressions break after `=` and indent by a single
`ContinuationIndentWidth`.
More detailed reproducer is at #231986
Added:
Modified:
clang/lib/Format/ContinuationIndenter.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 98232bb16cc04..384bad6492315 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1115,7 +1115,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
CurrentState.LastSpace = State.Column;
} else if (Previous.isOneOf(TT_ConditionalExpr, TT_CtorInitializerColon)) {
CurrentState.LastSpace = State.Column;
- } else if (Previous.is(TT_BinaryOperator) &&
+ } else if (Previous.isOneOf(TT_BinaryOperator, TT_EnumEqual) &&
((Previous.getPrecedence() != prec::Assignment &&
(Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
Previous.NextOperator)) ||
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index c6a87980264a4..9bbce9552bb9d 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3424,7 +3424,7 @@ class ExpressionParser {
Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
return prec::Relational;
}
- if (Current->isOneOf(TT_BinaryOperator, tok::comma))
+ if (Current->isOneOf(TT_BinaryOperator, TT_EnumEqual, tok::comma))
return Current->getPrecedence();
if (Current->isOneOf(tok::period, tok::arrow) &&
Current->isNot(TT_TrailingReturnArrow)) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 156a2eff27a71..f38287757ffba 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -7808,7 +7808,21 @@ TEST_F(FormatTest, EnumAssignmentContinuationIndentation) {
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
verifyFormat("enum Flag {\n"
" VeryLongFlagNameThatForcesBreak =\n"
- " 1 << 2 << 3,\n"
+ " 1 << 2 << 3,\n"
+ "};",
+ Style);
+
+ Style.ColumnLimit = 42;
+ verifyFormat("enum Flag {\n"
+ " VeryLongFlagNameThatForcesBreak =\n"
+ " 1 << 2 << 3,\n"
+ "};",
+ Style);
+
+ Style = getLLVMStyleWithColumns(42);
+ verifyFormat("enum Flag {\n"
+ " VeryLongFlagNameThatForcesBreak =\n"
+ " 1 << 2 << 3,\n"
"};",
Style);
}
More information about the cfe-commits
mailing list