Skip to content

Commit

Permalink
[clang-format] Treat new expressions as simple functions
Browse files Browse the repository at this point in the history
ccae7b4 improved handling for nested
calls, but this resulted in a lot of changes near `new` expressions.

This patch tries to restore previous behavior around new expressions, by
treating them as simple functions, which seem to align with the concept.

Fixes #105133.
  • Loading branch information
kadircet committed Aug 22, 2024
1 parent c368a72 commit d0f1e70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
const auto IsSimpleFunction = [&](const FormatToken &Tok) {
if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown)
return false;
// Nested calls that inolve `new` expressions also look like simple
// function calls, eg:
// - foo(new Bar())
if (Tok.is(tok::kw_new))
return true;
const auto *Previous = Tok.Previous;
if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
TT_LambdaDefinitionLParen) &&
Expand All @@ -870,6 +875,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// caaaaaaaaaaaall(
// caaaaaaaaaaaall(
// caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
// or
// caaaaaaaaaaaaaaaaaaaaal(
// new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
!IsSimpleFunction(Current)) {
CurrentState.NoLineBreak = true;
}
Expand Down
8 changes: 8 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9292,6 +9292,14 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
" aaaaaaaaaaaaaaaa);",
Style);
verifyFormat(
"fooooooooooo(new BARRRRRRRRR(\n"
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
Style);
verifyFormat(
"fooooooooooo(new FOO::BARRRR(\n"
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
Style);

Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
Style.BinPackArguments = false;
Expand Down

0 comments on commit d0f1e70

Please sign in to comment.