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

Denis Steckelmacher steckdenis at yahoo.fr
Mon May 2 10:18:43 PDT 2011


Le lundi 2 mai 2011 17:54:12 Anton Lokhmotov a écrit :
> 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.

Hello,

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.




More information about the cfe-dev mailing list