Skip to content

Commit e439fdf

Browse files
authored
[clang-format] Treat new expressions as simple functions (#105168)
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.
1 parent 74b538d commit e439fdf

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

clang/lib/Format/ContinuationIndenter.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,12 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
830830
const auto IsSimpleFunction = [&](const FormatToken &Tok) {
831831
if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown)
832832
return false;
833+
// Nested calls that involve `new` expressions also look like simple
834+
// function calls, eg:
835+
// - foo(new Bar())
836+
// - foo(::new Bar())
837+
if (Tok.is(tok::kw_new) || Tok.startsSequence(tok::coloncolon, tok::kw_new))
838+
return true;
833839
const auto *Previous = Tok.Previous;
834840
if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
835841
TT_LambdaDefinitionLParen) &&
@@ -852,6 +858,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
852858
// caaaaaaaaaaaall(
853859
// caaaaaaaaaaaall(
854860
// caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
861+
// or
862+
// caaaaaaaaaaaaaaaaaaaaal(
863+
// new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
855864
!IsSimpleFunction(Current)) {
856865
CurrentState.NoLineBreak = true;
857866
}

clang/unittests/Format/FormatTest.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -9383,6 +9383,18 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
93839383
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
93849384
" aaaaaaaaaaaaaaaa);",
93859385
Style);
9386+
verifyFormat(
9387+
"fooooooooooo(new BARRRRRRRRR(\n"
9388+
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9389+
Style);
9390+
verifyFormat(
9391+
"fooooooooooo(::new BARRRRRRRRR(\n"
9392+
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9393+
Style);
9394+
verifyFormat(
9395+
"fooooooooooo(new FOO::BARRRR(\n"
9396+
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9397+
Style);
93869398

93879399
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
93889400
Style.BinPackArguments = false;

0 commit comments

Comments
 (0)