[clang-format] Inherit RightAlign options across scopes
authorEmilia Dreamer <emilia@rymiel.space>
Wed, 11 Jan 2023 03:56:08 +0000 (05:56 +0200)
committerEmilia Dreamer <emilia@rymiel.space>
Wed, 11 Jan 2023 03:59:45 +0000 (05:59 +0200)
D119599 added the ability to align compound assignments, right aligning
them in order to line up at the equals sign.
However, that patch didn't account for AlignTokens being called
recursively across scopes, which reset the right justification to be
false in any scope besides the top scope. This meant the compound
assignments were aligned, just not at the right place.
(No tests also ever introduced any scopes)

This patch makes sure to inherit the right justification value, just as
every other parameter is passed on.

Fixes https://github.com/llvm/llvm-project/issues/58029

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D141288

clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp

index 99261bd..9951906 100644 (file)
@@ -609,7 +609,8 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
       ++CommasBeforeMatch;
     } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
       // Call AlignTokens recursively, skipping over this scope block.
-      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
+      unsigned StoppedAt =
+          AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
       i = StoppedAt - 1;
       continue;
     }
index 550c5a2..b39cae1 100644 (file)
@@ -17693,6 +17693,20 @@ TEST_F(FormatTest, AlignCompoundAssignments) {
                "dvsdsv        <<= 5;\n"
                "int dsvvdvsdvvv = 123;",
                Alignment);
+  verifyFormat("int xxx = 5;\n"
+               "xxx     = 5;\n"
+               "{\n"
+               "  int yyy = 6;\n"
+               "  yyy     = 6;\n"
+               "}",
+               Alignment);
+  verifyFormat("int xxx = 5;\n"
+               "xxx    += 5;\n"
+               "{\n"
+               "  int yyy = 6;\n"
+               "  yyy    += 6;\n"
+               "}",
+               Alignment);
   // Test that `<=` is not treated as a compound assignment.
   verifyFormat("aa &= 5;\n"
                "b <= 10;\n"