https://github.com/matthiasgoergens created 
https://github.com/llvm/llvm-project/pull/214521

Fixes llvm/llvm-project#212796.

## Cause

Commit `0f37c48d459d6bba3ab6866cceb57539dce002fa` introduced `TT_EnumEqual` so
enum assignments could be aligned independently. It retained the token's
underlying `tok::equal`, but omitted the new token type from
`ExpressionParser::getCurrentPrecedence`. Enumerator initialisers consequently
lost their assignment-precedence fake parentheses and their continuation
indentation.

## Fix

Recognise `TT_EnumEqual` alongside ordinary binary operators while constructing
the expression structure. `getPrecedence()` still yields `prec::Assignment`
from the underlying equals token, while the specialised type remains available
to the later enum-alignment pass.

## Trigger (reproduced on current main)

A freshly built unmodified current main reproduces the issue: at a 40-column
limit, clang-format breaks before the final operand of a wrapped enumerator
initializer and leaves that operand at the enumerator's two-space indentation
instead of the continuation indentation (regression in v23 vs v22).

## Verification

- The freshly built patched formatter breaks after the equals sign and gives
  the initializer its six-space continuation indentation. The regression test
  checks this exact input and output.
- All 1,276 freshly rebuilt formatter tests pass, including
  `FormatTest.WrapsEnumInitializers` and the existing
  consecutive-enum-alignment suite.
- Independent source-level audit confirmed the token classification and
  expression parser have not otherwise changed since the original candidate
  base; no competing fix or semantic blocker was found.
- `git diff --check` passes.
- Rebased cleanly onto upstream main (`1cc730fd7b31`, 2026-08-06) for this
  review PR.

From 00298cba1778ddaad95e40f1e0acef7d242d59ca Mon Sep 17 00:00:00 2001
From: Matthias Goergens <[email protected]>
Date: Sun, 2 Aug 2026 23:39:41 +0800
Subject: [PATCH] [clang-format] Restore expression precedence for enum
 assignments

AlignConsecutiveEnums gives enumerator equals signs a distinct token type, but 
the expression parser no longer recognises them as assignment operators. 
Include TT_EnumEqual when constructing fake parentheses so wrapped initializer 
expressions retain their continuation indentation.
---
 clang/lib/Format/TokenAnnotator.cpp   |  2 +-
 clang/unittests/Format/FormatTest.cpp | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 32ae8990f52c5..56d66d6f4fad6 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3437,7 +3437,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 b72a683ac1fff..a57af2cbd963c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -2898,6 +2898,17 @@ TEST_F(FormatTest, ShortEnums) {
                Style);
 }
 
+TEST_F(FormatTest, WrapsEnumInitializers) {
+  verifyFormat("enum {\n"
+               "  MyEnum =\n"
+               "      aaaaaaaaaaaaaaa + bbbbbbbbbbbbbbb\n"
+               "};",
+               "enum {\n"
+               "  MyEnum = aaaaaaaaaaaaaaa + bbbbbbbbbbbbbbb\n"
+               "};",
+               getLLVMStyleWithColumns(40));
+}
+
 TEST_F(FormatTest, ShortCompoundRequirement) {
   constexpr StringRef Code("template <typename T>\n"
                            "concept c = requires(T x) {\n"

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to