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.