This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Move function body to out-of-line: unnamed class method incorrect moving
ClosedPublic

Authored by denis-fatkulin on Feb 9 2023, 3:48 AM.

Details

Summary

The refactoring Move function body to out-of-line produces incorrect code for methods of unnamed classes.
For this simple example

// foo.h
struct Foo {
  struct {
    void f^oo() {}
  } Bar;
};

the refactoring generates code:

// foo.cpp
void Foo::(unnamed struct at D:\test\foo.h:2:3)foo() {}

Outplace definition for methods of unnamed classes is meaningless. The patch disables it.

Diff Detail

Event Timeline

denis-fatkulin created this revision.Feb 9 2023, 3:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 9 2023, 3:48 AM
Herald added a subscriber: arphaman. · View Herald Transcript
denis-fatkulin requested review of this revision.Feb 9 2023, 3:48 AM
kadircet added inline comments.Feb 9 2023, 6:33 AM
clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
395

could you move this comment closer to the isTempated if statement below?

401

not just classes, but also for cases with unnamed namespaces.

could you change this to look like:

for(DeclContext *DC = MD->getParent(); DC; DC = DC->getParent()) {
  if (auto *ND = llvm::dyn_cast<NamedDecl>(DC)) {
     if(ND->getDeclName().isEmpty())
        return false;
  }
}
clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
97

can you also have a test inside an unnamed namespace?

denis-fatkulin marked 3 inline comments as done.Feb 10 2023, 1:30 AM
denis-fatkulin added inline comments.
clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
395

Fixed

401

It's a useful remark. Tanks!

clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
97

Test case added.

kadircet accepted this revision.Feb 10 2023, 5:11 AM
kadircet added inline comments.
clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
89

nit: i think you can group all these 3 tests.

// Not available if any of the parent context is unnamed, as we can't spell it.
EXPECT_UNAVAILABLE(R"cpp(
struct Foo {
  struct { 
     void b^ar() {}
     struct Baz { void b^ar() {} };
  } Bar;
};
namespace {
  struct Foo { void f^oo() {} };
}
)cpp");
This revision is now accepted and ready to land.Feb 10 2023, 5:11 AM
This revision was landed with ongoing or failed builds.Feb 10 2023, 6:40 AM
This revision was automatically updated to reflect the committed changes.
denis-fatkulin marked 3 inline comments as done.