[llvm-dev] Problem in taking llvm ir file via command line argument

Ratnesh Tiwari via llvm-dev llvm-dev at lists.llvm.org
Sat Aug 11 23:12:03 PDT 2018


Hi, I am writing a program here hello.cpp for taking LLVM IR as a command
line argument. My IR file is "input.bc" . But I dont understand how it make
it via command line argument.

I am running this hello.cpp as:
g++ hello.cpp  -I /tmp/llvm/include/ -std=c++11 input.bc

But it shows error. Please suggest me correct way of taking llvm ir file as
CLA. Here, is mine program:

----------
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

static cl::opt<std::string>
FileName(cl::Positional, cl::desc("Bitcode file"),
         cl::Required);

int main(int argc, char** argv)
{
  cl::ParseCommandLineOptions(argc, argv, "LLVM hello world\n");
  LLVMContext context;

  ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
MemoryBuffer::getFile(FileName);
  if (std::error_code ec = mb.getError()) {
    errs() << ec.message();
    return -1;
  }

//  ErrorOr<Module *> m = parseBitcodeFile(mb->get(), context);
//  if (std::error_code ec = m.getError()) {
  Expected<std::unique_ptr<Module>> m =
parseBitcodeFile(mb->get()->getMemBufferRef(), context);
        if (std::error_code ec = errorToErrorCode(m.takeError())) {
    errs() << "Error reading bitcode: " << ec.message() << "\n";
    return -1;
}

  for (Module::const_iterator I = (*m)->getFunctionList().begin(),
    E = (*m)->getFunctionList().end(); I != E; ++I) {
    if (!I->isDeclaration()) {
      outs() << I->getName() << " has " << I->size() << " basic blocks.\n";
    }
  }

  return 0;
}
--------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180812/98325dd2/attachment.html>


More information about the llvm-dev mailing list