[cfe-dev] Parse custom options using CommonOptionsParser

Christoph Viebig (lists) lists at christoph-viebig.de
Wed Jan 21 02:11:35 PST 2015


Hello list,

I am currently working on a libTooling project and need to read custom
command line options for this.

Unfortunately I was not able to get the following sample code from
CommonOptionsParser [1] to run. Can you please give me an advice what
might be the problem here?

To make the sample code compiling I had to add the import of Tooling.h
and modify the Tool.run() call to retrieve the Front-end action via
.get() from the factory first. Also the category needs to be applied
on the new option, otherwise it's not shown in the help.

#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"

using namespace clang::tooling;
using namespace llvm;

static cl::OptionCategory MyToolCategory("My tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
static cl::opt<bool> YourOwnOption("abc", cl::cat(MyToolCategory));

int main(int argc, const char **argv) {
  CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  ClangTool Tool(OptionsParser.getCompilations(),
  OptionsParser.getSourcePathList());
  llvm::outs() << YourOwnOption.getValue();
  return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}

Thank you very much in advance!

Best regards
Christoph

[1]
http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1CommonOptionsParser.html



More information about the cfe-dev mailing list