[clang] [clang-refactor] Add Matcher Edit refactoring rule (PR #123782)

Roscoe A. Bartlett via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 22 06:15:51 PDT 2025


=?utf-8?b?0JjQs9C90LDRgiDQodC10YDQsw=?=,
=?utf-8?b?0JjQs9C90LDRgiDQodC10YDQsw=?Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/123782 at github.com>


bartlettroscoe wrote:

@IgnatSergeev, thanks for the reply!

> You were right about the amount of refactorings that LibTool could support And actually AST transformations are already supported in clang-transformer tool and its library

So is this really transforming the AST?  According to:

* https://clang.llvm.org/docs/ClangTransformerTutorial.html#rewriting-asts-to-text

it says:

> The astute reader may have noticed that we’ve been somewhat vague in our explanation of what the rewrite rules are actually rewriting. We’ve referred to “code”, but code can be represented both as raw source text and as an abstract syntax tree. So, which one is it?
> 
> **Ideally, we’d be rewriting the input AST to a new AST, but clang’s AST is not terribly amenable to this kind of transformation.** So, we compromise: we express our patterns and the names that they bind in terms of the AST, **but our changes in terms of source code text**. We’ve designed Transformer’s language to bridge the gap between the two representations, in an attempt to minimize the user’s need to reason about source code locations and other, low-level syntactic details.

> so the clang-refactor tool is abandoned as i could tell Furthermore, transformer is the main tool (or actually library) for transformations in clang-tidy

Just an FYI, we have found that the Apple fork of the llvm-project repo appears to have some additional C++ refactoring tooling that is used by the XCode IDE:

* https://github.com/swiftlang/llvm-project/tree/next/clang/tools

For example, there appears to be an "Extract Function" refactor that might actually be able to refactor real C++ code the exectuable:

* https://github.com/swiftlang/llvm-project/blob/next/clang/tools/clang-refactor-test/ClangRefactorTest.cpp

We have not done an in-depth evaluation of this capability yet, but it looks promising.

> So i would recommend reading about them, and searching through their code, maybe you could find some of the refactorings already implemented for those tools And i could be wrong, but everyone mostly uses transformer to implement their own refactorings privately, as sad for open source as it may sound

Are people using clang-transformer for more than just [clang-tidy "FixIt" checks](https://clang.llvm.org/docs/ClangTransformerTutorial.html#using-a-rewriterule-as-a-clang-tidy-check)?  I have done some initial searches and I can't seem to find anything other than just clang-tidy FixIts.

The question is how his does this clang transformer mapping of AST to source code work for larger chucks of C++ code, like for "Extract Function"?

FYI: We are starting to look at the LLNL ROSE Outliner tool:

* https://github.com/rose-compiler/rose/wiki/Publications#effective-source-to-source-outlining-to-support-whole-program-empirical-optimization

which is supposed to be able to implement a robust C++ "Extract Function" refactoring.

Or, perhaps everyone is waiting for better AI to be able to refactor C++ automatically?


https://github.com/llvm/llvm-project/pull/123782


More information about the cfe-commits mailing list