[clang] d8d331b - [clang-format] Fix aligning of java-style declarations

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 15 20:27:47 PDT 2022


Author: Danil Sidoruk
Date: 2022-08-15T20:27:15-07:00
New Revision: d8d331bc97103b6e191b96189166fefc6be54148

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

LOG: [clang-format] Fix aligning of java-style declarations

- Modify TokenAnnotator to work fine with java-style array declarations.
- Add test for aligning of java declarations.

Fixes #55931.

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

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/FormatTestJava.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index b0a1c691c771a..5ea1506bae193 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2065,6 +2065,11 @@ class AnnotatingParser {
     if (PreviousNotConst->isSimpleTypeSpecifier())
       return true;
 
+    // type[] a in Java
+    if (Style.Language == FormatStyle::LK_Java &&
+        PreviousNotConst->is(tok::r_square))
+      return true;
+
     // const a = in JavaScript.
     return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
   }

diff  --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index 03e16ae0a00d3..7b25a0bb4bdfd 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -584,6 +584,17 @@ TEST_F(FormatTestJava, AlignsBlockComments) {
                    "  void f() {}"));
 }
 
+TEST_F(FormatTestJava, AlignDeclarations) {
+  FormatStyle Style = getLLVMStyle(FormatStyle::LK_Java);
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("private final String[]       args;\n"
+               "private final A_ParserHelper parserHelper;\n"
+               "private final int            numOfCmdArgs;\n"
+               "private int                  numOfCmdArgs;\n"
+               "private String[]             args;",
+               Style);
+}
+
 TEST_F(FormatTestJava, KeepsDelimitersOnOwnLineInJavaDocComments) {
   EXPECT_EQ("/**\n"
             " * javadoc line 1\n"


        


More information about the cfe-commits mailing list