From: Sven Verdoolaege Date: Sun, 13 Nov 2011 14:39:55 +0000 (+0100) Subject: use the clang driver to obtain command line arguments for the clang frontend X-Git-Tag: isl-0.09~35 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=518637043dd40811cce0b0803cb303698e2a9062;p=platform%2Fupstream%2Fisl.git use the clang driver to obtain command line arguments for the clang frontend Recent clang perform the Linux header searching in the driver instead of the fronted. We therefore need to call the driver to obtain the system include paths and pass them along to the frontend. Helped-by: Chandler Carruth Signed-off-by: Sven Verdoolaege --- diff --git a/configure.ac b/configure.ac index e16a0fb..f5aaaa1 100644 --- a/configure.ac +++ b/configure.ac @@ -136,6 +136,12 @@ system) [AC_DEFINE([DiagnosticsEngine], [Diagnostic], [Define to Diagnostic for older versions of clang])]) + AC_EGREP_HEADER([ArrayRef], [clang/Driver/Driver.h], + [AC_DEFINE([USE_ARRAYREF], [], + [Define if Driver::BuildCompilation takes ArrayRef])]) + AC_EGREP_HEADER([CXXIsProduction], [clang/Driver/Driver.h], + [AC_DEFINE([HAVE_CXXISPRODUCTION], [], + [Define if Driver constructor takes CXXIsProduction argument])]) AC_LANG_POP CPPFLAGS="$SAVE_CPPFLAGS" ;; diff --git a/interface/extract_interface.cc b/interface/extract_interface.cc index 89baf81..006a3bf 100644 --- a/interface/extract_interface.cc +++ b/interface/extract_interface.cc @@ -44,6 +44,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -60,6 +63,7 @@ using namespace std; using namespace clang; +using namespace clang::driver; static llvm::cl::opt InputFilename(llvm::cl::Positional, llvm::cl::Required, llvm::cl::desc("")); @@ -127,6 +131,62 @@ struct MyASTConsumer : public ASTConsumer { } }; +#ifdef USE_ARRAYREF + +#ifdef HAVE_CXXISPRODUCTION +static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags) +{ + return new Driver(binary, llvm::sys::getDefaultTargetTriple(), + "", false, false, Diags); +} +#else +static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags) +{ + return new Driver(binary, llvm::sys::getDefaultTargetTriple(), + "", false, Diags); +} +#endif + +/* Create a CompilerInvocation object that stores the command line + * arguments constructed by the driver. + * The arguments are mainly useful for setting up the system include + * paths on newer clangs and on some platforms. + */ +static CompilerInvocation *construct_invocation(const char *filename, + DiagnosticsEngine &Diags) +{ + const char *binary = CLANG_PREFIX"/bin/clang"; + const llvm::OwningPtr driver(construct_driver(binary, Diags)); + std::vector Argv; + Argv.push_back(binary); + Argv.push_back(filename); + const llvm::OwningPtr compilation( + driver->BuildCompilation(ArrayRef(Argv))); + JobList &Jobs = compilation->getJobs(); + + Command *cmd = cast(*Jobs.begin()); + if (strcmp(cmd->getCreator().getName(), "clang")) + return NULL; + + const ArgStringList *args = &cmd->getArguments(); + + CompilerInvocation *invocation = new CompilerInvocation; + CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1, + args->data() + args->size(), + Diags); + return invocation; +} + +#else + +static CompilerInvocation *construct_invocation(const char *filename, + DiagnosticsEngine &Diags) +{ + return NULL; +} + +#endif + int main(int argc, char *argv[]) { llvm::cl::ParseCommandLineOptions(argc, argv); @@ -137,6 +197,10 @@ int main(int argc, char *argv[]) new TextDiagnosticPrinter(llvm::errs(), DO)); DiagnosticsEngine &Diags = Clang->getDiagnostics(); Diags.setSuppressSystemWarnings(true); + CompilerInvocation *invocation = + construct_invocation(InputFilename.c_str(), Diags); + if (invocation) + Clang->setInvocation(invocation); Clang->createFileManager(); Clang->createSourceManager(Clang->getFileManager()); TargetOptions TO;