[cfe-dev] default include paths in the frontend

Chandler Carruth chandlerc at google.com
Sat Nov 12 01:49:09 PST 2011


On Sat, Nov 12, 2011 at 1:23 AM, Sven Verdoolaege <skimo-cfe at kotnet.org>wrote:

> Let's assume I want to avoid a subprocess.
> Maybe there is no need, but that's how I'm doing it now.
>

Entirely reasonable...

> I can sketch out the code to do this if it helps
>
> If it's not too much trouble for you, then I would appreciate
> such a sketch.  It may also help others.
>

It looks something like the following. I've editted this down heavily from
some real code we've used, but I've not even compiled it (as you can tell)
so its very pseudo-code-ish... lemme know if you need something more
concrete, but hopefully this is enough to get you started:

/// \brief Retrieves the clang CC1 specific flags out of the compilation's
jobs.
/// Returns NULL on error.
static const clang::driver::ArgStringList *GetCC1Arguments(
    clang::DiagnosticsEngine *Diagnostics,
    clang::driver::Compilation *Compilation) {
  // We expect to get back exactly one Command job, if we didn't something
  // failed. Extract that job from the Compilation.
  const clang::driver::JobList &Jobs = Compilation->getJobs();
  if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) {
    // diagnose this...
    return NULL;
  }

  // The one job we find should be to invoke clang again.
  const clang::driver::Command *Cmd =
cast<clang::driver::Command>(*Jobs.begin());
  if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
    // diagnose this...
    return NULL;
  }

  return &Cmd->getArguments();
}

const std::vector<char*> Argv = ...; // get this from somewhere...
const char *const BinaryName = Argv[0];
DiagnosticOptions DefaultDiagnosticOptions;
TextDiagnosticPrinter DiagnosticPrinter(
    llvm::errs(), DefaultDiagnosticOptions);
DiagnosticsEngine
Diagnostics(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>(
    new DiagnosticIDs()), &DiagnosticPrinter, false);

clang::driver::Driver Driver(..., Diagnostics);
const llvm::OwningPtr<clang::driver::Compilation> Compilation(
    Driver.BuildCompilation(llvm::ArrayRef<const char*>(
        &Argv[0], Argv.size() - 1)));
const clang::driver::ArgStringList *const CC1Args = GetCC1Arguments(
    &Diagnostics, Compilation.get());
if (CC1Args == NULL) {
  return false;
}
llvm::OwningPtr<clang::CompilerInvocation> Invocation(
    new clang::CompilerInvocation);
  clang::CompilerInvocation::CreateFromArgs(
      *Invocation, CC1Args->data() + 1, CC1Args->data() + CC1Args->size(),
      Diagnostics);
  Invocation->getFrontendOpts().DisableFree = false;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111112/9da8df35/attachment.html>


More information about the cfe-dev mailing list