Author: Eugene Shalygin
Date: 2026-05-10T14:29:39Z
New Revision: b2f37f4b0edf928dadfaf8d756d94e3c028ee25a

URL: 
https://github.com/llvm/llvm-project/commit/b2f37f4b0edf928dadfaf8d756d94e3c028ee25a
DIFF: 
https://github.com/llvm/llvm-project/commit/b2f37f4b0edf928dadfaf8d756d94e3c028ee25a.diff

LOG: clang-format: ensure ternary operands are aligned (#196697)

Set ParentState::AlignedTo for ternary operands.

Added: 
    

Modified: 
    clang/lib/Format/ContinuationIndenter.cpp
    clang/unittests/Format/AlignmentTest.cpp

Removed: 
    


################################################################################
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 aaaaaaaaaaaaaaaaaaaa =\n"
+               "\t\t000000000000000001 ? 2\n"
+               "\t\t                   : 3;\n"
+               "}",
+               Style);
 }
 
 } // namespace


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

Reply via email to