How to Provide C Source Code Files as Input to RecursiveASTVisitor based CLANG Tool


I was able to setup and execute the sample provided on

The sample tool can be executed using the code as a string argument.

Wanted to know if there is a way we can provide the C-source code files as an argument to this main function (in FindClassDecls.cpp).

Disclaimer: I have just started on Clang.


Sunny. R. Billava

Sr. TechLead - AESBU | Powertrain Practice

Phone: +91-20-6652 5000 Ext. 2813 | Cell: +91-9561 125 413 | E-mail:

Hi Sunny,

you can do as follows:

static cl::OptionCategory MySampleCategory(“clang-tool options”);

int main(int argc, const char *argv[])
CommonOptionsParser OptionsParser(argc, argv, MySampleCategory);
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());


Take a look at Matchers as well. It might be more handy for you:

Of course, you could also just read the specified files into a string variable and pass it to runToolOnCode:

std::string FileContent;
// fill it from the files specified in argv

runToolOnCode(new FindNamedClassAction, FileContent);