Skip to content

[clang-format] Don't wrap before attributes in parameter lists #132519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down