[cfe-dev] cfe-dev Digest, Vol 46, Issue 91

Speziale Ettore speziale.ettore at gmail.com
Tue May 3 00:25:37 PDT 2011


Hi,

> Thank you very much, it seems very interesting and I think it will greatly 
> help me. The current Clover code uses clang::CompilerInstance, and this class 
> has a clang::CompilerInstance::getSourceManager() function that can be used to 
> add a memory buffer as source like you did.
> 
> Thanks,
> Denis Steckelmacher.

Here at Politecnico we are currently build directly a CompilerInstance
from the command line. This allows to add compiler options from the
OpenCL API or from the environment (EnvCompilerOpts, not standard, used
for debugging). I don't known if there are a better solution. However,
this is the code:

void Program::BuildCompilerInvocation(
  Device &Dev,
  llvm::StringRef UserOpts,
  clang::CompilerInvocation &Invocation) {

  std::istringstream ISS(EnvCompilerOpts + UserOpts.str());
  std::string Token;
  llvm::SmallVector<const char *, 16> Argv;

  while(ISS >> Token) {
    char *CurArg = new char[Token.size() + 1];

    std::strcpy(CurArg, Token.c_str());
    Argv.push_back(CurArg);
  }
  Argv.push_back("opencl-sources.cl");

  clang::CompilerInvocation::CreateFromArgs(Invocation,
                                            Argv.data(),
                                            Argv.data() + Argv.size(),
                                            *Diag);
  Invocation.setLangDefaults(clang::IK_OpenCL);

  clang::PreprocessorOptions &PreprocOpts = 
    Invocation.getPreprocessorOpts();
  PreprocOpts.addRemappedFile("opencl-sources.cl", &*Src);
  PreprocOpts.RetainRemappedFileBuffers = true;

  Dev.SetTargetOptions(Invocation.getTargetOpts());
}

I think this code was originally from Peter Collingbourne, but I don't
rembember the exact location where I have see it.

Best regards,
speziale.ettore at gmail.com




More information about the cfe-dev mailing list