[cfe-dev] Proposal: add "replace" functionality to clang-query

Aaron Ballman via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 25 09:52:09 PDT 2020


On Tue, Mar 24, 2020 at 4:22 PM Alexander Timin via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
> Hey!
>
> I think that medium-scale medium-complexity C++ refactorings (like replacing the order of the two arguments of a function across a large codebase) are much harder that they should be: clang-refactor and clang-rename do not cover them and writing a full libTooling tool is not the most time-efficient solution.
>
> I propose extending the functionality of clang-query to cover this use case and allow generating replacements (which can be then applied by clang-apply-replacements) from matches:
>
> replace MATCHER NODE PATTERN
>
> for example,
>
> let f = cxxMethodDecl(hasName("Foo"))
> let arg1 = hasArgument(0, expr().bind("arg1"))
> let arg2 = hasArgument(1, expr().bind("arg2"))
> let m = callExpr(on(f), arg1, arg2)
> replace m root Foo(${arg2}, ${arg1})
>
> A more detailed design doc is available here.
>
> An early prototype patch is available here.
> A real-life example: the query is here, the output in the form of a Chromium patch is here.
>
> WDYT?

Thank you for bringing up this idea, I've wanted something that can do
this for a while. I think the idea has a lot of merit, but I'm
wondering whether we want to focus on code transformations within
clang-query like this as opposed to putting that effort into the
in-progress Transformer work
(http://lists.llvm.org/pipermail/cfe-dev/2019-January/060950.html).
Are you aware of those efforts? If so, how do you see this proposal
coexisting with that functionality?

~Aaron


> Alexander
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


More information about the cfe-dev mailing list