[clang] [clang-format] Don't wrap before attributes in parameter lists (PR #132519)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 21 21:39:24 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fix #<!-- -->132240
---
Full diff: https://github.com/llvm/llvm-project/pull/132519.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+14)
- (modified) clang/unittests/Format/FormatTest.cpp (+6)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/132519
More information about the cfe-commits
mailing list