[clang] [clang-format] Fix a bug in ContinuationIndenter (PR #66354)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 03:13:07 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang
            
<details>
<summary>Changes</summary>
See https://reviews.llvm.org/D136154#3890747 for context.

Fixes part of #58592.
--
Full diff: https://github.com/llvm/llvm-project/pull/66354.diff

2 Files Affected:

- (modified) clang/lib/Format/ContinuationIndenter.cpp (+3-2) 
- (modified) clang/unittests/Format/FormatTest.cpp (+23) 


<pre>
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 75ab08de42ea0e8..8f14105751daefa 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -876,8 +876,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &amp;State, bool DryRun,
                  FormatStyle::BCIS_AfterColon) {
     CurrentState.Indent = State.Column;
     CurrentState.LastSpace = State.Column;
-  } else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
-                               TT_CtorInitializerColon)) &amp;&amp;
+  } else if (Previous.isOneOf(TT_ConditionalExpr, TT_CtorInitializerColon)) {
+    State.Stack.back().LastSpace = State.Column;
+  } else if (Previous.is(TT_BinaryOperator) &amp;&amp;
              ((Previous.getPrecedence() != prec::Assignment &amp;&amp;
                (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
                 Previous.NextOperator)) ||
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4f72166bdce2d73..0d0fbdb84e3271b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -7108,6 +7108,29 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
                &quot;(someOtherLongishConditionPart1 || &quot;
                &quot;someOtherEvenLongerNestedConditionPart2);&quot;,
                Style);
+
+  Style = getLLVMStyleWithColumns(20);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+  Style.BinPackParameters = false;
+  Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat(&quot;struct Foo {\n&quot;
+               &quot;  Foo(\n&quot;
+               &quot;    int arg1,\n&quot;
+               &quot;    int arg2)\n&quot;
+               &quot;      : Base(\n&quot;
+               &quot;          arg1,\n&quot;
+               &quot;          arg2) {}\n&quot;
+               &quot;};&quot;,
+               Style);
+  verifyFormat(&quot;return abc\n&quot;
+               &quot;         ? foo(\n&quot;
+               &quot;             a,\n&quot;
+               &quot;             b,\n&quot;
+               &quot;             bar(\n&quot;
+               &quot;               abc))\n&quot;
+               &quot;         : g(abc);&quot;,
+               Style);
 }
 
 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66354


More information about the cfe-commits mailing list