This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Adjust braced list detection
ClosedPublic

Authored by cpplearner on Nov 25 2021, 4:43 AM.

Details

Summary

This avoids mishandling nested compound statements that are followed by another compound statement.

Fixes https://llvm.org/PR38314 and https://llvm.org/PR48305.

Diff Detail

Event Timeline

cpplearner requested review of this revision.Nov 25 2021, 4:43 AM
cpplearner created this revision.

Thanks for working on this.
First of all, the test seems ok for https://llvm.org/PR48305, but I'd like to see a test case for https://llvm.org/PR38314 (since you wrote in the description that this bug gets fixed as well).
And of course, we need to make the failing tests pass! :)

curdeius edited the summary of this revision. (Show Details)Nov 25 2021, 6:47 AM
HazardyKnusperkeks removed a reviewer: Restricted Project.Nov 25 2021, 7:40 AM
HazardyKnusperkeks added a project: Restricted Project.
MyDeveloperDay set the repository for this revision to rG LLVM Github Monorepo.
MyDeveloperDay added a project: Restricted Project.
MyDeveloperDay added a subscriber: cfe-commits.

FormatsBracedListsInColumnLayout is actually failing in its own test... it now produces

void foo() {
  { // asdf
    { int a; }
  }
  {
    { int b; }
  }
}

rather than previously

void foo() {
  {// asdf
   {int a;
}
}
{
  { int b; }
}
}

The other failure is

-class C extends {} {}
+class C extends {}
+{}

I'm slightly struggling to work out in both cases if its the test that is wrong @cpplearner what do you think?

For your test I think its your test that is wrong, looking at the simpler versions (using clang-format-12)

void foo() {
  {
    { int a; }
  }
}

void foo() {
  { int a; }
}
cpplearner updated this revision to Diff 389822.EditedNov 25 2021, 9:23 AM

Fixed tests.

Since in JavaScript, the thing after extends must be an expression, and the thing after implements must be an object type, I decided to parse both by directly calling parseBracedList, instead of going through tryToParseBracedList.

MyDeveloperDay accepted this revision.Nov 25 2021, 9:38 AM

I ran the examples from https://bugs.llvm.org/show_bug.cgi?id=38314 and its so much better than the previous state!

Thanks for this patch, LGTM

namespace n {
void foo() {
    {
        {
            {
                statement();
                if (false) {
                }
            }
        }
        {}
    }
}
}  // namespace n

void foo() {
    {
        {
            statement();
            if (false) {
            }
        }
    }
    {}
}  // namespace n
This revision is now accepted and ready to land.Nov 25 2021, 9:38 AM
HazardyKnusperkeks requested changes to this revision.Nov 25 2021, 1:03 PM

That will be a pain in the ass to merge with my current changes, but looks good.

First of all, the test seems ok for https://llvm.org/PR48305, but I'd like to see a test case for https://llvm.org/PR38314 (since you wrote in the description that this bug gets fixed as well).

But I second that.

This revision now requires changes to proceed.Nov 25 2021, 1:03 PM

Thanks for the update.

(You forgot to add the context, but since it was already accepted it's okay.)

This revision is now accepted and ready to land.Dec 1 2021, 12:12 AM
curdeius accepted this revision.Dec 1 2021, 12:31 AM

Thanks! Do you need someone to land this on your behalf? If yes, don't forget to send here your name and email.

Yes, I need someone to commit it on my behalf. My name and email address: Tan S. B. <cpplearner@outlook.com>

owenpan accepted this revision.Dec 5 2021, 2:10 PM
This revision was landed with ongoing or failed builds.Dec 5 2021, 10:40 PM
This revision was automatically updated to reflect the committed changes.

It appears that this regressed the formatting of initializer lists in some cases:

# Before
% cat ~/test.cc
class A {
  A() : a{} {}

  A(int b) : b(b) {}

  A(int a, int b) : a(a), bs{{bs...}} {
    f();
  }

  int a, b;
};

# After
% build/bin/clang-format -style=google ~/test.cc
class A {
  A() : a {}
  {}

  A(int b) : b(b) {}

  A(int a, int b) : a(a), bs {
    { bs... }
  }
  { f(); }

  int a, b;
};

@cpplearner could you please take a look?

We might want to revert this until we get a solution to avoid flip flopping peoples formats, but this to me comes down to what we've been saying before about using the TT_ types to labels the types of () {} []

I think in hindsight adding l_brace here just too generic (we need to know if its a "BracedInitializer {" or just a scope "{"

(NextTok->isOneOf(tok::l_brace, tok::identifier)

This is having an impact on code in flang, I don't deny it might be better, but it may not be what is wanted.

-  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}
-  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} {}
+  explicit Expr(const Scalar<Result> &x) : u {
+    Constant<Result> { x }
+  }
+  {}
+  explicit Expr(Scalar<Result> &&x) : u {
+    Constant<Result> { std::move(x) }
+  }
+  {}