[clang] da7f156 - [clang-format] Don't wrap before attributes in parameter lists (#132519)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 24 19:18:16 PDT 2025
Author: Owen Pan
Date: 2025-03-24T19:18:13-07:00
New Revision: da7f1564a82e431dfbbf5b8d0bd53c64c5ca6af9
URL: https://github.com/llvm/llvm-project/commit/da7f1564a82e431dfbbf5b8d0bd53c64c5ca6af9
DIFF: https://github.com/llvm/llvm-project/commit/da7f1564a82e431dfbbf5b8d0bd53c64c5ca6af9.diff
LOG: [clang-format] Don't wrap before attributes in parameter lists (#132519)
Fix #132240
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
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..0b90bd360b758 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12640,6 +12640,12 @@ TEST_F(FormatTest, UnderstandsAttributes) {
AfterType);
FormatStyle CustomAttrs = getLLVMStyle();
+ 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);
+
CustomAttrs.AttributeMacros.push_back("__unused");
CustomAttrs.AttributeMacros.push_back("__attr1");
CustomAttrs.AttributeMacros.push_back("__attr2");
More information about the cfe-commits
mailing list