Skip to content
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

[clang-format] Treat new expressions as simple functions #105168

Merged
merged 1 commit into from
Aug 24, 2024

Conversation

kadircet
Copy link
Member

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.

@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2024

@llvm/pr-subscribers-clang-format

Author: kadir çetinkaya (kadircet)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/105168.diff

2 Files Affected:

  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+2)
  • (modified) clang/unittests/Format/FormatTest.cpp (+4)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 46dafad65863dc..8f65a1445dc3a5 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -848,6 +848,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
   const auto IsSimpleFunction = [&](const FormatToken &Tok) {
     if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown)
       return false;
+    if (Tok.is(tok::kw_new))
+      return true;
     const auto *Previous = Tok.Previous;
     if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
                                          TT_LambdaDefinitionLParen) &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 794ccab3704534..bdaacd065a45d1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9292,6 +9292,10 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
       "    aaaaaaaaaaaaaaaa);",
       Style);
+  verifyFormat(
+      "fooooooooooo(new BARRRRRRRRR(\n"
+      "    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
+      Style);
 
   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
   Style.BinPackArguments = false;

@HazardyKnusperkeks
Copy link
Contributor

How does it look like without the new?

@kadircet
Copy link
Member Author

$ cat a.cc
fooooooooooo(BARRRRRRRRRRRRR(
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));
$ clang-format a.cc
fooooooooooo(BARRRRRRRRRRRRR(
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));

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 llvm#105133.
@kadircet kadircet force-pushed the treat_new_as_simple branch from d0f1e70 to 089699a Compare August 23, 2024 13:29
@kadircet kadircet requested a review from owenca August 23, 2024 13:29
@kadircet kadircet merged commit e439fdf into llvm:main Aug 24, 2024
8 checks passed
5chmidti pushed a commit that referenced this pull request Aug 24, 2024
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.
dmpolukhin pushed a commit to dmpolukhin/llvm-project that referenced this pull request Sep 2, 2024
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 llvm#105133.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format over breaking near "new" expressions
5 participants