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

Anton Lokhmotov Anton.Lokhmotov at arm.com
Mon May 2 09:54:12 PDT 2011


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);
    if(!src_file)
    {
        llvm.errs() << "Error: Failed to open file \'" << tmp_file_path <<
"\'\n";
        exit(EXIT_FAILURE);
    }
    src_mgr.createMainFileID(src_file);

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);
    src_mgr.createMainFileIDForMemBuffer(mem);

Hope this helps.

Anton.







More information about the cfe-dev mailing list