Created
September 4, 2014 02:03
-
-
Save yiding/172107cc7cfdd7142e6b to your computer and use it in GitHub Desktop.
Trivially parsing clang command line args with clang itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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