[clang] [clang-format] Don't wrap before attributes in parameter lists (PR #132519)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 21 21:38:54 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/132519
Fix #132240
>From 16137a72cda66f20cf381b6eb6af11375ef39f92 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 21 Mar 2025 21:34:52 -0700
Subject: [PATCH] [clang-format] Don't wrap before attributes in parameter
lists
Fix #132240
---
clang/lib/Format/TokenAnnotator.cpp | 14 ++++++++++++++
clang/unittests/Format/FormatTest.cpp | 6 ++++++
2 files changed, 20 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 35577cd6db7a1..01bf8c3778928 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4078,6 +4078,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
}
bool InFunctionDecl = Line.MightBeFunctionDecl;
+ bool InParameterList = false;
for (auto *Current = First->Next; Current; Current = Current->Next) {
const FormatToken *Prev = Current->Previous;
if (Current->is(TT_LineComment)) {
@@ -4132,6 +4133,19 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Current->CanBreakBefore =
Current->MustBreakBefore || canBreakBefore(Line, *Current);
+
+ if (Current->is(TT_FunctionDeclarationLParen)) {
+ InParameterList = true;
+ } else if (Current->is(tok::r_paren)) {
+ const auto *LParen = Current->MatchingParen;
+ if (LParen && LParen->is(TT_FunctionDeclarationLParen))
+ InParameterList = false;
+ } else if (InParameterList &&
+ Current->endsSequence(TT_AttributeMacro,
+ TT_PointerOrReference)) {
+ Current->CanBreakBefore = false;
+ }
+
unsigned ChildSize = 0;
if (Prev->Children.size() == 1) {
FormatToken &LastOfChild = *Prev->Children[0]->Last;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5df7865f5a629..7e5b285699d59 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12664,6 +12664,12 @@ TEST_F(FormatTest, UnderstandsAttributes) {
verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
+ CustomAttrs.AttributeMacros.push_back("my_attr_name");
+ verifyFormat("void MyGoodOldFunction(\n"
+ " void *const long_enough = nullptr,\n"
+ " void *my_attr_name even_longeeeeeeeeeeeeeeeeer = nullptr);",
+ CustomAttrs);
+
// Check that these are not parsed as function declarations:
CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
More information about the cfe-commits
mailing list