[PATCH] D140267: [clang-format] Allow line break between template closer and right paren

Emilia Dreamer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 17 17:56:34 PST 2022


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

This allows for `AlignAfterOpenBracket: BlockIndent` to work correctly
when using values with type parameters.

Line breaks after any template closer was disallowed 10 years ago, in
90e51fdbab33d43e0d4f932c2d90d0b86e865664, as a fix to https://github.com/llvm/llvm-project/issues/15158.
Allowing it again, but only if preceding a specific character, seems to
work out fine.

Note that this change originally caused a test failure in `FormatTestJS.NoBreakAfterAsserts`,
which is why there is an explicit negative check against `TT_JsTypeColon`.

Given this, I'm not 100% confident in this change not producing defects,
especially in other languages, but that was the only one in the test
suite.
Perhaps additional restrictions should be used, such as requiring
`BAS_BlockIndent` to be set.

Fixes https://github.com/llvm/llvm-project/issues/59567


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140267

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -24868,6 +24868,12 @@
                "(\n"
                ") = nullptr;",
                Style);
+
+  verifyFormat("static_assert(\n"
+               "    std::is_base_of_v<std::output_iterator_tag, Category> ||\n"
+               "    std::is_base_of_v<std::input_iterator_tag, Category>\n"
+               ");",
+               Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -4952,6 +4952,10 @@
     return false;
   if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
     return true;
+  if (Left.is(TT_TemplateCloser) && Right.is(tok::r_paren) &&
+      (!Right.Next || Right.Next->isNot(TT_JsTypeColon))) {
+    return true;
+  }
   if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
       (Left.is(tok::less) && Right.is(tok::less))) {
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140267.483794.patch
Type: text/x-patch
Size: 1210 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221218/14eef9b6/attachment-0001.bin>


More information about the cfe-commits mailing list