[flang-commits] [compiler-rt] [libcxx] [lld] [mlir] [clang] [llvm] [openmp] [flang] [lldb] [clang-format] SpacesInSquareBrackets not working for Java (PR #77833)

via flang-commits flang-commits at lists.llvm.org
Thu Jan 11 13:00:04 PST 2024


https://github.com/mydeveloperday created https://github.com/llvm/llvm-project/pull/77833

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


>From 4ccf762ec644b4f79b2862750035ec7a97cd74fc Mon Sep 17 00:00:00 2001
From: paul_hoad <paul_hoad at amat.com>
Date: Thu, 11 Jan 2024 20:50:48 +0000
Subject: [PATCH] [clang-format] SpacesInSquareBrackets not working for Java

spaces in [] needs to be handled the same in Java the same as C#.
---
 clang/lib/Format/TokenAnnotator.cpp       |  4 ++++
 clang/unittests/Format/FormatTestJava.cpp | 15 +++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 227aa0b97af6ba..bcd12ab65816b0 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4668,6 +4668,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 flang-commits mailing list