[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-08 Thread via cfe-commits

https://github.com/mydeveloperday commented:

Looks good

https://github.com/llvm/llvm-project/pull/91112
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-06 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/91112
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-06 Thread Emilia Kond via cfe-commits

https://github.com/rymiel approved this pull request.

Also works well for pattern matching
```java
switch (obj) {
  case String s when !s.isEmpty() -> 0;
  default -> 1;
}
```
and upcoming destructuring
```java
switch (n) {
  case IntExpr(int i)  -> i;
  case NegExpr(Expr n) -> -1 * eval(n);
  case AddExpr(Expr lhs, Expr rhs) -> eval(lhs) + eval(rhs);
  case MulExpr(Expr lhs, Expr rhs) -> eval(lhs) * eval(rhs);
  default  -> throw new IllegalStateException();
};
```

https://github.com/llvm/llvm-project/pull/91112
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread via cfe-commits

mydeveloperday wrote:

This looks good

https://github.com/llvm/llvm-project/pull/91112
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/91112

>From beab69244ce686a1d53342979d46f269a46c79de Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..6d092219877f91 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: java
+
+  true:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: java
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..74893f23210cd0 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code{.java}
+///   true:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ -396,12 +

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/91112

>From 31a45ace7d828c63b31d0ba20ed07f2a0340c41d Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f0f1f44491251c 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..7253f3b57b3003 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ -396,12 +413,14 @@

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/91112

>From be8569c3721337317635a2f0640237a2d5acd73a Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH 1/2] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f320caf0d21998 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..d08db77c2ab063 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ -3

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Owen Pan (owenca)


Changes

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows 
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.

---

Patch is 26.58 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/91112.diff


14 Files Affected:

- (modified) clang/docs/ClangFormatStyleOptions.rst (+35-1) 
- (modified) clang/docs/ReleaseNotes.rst (+3) 
- (modified) clang/include/clang/Format/Format.h (+35-1) 
- (modified) clang/lib/Format/Format.cpp (+4) 
- (modified) clang/lib/Format/FormatToken.h (+3) 
- (modified) clang/lib/Format/TokenAnnotator.cpp (+2) 
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+6) 
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+37-9) 
- (modified) clang/lib/Format/UnwrappedLineParser.h (+1-1) 
- (modified) clang/lib/Format/WhitespaceManager.cpp (+14-8) 
- (modified) clang/lib/Format/WhitespaceManager.h (+1-1) 
- (modified) clang/unittests/Format/ConfigParseTest.cpp (+2) 
- (modified) clang/unittests/Format/FormatTestJava.cpp (+171) 
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+18) 


``diff
diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f320caf0d21998 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..d08db77c2ab063 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ -396,12 +413,14 @@ struct FormatStyle {
 bool operator==(const ShortCaseSt

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/91112

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows 
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.

>From be8569c3721337317635a2f0640237a2d5acd73a Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f320caf0d21998 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..d08db77c2ab063 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool Align