How to run FrontendAction over multiply TU

How can we run ClangTool based AST action over multiply source files.

For example command line tool:

./loop-convert --extra-arg-before=-I’/media/sf_share/llvm_9/llvm9-build/lib/clang/9.0.0/include’
-p ‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0’
‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0/libbb/rtc.c’ ‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0/libbb/ubi.c’ ‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0/libbb/dump.c’ ‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0/libbb/loop.c’ ‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0/libbb/mtab.c’ ‘/home/fvmu/scnd_disk/my_projects/for_code_data_divider/busybox-1.32.0/libbb/read.c’

and code (taken from

CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
LoopPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(LoopMatcher, &Printer);

I can iterate over the compilation database entries inside my program or write python wrapper. But I beleive there is an easier way.

newFrontendActionFactory(&Finder).get() works only for last source file in list (libbb/read.c). So how can I match AST nodes over multiply TU in one clang tool?


Check out this class:, and its usages. Might be the one you’re looking for. It takes care of going over every TU. And it’s also doing it multithreaded!

samins KAlex via cfe-dev <> ezt írta (időpont: 2020. júl. 26., V, 10:23):

  • cfe-dev@

Thank you. I found that passing many files (144) to ClangTool-based app causing it to process only last file. But when I pass 5-10 source files it works well as expected. The exact amount of files that does not cause problem I’m going to determine. I use clang 9.0.0.