Skip to content

Conversation

@chaitanyav
Copy link
Contributor

when BreakTemplateDeclarations is set to yes and
RequiresClausePosition is SingleLine or WithPreceding
requires clause should be on the same line

Fixes #150845

@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-clang-format

Author: NagaChaitanya Vellanki (chaitanyav)

Changes

when BreakTemplateDeclarations is set to yes and
RequiresClausePosition is SingleLine or WithPreceding
requires clause should be on the same line

Fixes #150845


Full diff: https://github.com/llvm/llvm-project/pull/159277.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+5)
  • (modified) clang/unittests/Format/FormatTest.cpp (+13)
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..efbc86af72c17 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10848,6 +10848,19 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
                "public:\n"
                "  E *f();\n"
                "};");
+  Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
+  verifyNoChange("template <typename T> requires std::floating_point<T>\n"
+                 "using LerpValue = ClampedValue<T, T{ 0 }, T{ 1 }>;",
+                 Style);
+  Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
+  verifyNoChange("template <typename T> requires std::floating_point<T>\n"
+                 "using LerpValue = ClampedValue<T, T{ 0 }, T{ 1 }>;",
+                 Style);
+  Style.RequiresClausePosition = FormatStyle::RCPS_OwnLine;
+  verifyFormat("template <typename T>\n"
+               "requires std::floating_point<T>\n"
+               "using LerpValue = ClampedValue<T, T{ 0 }, T{ 1 }>;",
+               Style);
 
   FormatStyle NeverBreak = getLLVMStyle();
   NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;

@chaitanyav chaitanyav force-pushed the fix_150845 branch 5 times, most recently from 7eb6b33 to c73c23f Compare September 17, 2025 18:24
Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but wait if someone else wants to say something.

@chaitanyav
Copy link
Contributor Author

@llvm/pr-subscribers-clang-format please review.

@chaitanyav chaitanyav self-assigned this Sep 20, 2025
Copy link
Contributor

@owenca owenca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…sition

               when BreakTemplateDeclarations is set to yes and
               RequiresClausePosition is SingleLine or WithPreceding
               requires clause should be on the same line
Fixes llvm#150845
@chaitanyav
Copy link
Contributor Author

Closing this PR since its not a bug as per #150845 (comment)

@chaitanyav chaitanyav closed this Sep 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-format] BreakTemplateDeclarations and RequiresClausePosition

4 participants