From 044484d389819cbaefd8b12b12ff71227206e4e6 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Sat, 12 Nov 2011 18:10:44 +0100 Subject: [PATCH] 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 --- configure.ac | 6 ++++++ pet.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/configure.ac b/configure.ac index 6c47cd0..79a7ca7 100644 --- a/configure.ac +++ b/configure.ac @@ -48,6 +48,12 @@ AC_EGREP_HEADER([DiagnosticsEngine], [clang/Basic/Diagnostic.h], [Define to Diagnostic for newer versions of clang])], [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/pet.cc b/pet.cc index d8f9679..f1d116d 100644 --- a/pet.cc +++ b/pet.cc @@ -42,6 +42,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -72,6 +75,7 @@ using namespace std; using namespace clang; +using namespace clang::driver; /* Called if we found something we didn't expect in one of the pragmas. * We'll provide more informative warnings later. @@ -493,6 +497,62 @@ static void update_arrays(struct pet_scop *scop, } } +#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 + /* Extract a pet_scop from the C source file called "filename". * If "function" is not NULL, extract the pet_scop from the function * with that name. @@ -523,6 +583,9 @@ struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx, delete printer; DiagnosticsEngine &Diags = Clang->getDiagnostics(); Diags.setSuppressSystemWarnings(true); + CompilerInvocation *invocation = construct_invocation(filename, Diags); + if (invocation) + Clang->setInvocation(invocation); Clang->createFileManager(); Clang->createSourceManager(Clang->getFileManager()); TargetOptions TO; -- 2.11.4.GIT