Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "llvm/Bitcode/ReaderWriter.h"
- #include "llvm/IR/LLVMContext.h" // added to fix LLVMContext problem
- #include "llvm/IR/Function.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;
- // use of cl namespace (command line) for setting
- // a variable we need
- static cl::opt<std::string> FileName(cl::Positional, cl::desc("Bitcode file"), cl::Required);
- int
- main(int argc, char **argv)
- {
- // Parse the command line
- cl::ParseCommandLineOptions(argc, argv, "LLVM Hello World\n");
- LLVMContext context;
- // obtain the file to a MemoryBuffer
- ErrorOr<std::unique_ptr<MemoryBuffer>> mb = MemoryBuffer::getFile(FileName);
- if (std::error_code ec = mb.getError())
- {
- errs() << ec.message();
- return -1;
- }
- // get the memory buffer and parse its bitcode (the compiled IR).
- ErrorOr<Module *> m = parseBitcodeFile(mb->get(), context);
- if (std::error_code ec = m.getError())
- {
- errs() << "Error reading bitcode: " << ec.message() << "\n";
- return -1;
- }
- // The module now contain all the functions
- // from the code, and from each function
- // we can extract name and basic blocks
- for (Module::const_iterator I = (*m)->getFunctionList().begin(),
- E = (*m)->getFunctionList().end(); I != E; ++I)
- {
- outs() << "Function: " << I->getName() << "(";
- for (Function::const_arg_iterator arg = I->getArgumentList().begin(),
- arg_end = I->getArgumentList().end(); arg != arg_end; ++arg)
- {
- arg->getType()->print(outs());
- outs() << " %" << arg->getName();
- if (arg->getNextNode() != arg_end)
- outs() << ",";
- }
- outs() << ")";
- if (!I->isDeclaration())
- // if it's an internal declaration LLVM
- // has analyzed how many basic blocks contain
- {
- outs() << " has " << I->size() << " basic blocks.\n";
- }
- // in other case is an external function
- else
- {
- outs() << " is used but it's external to this code.\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement