[clang] [clang-format] Add Leave to AlwaysBreakTemplateDeclarations (PR #80569)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 3 16:05:00 PST 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/80569
Now with a8279a8bc541, we can make the update.
>From 45b720cbda6b08ca55b7d131bb0541f38f36e7c9 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 3 Feb 2024 16:01:49 -0800
Subject: [PATCH] [clang-format] Add Leave to AlwaysBreakTemplateDeclarations
Closes #78067.
---
clang/docs/ClangFormatStyleOptions.rst | 12 ++++
clang/include/clang/Format/Format.h | 10 +++
clang/lib/Format/ContinuationIndenter.cpp | 5 +-
clang/lib/Format/Format.cpp | 1 +
clang/lib/Format/TokenAnnotator.cpp | 4 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 +
clang/unittests/Format/FormatTest.cpp | 78 ++++++++++++++++++++++
7 files changed, 110 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 0b887288fe2cb..5e23b16fa5910 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -1638,6 +1638,18 @@ the configuration (without a prefix: ``Auto``).
Possible values:
+ * ``BTDS_Leave`` (in configuration: ``Leave``)
+ Do not change the line breaking before the declaration.
+
+ .. code-block:: c++
+
+ template <typename T>
+ T foo() {
+ }
+ template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa,
+ int bbbbbbbbbbbbbbbbbbbbb) {
+ }
+
* ``BTDS_No`` (in configuration: ``No``)
Do not force break before declaration.
``PenaltyBreakTemplateDeclaration`` is taken into account.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index efcb4e1d87ea4..0328854d83165 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1008,6 +1008,16 @@ struct FormatStyle {
/// Different ways to break after the template declaration.
enum BreakTemplateDeclarationsStyle : int8_t {
+ /// Do not change the line breaking before the declaration.
+ /// \code
+ /// template <typename T>
+ /// T foo() {
+ /// }
+ /// template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa,
+ /// int bbbbbbbbbbbbbbbbbbbbb) {
+ /// }
+ /// \endcode
+ BTDS_Leave,
/// Do not force break before declaration.
/// ``PenaltyBreakTemplateDeclaration`` is taken into account.
/// \code
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index a3eb9138b2183..a4e4b7ace684d 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -561,7 +561,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
return true;
}
}
- return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No;
+ return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No &&
+ (Style.AlwaysBreakTemplateDeclarations !=
+ FormatStyle::BTDS_Leave ||
+ Current.NewlinesBefore > 0);
}
if (Previous.is(TT_FunctionAnnotationRParen) &&
State.Line->Type != LT_PreprocessorDirective) {
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 10fe35c79a4f2..7ffc73ca6629e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -296,6 +296,7 @@ template <>
struct ScalarEnumerationTraits<FormatStyle::BreakTemplateDeclarationsStyle> {
static void enumeration(IO &IO,
FormatStyle::BreakTemplateDeclarationsStyle &Value) {
+ IO.enumCase(Value, "Leave", FormatStyle::BTDS_Leave);
IO.enumCase(Value, "No", FormatStyle::BTDS_No);
IO.enumCase(Value, "MultiLine", FormatStyle::BTDS_MultiLine);
IO.enumCase(Value, "Yes", FormatStyle::BTDS_Yes);
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d0c4273cfc7e5..fd02faf85fede 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5182,7 +5182,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
// concept ...
if (Right.is(tok::kw_concept))
return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
- return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes;
+ return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
+ (Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
+ Right.NewlinesBefore > 0);
}
if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
switch (Style.RequiresClausePosition) {
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 6436581ddae5a..d32acd2bd32dc 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -691,6 +691,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::RTBS_TopLevelDefinitions);
Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
+ CHECK_PARSE("AlwaysBreakTemplateDeclarations: Leave",
+ AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Leave);
CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a471e36f8d682..50d0da4dff33e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10657,6 +10657,84 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
"bbbbbbbbbbbbbbbbbbbb) {}",
NeverBreak);
+
+ auto Style = getLLVMStyle();
+ Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Leave;
+
+ verifyNoChange("template <typename T>\n"
+ "class C {};",
+ Style);
+ verifyFormat("template <typename T> class C {};", Style);
+
+ verifyNoChange("template <typename T>\n"
+ "void f();",
+ Style);
+ verifyFormat("template <typename T> void f();", Style);
+
+ verifyNoChange("template <typename T>\n"
+ "void f() {}",
+ Style);
+ verifyFormat("template <typename T> void f() {}", Style);
+
+ verifyNoChange("template <typename T>\n"
+ "// T can be A, B or C.\n"
+ "struct C {};",
+ Style);
+ verifyFormat("template <typename T> // T can be A, B or C.\n"
+ "struct C {};",
+ Style);
+
+ verifyNoChange("template <typename T>\n"
+ "C(T) noexcept;",
+ Style);
+ verifyFormat("template <typename T> C(T) noexcept;", Style);
+
+ verifyNoChange("template <typename T>\n"
+ "ClassName(T) noexcept;",
+ Style);
+ verifyFormat("template <typename T> ClassName(T) noexcept;", Style);
+
+ verifyNoChange("template <typename T>\n"
+ "POOR_NAME(T) noexcept;",
+ Style);
+ verifyFormat("template <typename T> POOR_NAME(T) noexcept;", Style);
+
+ verifyNoChange("template <enum E>\n"
+ "class A {\n"
+ "public:\n"
+ " E *f();\n"
+ "};",
+ Style);
+ verifyFormat("template <enum E> class A {\n"
+ "public:\n"
+ " E *f();\n"
+ "};",
+ Style);
+
+ verifyNoChange("template <auto x>\n"
+ "constexpr int simple(int) {\n"
+ " char c;\n"
+ " return 1;\n"
+ "}",
+ Style);
+ verifyFormat("template <auto x> constexpr int simple(int) {\n"
+ " char c;\n"
+ " return 1;\n"
+ "}",
+ Style);
+
+ Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
+ verifyNoChange("template <auto x>\n"
+ "requires(x > 1)\n"
+ "constexpr int with_req(int) {\n"
+ " return 1;\n"
+ "}",
+ Style);
+ verifyFormat("template <auto x> requires(x > 1)\n"
+ "constexpr int with_req(int) {\n"
+ " return 1;\n"
+ "}",
+ Style);
}
TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
More information about the cfe-commits
mailing list