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

Сергеев Игнатий via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 24 08:35:16 PDT 2025


IgnatSergeev wrote:

@bartlettroscoe, 
> So is this really transforming the AST?

Well yeah, you are right, it replaces it with text, but what i ment, is that it supports replacements with EditGenerators, which are really expressive (a few words about them https://clang.llvm.org/docs/ClangTransformerTutorial.html#editgenerators-advanced)

But i would say that even clang-refactor uses source code transformations, it doesn`t change AST

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

The link you provided is to a test tool, but i see how you can adapt this to some tool
And speaking about "Extract Function", i searched through that file, and found only a rename symbol refactor

After a brief look, i think it uses source code transformations too
main -> calls rename (https://github.com/swiftlang/llvm-project/blob/next/clang/tools/clang-refactor-test/ClangRefactorTest.cpp#L520) -> calls clang_Refactoring_initiateAction and after uses it`s action (https://github.com/swiftlang/llvm-project/blob/next/clang/tools/libclang/CRefactor.cpp#L1384) -> creates RefactoringAction (https://github.com/swiftlang/llvm-project/blob/next/clang/tools/libclang/CRefactor.cpp#L750) that uses RefactoringOperation and RenamingAction for refactoring

RenamingAction is based on Refactoring library (the same that clang-refactor uses) and it use the same source code transformations (https://github.com/swiftlang/llvm-project/blob/next/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp)
And RefactoringOperation (https://github.com/swiftlang/llvm-project/blob/next/clang/include/clang/Tooling/Refactor/RefactoringOperation.h) uses RefactoringResult that as i see in comment also uses raw source code transformations
![image](https://github.com/user-attachments/assets/b2fabe88-d582-4cde-a071-1a6af7df24ce)

I hope my it makes sense
So at the end it is still a source code transformation, these functions don`t allow you to get transformed AST
But that was a brief look, i could be wrong

> LLNL ROSE Outliner tool

At first glance could be what you want

> 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"?

Maybe it`s possible, but i couldn`t invent something like that
At least not purely using transformer, it`s mostly used for local transformations, as there are much more of those

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

Actually, that`s an interesting idea

But to be honest, i think that there are`t many "users" for large refactoring tools, so when they are needed people write small portion of their own transformations, so they doesn`t get to open source


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


More information about the cfe-commits mailing list