[cfe-dev] Compile code in memory buffer to obj in memory.

Kim soldoutseashell at gmail.com
Sun Nov 3 18:46:38 PST 2013


Hey clang devs,

I'm looking to have multiple memory buffers containing my input code 
that are treated as individual compilation units (eg. as if each buffer 
was a cpp file). I looked into this and found I can have auxiliary names 
remapped to strings that contain my code, and this works for a single 
remapping but when I try to add more remapped names clang triggers an 
assertion:

Assertion failed: 0 && "Replacing with the same buffer", file 
..\..\..\..\..\too
ls\clang\lib\Basic\SourceManager.cpp, line 75

Remapping code (remapping_data is a vector of strings containing code, 
remapping_targets is another vector but with the names that were 
supplied as arguments, their indexes correspond to the mapping)

   clang::PreprocessorOptions & opts = 
Clang->getInvocation().getPreprocessorOpts();

   for(int i = 0; i < remapping_targets.size(); ++i )
   {
    // Create memory buffer with source text
    llvm::MemoryBuffer * buffer = llvm::MemoryBuffer::getMemBufferCopy 
(remapping_data[i]);
    if (buffer == NULL)
       error ("Failed to create memory buffer");

    // Remap auxiliary name from string vector remapping_targets to 
memory buffer
    opts.addRemappedFile (remapping_targets[i], buffer);
   }

   // Execute the frontend actions.
   Success = ExecuteCompilerInvocation(Clang.get());

As a secondary issue I also want to have the resulting object files from 
the compilation placed in memory rather than on disk. I haven't looked 
at this yet but I assume it should be pretty easy. Is there any built in 
functionality in clang for achieving this or will I need to modify the 
source itself?

Thanks.




More information about the cfe-dev mailing list