2

I try to compile the minimal example from here

#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>

using namespace llvm;
int main()
{
  LLVMContext context;
  SMDiagnostic error;
  Module *m = parseIRFile("hello.bc", error, context);

  if(m)
  {
    m->dump();
  } 

  return 0;
}

using

g++ myFile.cpp `llvm-config --cxxflags --ldflags --libs all --system-libs` -std=c++11 -ldl -lpthread

and get

error: cannot convert ‘std::unique_ptr’ to ‘llvm::Module*’ in initialization

All examples and the llvm source itself everywhere uses llvm::Module *; so why do I get this error?

Note I use: LLVMVersion=3.6.0svn LLVM_CONFIGTIME= Thu Dec 18 10:51:37 CET 2014 Is it a problem with the 3.6 trunk? Should I opt for 3.5 branch?

Thx Alex

Community
  • 1
  • 1
alex
  • 315
  • 1
  • 11
  • Btw.: is there a llvm users list or forum? I could not find one; it seems there is only the llvm-dev list which is not adequate for my sort of questions... – alex Jan 12 '15 at 13:00

1 Answers1

4

The issue is that parseIRFile gives you back a unique_ptr<Module> and there is no implicit conversion from unique_ptr<Module> to Module* (which is good!!). To fix, just use the correct type:

std::unique_ptr<Module> m = parseIRFile(..);
auto m = parseIRFile(..); // avoid all future type issues

Using unique_ptr for memory management is much smarter than using raw pointers - and this interface makes clear that you are responsible for ownership of m. This way, you don't have to remember to delete it.

If you really really want to use a raw pointer, just call release on the returned object so that it no longer owns it:

Module* m = parseIRFile(..).release();

I only present that for completeness though - really prefer to keep your object a unique_ptr.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Ok, thank you. I'm a bit irritated, because in the documentation there are everywhere Module*, Function* and so on. Should all these have to be replaced by std::unique_ptr? – alex Jan 12 '15 at 13:57
  • @alex Apparently. Believe me, we've all been there with the incorrect documentation... At least in this case, the compiler will clearly tell you where you're wrong. – Barry Jan 12 '15 at 14:32
  • llvm is huge and complex (from my point of view, I'm a bit out of c/c++ practice). Is there a place where llvm beginners can discuss? It seems there's no official list, I have several issues (e.g. (http://stackoverflow.com/questions/27863706/llvm-out-of-source-pass-build-loadable-modules-not-supported-on-linux)), and the resonance is low. – alex Jan 12 '15 at 14:50
  • @alex No clue - I don't know anything about LLVM. – Barry Jan 12 '15 at 14:59