[clang] 51ba660 - [clang-format] Inherit RightAlign options across scopes

Emilia Dreamer via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 10 20:02:34 PST 2023


Author: Emilia Dreamer
Date: 2023-01-11T05:59:45+02:00
New Revision: 51ba660a0700e659e1a97f3ca5879b8617ec5c26

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

LOG: [clang-format] Inherit RightAlign options across scopes

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 99261bdf4519e..9951906b6af01 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -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;
     }

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 550c5a29f75e6..b39cae1ebe3f2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"


        


More information about the cfe-commits mailing list