[clang] c65b939 - [clang-format] SpacesInSquareBrackets not working for Java (#77833)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 12 00:30:57 PST 2024


Author: MyDeveloperDay
Date: 2024-01-12T08:30:53Z
New Revision: c65b939fb795ce4688d33531341d460a47d16664

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

LOG: [clang-format] SpacesInSquareBrackets not working for Java (#77833)

spaces in [] needs to be handled the same in Java the same as C#.

Co-authored-by: paul_hoad <paul_hoad at amat.com>

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 d2f6932880df53..24ce18a64348c1 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4674,6 +4674,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
   } else if (Style.Language == FormatStyle::LK_Java) {
     if (Left.is(tok::r_square) && Right.is(tok::l_brace))
       return true;
+    // spaces inside square brackets.
+    if (Left.is(tok::l_square) || Right.is(tok::r_square))
+      return Style.SpacesInSquareBrackets;
+
     if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
       return Style.SpaceBeforeParensOptions.AfterControlStatements ||
              spaceRequiredBeforeParens(Right);

diff  --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index 202d603d057790..6da5f4fa254331 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -603,6 +603,21 @@ TEST_F(FormatTestJava, ShortFunctions) {
                Style);
 }
 
+TEST_F(FormatTestJava, ConfigurableSpacesInSquareBrackets) {
+  FormatStyle Spaces = getLLVMStyle(FormatStyle::LK_Java);
+
+  verifyFormat("Object[] arguments", Spaces);
+  verifyFormat("final Class<?>[] types = new Class<?>[numElements];", Spaces);
+  verifyFormat("types[i] = arguments[i].getClass();", Spaces);
+
+  Spaces.SpacesInSquareBrackets = true;
+
+  verifyFormat("Object[ ] arguments", Spaces);
+  verifyFormat("final Class<?>[ ] types = new Class<?>[ numElements ];",
+               Spaces);
+  verifyFormat("types[ i ] = arguments[ i ].getClass();", Spaces);
+}
+
 } // namespace
 } // namespace test
 } // namespace format


        


More information about the cfe-commits mailing list