[PATCH] D129628: Fix aligning of java-style declarations

Danil Sidoruk via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 13 03:21:05 PDT 2022


eoanermine created this revision.
eoanermine added reviewers: MyDeveloperDay, owenpan, HazardyKnusperkeks, curdeius.
eoanermine added projects: clang, clang-format.
Herald added a project: All.
eoanermine requested review of this revision.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129628

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


Index: clang/unittests/Format/FormatTestJava.cpp
===================================================================
--- clang/unittests/Format/FormatTestJava.cpp
+++ clang/unittests/Format/FormatTestJava.cpp
@@ -584,6 +584,17 @@
                    "  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"
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2028,6 +2028,11 @@
     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);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129628.444199.patch
Type: text/x-patch
Size: 1427 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220713/868d93c5/attachment.bin>


More information about the cfe-commits mailing list