Search code examples
c++llvmbitcode

Extracting LLVM::Module from LLVM::ModuleRef


I'm trying to build a simple bitcode reader (rather than a dedicated pass, in order to be able to debug more easily) and I have some problems extracting the module. Here's what I have inside main:

LLVMModuleRef module;    
char *message = nullptr;
LLVMMemoryBufferRef memoryBuffer;

LLVMCreateMemoryBufferWithContentsOfFile(
    argv[1],
    &memoryBuffer,
    &message);

LLVMParseBitcode2(memoryBuffer,&module);

// for (auto func:module->getFunctionList())
{
    /* ... */
}

How can I extract Module from LLVMModuleRef? Surely I'm missing something trivial here.


Solution

  • Why are you mixing C and C++ APIs?

    If you want to work with llvm::Module, I assume you are coding in C++, so just use C++ API to parse the bitcode:

        #include "llvm/IRReader/IRReader.h"
    
        SMDiagnostic Err;
        LLVMContext ctx;
        unique_ptr<Module> M = parseIRFile(path, Err, ctx);
    
        if (!M) {
            Err.print("Error loading bitcode", errs());
        }