Skip to content

Commit 16137a7

Browse files
committed
[clang-format] Don't wrap before attributes in parameter lists
Fix #132240
1 parent 4b86a7f commit 16137a7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,6 +4078,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40784078
}
40794079

40804080
bool InFunctionDecl = Line.MightBeFunctionDecl;
4081+
bool InParameterList = false;
40814082
for (auto *Current = First->Next; Current; Current = Current->Next) {
40824083
const FormatToken *Prev = Current->Previous;
40834084
if (Current->is(TT_LineComment)) {
@@ -4132,6 +4133,19 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
41324133

41334134
Current->CanBreakBefore =
41344135
Current->MustBreakBefore || canBreakBefore(Line, *Current);
4136+
4137+
if (Current->is(TT_FunctionDeclarationLParen)) {
4138+
InParameterList = true;
4139+
} else if (Current->is(tok::r_paren)) {
4140+
const auto *LParen = Current->MatchingParen;
4141+
if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4142+
InParameterList = false;
4143+
} else if (InParameterList &&
4144+
Current->endsSequence(TT_AttributeMacro,
4145+
TT_PointerOrReference)) {
4146+
Current->CanBreakBefore = false;
4147+
}
4148+
41354149
unsigned ChildSize = 0;
41364150
if (Prev->Children.size() == 1) {
41374151
FormatToken &LastOfChild = *Prev->Children[0]->Last;

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12664,6 +12664,12 @@ TEST_F(FormatTest, UnderstandsAttributes) {
1266412664
verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
1266512665
verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
1266612666

12667+
CustomAttrs.AttributeMacros.push_back("my_attr_name");
12668+
verifyFormat("void MyGoodOldFunction(\n"
12669+
" void *const long_enough = nullptr,\n"
12670+
" void *my_attr_name even_longeeeeeeeeeeeeeeeeer = nullptr);",
12671+
CustomAttrs);
12672+
1266712673
// Check that these are not parsed as function declarations:
1266812674
CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
1266912675
CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;

0 commit comments

Comments
 (0)