Skip to content

Instantly share code, notes, and snippets.

@yiding
Created September 4, 2014 02:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yiding/172107cc7cfdd7142e6b to your computer and use it in GitHub Desktop.
Save yiding/172107cc7cfdd7142e6b to your computer and use it in GitHub Desktop.
Trivially parsing clang command line args with clang itself.
#include <llvm/Option/ArgList.h>
#include <llvm/Option/Arg.h>
#include <clang/Driver/Options.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Support/raw_ostream.h>
#include <memory>
using namespace llvm;
using namespace llvm::opt;
using namespace clang;
using namespace clang::driver;
int main(int argc, char *argv[]) {
std::unique_ptr<OptTable> OptTable(driver::createDriverOptTable());
unsigned int MissingArgIndex;
unsigned int MissingArgCount = 0;
std::unique_ptr<InputArgList> Args(
OptTable->ParseArgs(
&argv[1],
&argv[argc],
MissingArgIndex,
MissingArgCount,
0,
options::CLOption | options::NoDriverOption));
if (MissingArgCount > 0) {
errs() << "Argument missing values: " << argv[MissingArgIndex] << " " << MissingArgCount;
return 100;
}
for (auto it = Args->filtered_begin(options::OPT_UNKNOWN),
ie = Args->filtered_end(); it != ie; ++it) {
errs() << "Unknown argument: " << (*it)->getAsString(*Args) << "\n";
return 100;
}
for (Arg *arg : *Args) {
outs() << arg->getAsString(*Args) << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment