https://github.com/chaitanyav updated 
https://github.com/llvm/llvm-project/pull/159277

>From 7eb6b33350e4887fca8ca882ab1a46fe811017b8 Mon Sep 17 00:00:00 2001
From: NagaChaitanya Vellanki <pnag...@protonmail.com>
Date: Wed, 17 Sep 2025 00:04:24 -0700
Subject: [PATCH] [clang-format] Fix for BreakTemplateDeclarations and
 RequiresClausePosition

               when BreakTemplateDeclarations is set to yes and
               RequiresClausePosition is SingleLine or WithPreceding
               requires clause should be on the same line
Fixes #150845
---
 clang/lib/Format/TokenAnnotator.cpp   |  5 ++++
 clang/unittests/Format/FormatTest.cpp | 33 +++++++++++++++++++++------
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index d97f56751ea69..f22c51af7d3f1 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5854,6 +5854,11 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
&Line,
     // concept ...
     if (Right.is(tok::kw_concept))
       return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
+    if (Right.is(TT_RequiresClause) &&
+        (Style.RequiresClausePosition == FormatStyle::RCPS_WithPreceding ||
+         Style.RequiresClausePosition == FormatStyle::RCPS_SingleLine)) {
+      return false;
+    }
     return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
            (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
             Right.NewlinesBefore > 0);
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index d9db06667d802..7bbd810dd5429 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10848,6 +10848,21 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
                "public:\n"
                "  E *f();\n"
                "};");
+  AlwaysBreak.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
+  verifyNoChange("template <typename T> requires std::floating_point<T>\n"
+                 "using LerpValue = ClampedValue<T, T{0}, T{1}>;",
+                 AlwaysBreak);
+  AlwaysBreak.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
+  verifyNoChange("template <typename T> requires std::floating_point<T>\n"
+                 "using LerpValue = ClampedValue<T, T{0}, T{1}>;",
+                 AlwaysBreak);
+  AlwaysBreak.RequiresClausePosition = FormatStyle::RCPS_OwnLine;
+  verifyFormat("template <typename T>\n"
+               "requires std::floating_point<T>\n"
+               "using LerpValue = ClampedValue<T, T{0}, T{1}>;",
+               "template <typename T> requires std::floating_point<T>\n"
+               "using LerpValue = ClampedValue<T, T{0}, T{1}>;",
+               AlwaysBreak);
 
   FormatStyle NeverBreak = getLLVMStyle();
   NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
@@ -10937,17 +10952,21 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
                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"
+  verifyFormat("template <auto x>\n"
+               "requires(x > 1)\n"
+               "constexpr int with_req(int) {\n"
+               "  return 1;\n"
+               "}",
+               "template <auto x> requires(x > 1)\n"
                "constexpr int with_req(int) {\n"
                "  return 1;\n"
                "}",
                Style);
+  verifyNoChange("template <auto x> requires(x > 1)\n"
+                 "constexpr int with_req(int) {\n"
+                 "  return 1;\n"
+                 "}",
+                 Style);
 }
 
 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to