[clang] [clang-format] Treat new expressions as simple functions (PR #105168)
kadir çetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 22 02:02:05 PDT 2024
https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/105168
>From d0f1e7032647845fbe235856998e86bfbb59e937 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Tue, 20 Aug 2024 19:40:42 +0200
Subject: [PATCH] [clang-format] Treat new expressions as simple functions
ccae7b461be339e717d02f99ac857cf0bc7d17f 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 https://github.com/llvm/llvm-project/issues/105133.
---
clang/lib/Format/ContinuationIndenter.cpp | 8 ++++++++
clang/unittests/Format/FormatTest.cpp | 8 ++++++++
2 files changed, 16 insertions(+)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 46dafad65863dc..3184c5970b285a 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -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) &&
@@ -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;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 794ccab3704534..7c14c1b65c5022 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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;
More information about the cfe-commits
mailing list