[PATCH] D140619: ExtractFunction: support extracting expressions and selected part of it

Kacper Kowalski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 23 03:58:39 PST 2022


KKoovalsky created this revision.
Herald added subscribers: kadircet, arphaman, mgrang.
Herald added a project: All.
KKoovalsky requested review of this revision.
Herald added subscribers: cfe-commits, ilya-biryukov.
Herald added a project: clang-tools-extra.

This is mainly insipred by ExtractVariable's ParsedBinaryOperator,
tweaked a little to collect references within the selected area.

On the side:
This is my first PR to the LLVM (and clangd) - I wanted to see how it
goes for the future, to contribute even more, as I am a C++ fan and I
love open-source, plus recently, I really like to automate stuff when
coding!

Getting back to the implemented functionality:
it upports extraction of expressions to a function or method:

- int x{[[a + b + c]]},
- int x{a + [[b + c]] + d};

Handles:

- Binary operators.
- Operator overloads, which resemble binary operators, e.g. struct S{ S& operator+(const S&); }
- Infers return type of the expression basing on the operation return type; refs and const-refs are properly inferred as well.
- Collects parameters from the selected area. Skips global variables and members. (Uses already-in-place DeclInfoMap containing info about the declarations within the enclosing function).
- Handles deeply nested arguments within function calls, and collects them as the extracted function parameters.

Known limitations:

- Macros within the selected area; the selected area is treated as the expr would be selected to the most-LHS entity; effectively: int x{a + [[SOME_MACRO(A) + b]] + c} becomes: int x{[[a + SOME_MACRO(A) + b]] + c} This is known limitation of ExtractVariable, thus nothing new here.
- Does not make the method 'const' if the selected members are not mutated, in case the enclosing method is non-const.

PS. This contribution is a little messy, since it was hard to support
partially selected expressions, because the code already-in-place used
the term "root statement", which assumes that the entire statement is
selected. With this PR this assumption will be broken.
Yet, adding this feature taught me a lot, and I understand the code
structure better. I would like to boil down all the details of
implementing the Extract Function feature as a whole.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140619

Files:
  clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
  clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140619.485081.patch
Type: text/x-patch
Size: 43487 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221223/d8a887de/attachment-0001.bin>


More information about the cfe-commits mailing list