[cfe-dev] Question about writing a refactoring tool

Zachary Turner via cfe-dev cfe-dev at lists.llvm.org
Tue Dec 15 15:58:57 PST 2020


I'm interested in writing a simple refactor that will find all uses of a
symbol Foo and replace it with either true or false, then simplify the
conditional constructs in which they appeared.  For example, if I have some
code:

if (!Foo)
{
   // Do thing A
}
else
{
    // Do thing B
}

And I run the tool to replace Foo with True, I would expect my refactor to
transform the above block into:

// Do thing B

Similarly, if the code was:

if (Foo || Bar)
{
    // Do thing
}

and I replace Foo with true, I would expect it to transform the code into:

if (Bar)
{
    // Do thing
}


I've got a simple refactoring command integrated into clang-refactor, but
it currently doesn't do anything other than appear in the --help output.
 I've got a couple of questions:

1. What is the difference between clang-tidy and clang-refactor?
Conceptually I get it, clang-refactor is for "larger" stuff, but *why*?
What about clang-refactor makes it more suitable for large refactors?  I'm
already a little familiar with the ASTMatcher interface, and it seems like
clang-refactor doesn't make use of it, so using clang-refactor I'm sort of
at ground 0 with very few examples to go off of, whereas if I were using
clang-tidy I would be able to make faster progress because I'm already a
little familiar with it.

2. What does USR mean?  I see references to this acronym all over the place
in the implementation of the local-rename command, but it's not explained
anywhere.

3. Does the refactoring tooling in general contain sufficient information
for me be able to accomplish the specific refactor I'm looking into?  And
are there any other examples of refactors people have written besides
local-rename and extract?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20201215/d029d66c/attachment.html>


More information about the cfe-dev mailing list