cfe-dev Digest, Vol 46, Issue 91

Hi Denis,

Great to hear you are working on an open source OpenCL C compiler. ARM,
Intel and Apple are open-sourcing changes to Clang to support OpenCL C, so
you should be able to build on these in your work.

So I am asking for a way to avoid writing the source code given by the
application to a temporary file, that is then read by Clang.

If I understand you correctly, you refer to initialising the source manager
e.g. as:

    clang::FileManager file_mgr = ...;
    clang::SourceManager src_mgr = ...;

    const std::string tmp_file_path = ...;
    const clang::FileEntry * src_file = file_mgr.getFile(tmp_file_path);
        llvm.errs() << "Error: Failed to open file \'" << tmp_file_path <<

If, however, the source code is provided as a character string, e.g.

    // Note that 'length' can be > strlen('source'), i.e.
    // 'source' can contain '\0' at a position < 'length'.
    size_t length = ...;
    const char * source = ...;
    assert( NULL != source && 0 <= length );

you can do something liek this:

    // Wrapping 'source' in std::string solves the above
    // issue (at a cost of copying).
    const std::string source_string(source, length);

    const llvm::StringRef data(source_string);
    const llvm::StringRef name("<source>");

    llvm::MemoryBuffer * mem = llvm::MemoryBuffer::getMemBuffer(data, name);

Hope this helps.