ClangTool and the usual commandline options

I’m writing a program that uses ClangTool to parrse C++ code; per the tutorials, the main module looks as below.

It works for a single file, but more complex inputs require the usual commandline options to set include path, Microsoft compatibility mode et cetera, and by default you only get a few options like -help and -version.

How do you get the usual set of commandline options that clang has?

// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static cl::OptionCategory MyToolCategory(“my-tool options”);

// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It’s nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);

int main(int argc, const char **argv) {
std::set_new_handler( {
errs() << "new: " << strerror(errno) << ‘\n’;
exit(1);
});

sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);

#ifdef _WIN32
// Stack overflow
AddVectoredExceptionHandler(0, handler);
#endif

CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory().get());
}

I’m writing a program that uses ClangTool to parrse C++ code; per the tutorials, the main module looks as below.

It works for a single file, but more complex inputs require the usual commandline options to set include path, Microsoft compatibility mode et cetera, and by default you only get a few options like -help and -version.

a) you use a compilation database (http://clang.llvm.org/docs/JSONCompilationDatabase.html), which is made to work well for multiple files with different sets of arguments
b) you call your tool with
./bin/my-tool path/to/file.cc –

That works, thanks!