Multi TU refactoring


I am trying to create a clang Tool using ASTMatchers which renames a particular function invocation. However, when it renames a call in a file which is included in multiple TUs (header files for example), the tool duplicates the replacement. Here is an example:

TU1: rename.cpp, header.h
rename.cpp: foo() → bar()
header.h: foo() → barbar()

TU2: rename2.cpp, header.h
rename2.cpp: foo() → bar()
header.h: foo() → barbar()

How do I deduplicate the replacements? I have tried to use merge() but that seems to have no effect.
PS. I am using Clang 10 for this.


I think the problem you have is similar to the one clang-tidy has. If you have 2 threads/processes that process the same header file, you’ll suggest the Sam change twice.

You can run your program single threaded/process. However clang-tidy also has the flag --export-fixes=<filename> with as documentation: YAML file to store suggested fixes in. The stored fixes can be applied to the input source code with clang-apply-replacements.

This way, the program can suggest duplicate fixes and only one of them gets applied.

1 Like

sorry for the late reply. I took your advice and used clang-tidy. However, I find it strange that in spite of clang having multi-TU refactoring headers and functions, I have not found a simple enough tool which deals with multi-TU refactoring without having to use yaml files for deduplication. Do you know of such a tool which is simple enough to understand? I have searched for days altogether but didn’t find any. One which comes close is HIPIFY, but I haven’t actually used it so can’t say it works for multi-TU setting.


Hey gPats,

I’m not an expert about this, though can’t you from your own tool call the same export module?