[clang] [clang-format] Fix missing indentation for assignment with attributes (PR #230417)
Priyanshu Ranjan via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 9 02:40:23 PDT 2026
https://github.com/priyanshujaiz updated https://github.com/llvm/llvm-project/pull/230417
>From 480dca9c02b7ac212a074eab91cd9de5aa716274 Mon Sep 17 00:00:00 2001
From: Priyanshu Jaiz <pj041132 at example.com>
Date: Fri, 9 Oct 2026 09:02:04 +0000
Subject: [PATCH] [clang-format] Fix missing indentation for assignment with
attributes Fixes #229206. When `BreakBeforeBinaryOperators: All` is
configured, formatting a variable definition with an attribute (e.g.
`[[maybe_unused]]`) between the variable name and the assignment previously
caused the assignment operator to lose all continuation indentation (starting
at column 0). The root cause was in
`ContinuationIndenter::getNewLineColumn()`. The logic assumed that any token
following an attribute closing bracket (`TT_AttributeRSquare`) should not be
indented as a continuation. It failed to account for binary operators (like
`=`) which are valid continuations of the variable declaration statement. A
regression test has been added to
`FormatTest.BreakBinaryOperatorsInPresenceOfTemplates` to verify the 4-space
continuation indent is correctly applied when formatting wraps at the column
limit. Assisted-by: Antigravity AI
---
clang/lib/Format/ContinuationIndenter.cpp | 5 +++--
clang/unittests/Format/FormatTest.cpp | 9 +++++++++
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index bb8179cbc4f95f..636ab7d24ee316 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1692,10 +1692,11 @@ ContinuationIndenter::getNewLineColumn(const LineState &State) {
Current.isNot(tok::l_paren) &&
!Current.endsSequence(TT_StartOfName, TT_AttributeMacro,
TT_PointerOrReference)) ||
- PreviousNonComment->isOneOf(TT_AttributeRParen, TT_AttributeRSquare,
+ (PreviousNonComment->isOneOf(TT_AttributeRParen, TT_AttributeRSquare,
TT_FunctionAnnotationRParen,
TT_JavaAnnotation,
- TT_LeadingJavaAnnotation))) ||
+ TT_LeadingJavaAnnotation) &&
+ !Current.isBinaryOperator()))) ||
(!Style.IndentWrappedFunctionNames &&
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)) ||
(State.Line->ReturnTypeWrapped && PreviousNonComment &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d346036448140c..c77a0db87d31b6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -7790,6 +7790,15 @@ TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
" and is_swappable_v<hash<T>>\n"
" and is_callable_v<hash<T>(T)>;",
Style);
+
+ FormatStyle Style80 = getLLVMStyle();
+ Style80.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
+ verifyFormat("static constexpr long long variable_with_long_name_without_attribute\n"
+ " = 1234567890;\n"
+ "static constexpr long long long_variable_with_attribute "
+ "[[maybe_unused]]\n"
+ " = 9876543210;",
+ Style80);
}
TEST_F(FormatTest, ConstructorInitializers) {
More information about the cfe-commits
mailing list