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

Extract function can extract code containing types defined in the enclosing functions scope #1710

Open
5chmidti opened this issue Jul 22, 2023 · 1 comment

Comments

@5chmidti
Copy link

Clangd allows extracting a selection where a type is used that is defined in the scope of the enclosing function, making the type undefined in the extracted function.

void foo() {
      using MyType = int;
      [[MyType x = 42;
      MyType y = x;]]
}
void extracted() {
  MyType x = 42;
  MyType y = x;
}
void foo() {
  using MyType = int;
  extracted();
}

Clangd 17 at commit 852fe30b687e

I think this could be implemented by using a RecursiveASTVisitor to find all TypeDecls of the enclosing function and comparing those to the referenced Types within the selected region. If there exists a type that is declared in the enclosing function, but outside the selection, and that type is used within the selection, then don't extract.

@5chmidti 5chmidti changed the title Extract function can extract code that contains types that are defined in the enclosing functions scope Extract function can extract code containing types defined in the enclosing functions scope Jul 22, 2023
@5chmidti
Copy link
Author

The fix could address the FIXME comments in CapturedZoneInfo
https://github.com/llvm/llvm-project/blob/4c73549ebf171c1fa8039dc97e8fa260e824aa96/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp#L529-L530

  // FIXME: capture TypeAliasDecl and UsingDirectiveDecl
  // FIXME: Capture type information as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant