[clang] [clang-format] Add BreakBeforeTemplateClose option (PR #118046)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 11 23:14:35 PST 2025


================
@@ -2252,6 +2252,25 @@ struct FormatStyle {
   /// \version 16
   BreakBeforeInlineASMColonStyle BreakBeforeInlineASMColon;
 
+  /// If ``true``, a line break will be placed before the ``>`` in a multiline
+  /// template declaration.
+  /// \code
+  ///    true:
+  ///    template <
+  ///        typename Foo,
+  ///        typename Bar,
+  ///        typename Baz
+  ///    >
+  ///
+  ///    false:
+  ///    template <
+  ///        typename Foo,
+  ///        typename Bar,
+  ///        typename Baz>
----------------
owenca wrote:

> In my mind that's a bit of a different option. You can see in your examples that you're assuming that the `>` goes on the next line, but that's what this PR is trying to add support for.

`Always` would break before a template closer unconditionally.

> Like, your enum (which is totally reasonable) would switch between:
> 
> ```
> template <
>     typename Foo, typename Bar,
>     typename Baz
> >
> ```
> 
> versus
> 
> ```
> template <typename Foo, typename Bar,
>           typename Baz
> >
> ```
> 
> But what I'm currently trying to add support for is:
> 
> ```
> template <
>     typename Foo, typename Bar,
>     typename Baz
> >
> 
> template <typename Foo, typename Bar,
>           typename Baz
> >
> ```
> 
> versus
> 
> ```
> template <
>     typename Foo, typename Bar,
>     typename Baz>
> 
> template <typename Foo, typename Bar,
>           typename Baz>
> ```

I don't recall that you had a test case for the last pattern, but it seems that your current implementation is `Multiline`, which would break before the template closer if the template doesn't fit on one line regardless whether there is a break after the template opener (`BlockIndent`).

To clarify:
- BlockIndent
```
template <
    typename Foo, typename Bar,
    typename Baz
>

template <typename Foo, typename Bar,
          typename Baz>
```
- Multiline
```
template <
    typename Foo, typename Bar,
    typename Baz
>

template <typename Foo, typename Bar,
          typename Baz
>
```

> So I believe that should be another patch.

Like I said, I'm ok with some of the enumerated values being implemented in another patch.

https://github.com/llvm/llvm-project/pull/118046


More information about the cfe-commits mailing list