How do I run my Clang Tool on a CMake based build system?

Viewed 251

I am working on a tool, using clang's LibTooling library, and I want it to parse the entire build system.

Code I've been trying to call clang with:

int main(int argc, const char** argv) {
  clang::tooling::CommonOptionParser parser(argc, argv, OptionCategory);
  clang::tooling::ClangTool tool(parser.getCompilations(),
                                 parser.getSourcePathList());

  return tool.run(
      clang::tooling::newFrontendActionFactory<tlx::TemplateAction>().get());
}

The build system I want to run my tool on uses CMake, so I can easily export the compile_commands.json compilation database.

But I dont know how can I pass it to my tool.

I've tried: ./myTool <path>/compile_commands.json where I get an error skipping <path>/compile_commands.json. Compile command not found.

I also tried: ./myTool -p <path to build> <path to source>/main.cppwhich works but than I have to list every translation unit. The point of having a compilation database is that it contains every translation unit with the command that it was compiled with.

Is there a way to just simply pass the compile_commands.json as an argument to my tool and let the CommonOptionParser parse it?

0 Answers
Related