<div dir="ltr">Thank you Alex. Yes, it works as expected.. <div><br></div><div>Regards,</div><div>Irene</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Dec 10, 2018 at 11:23 AM Alex Denisov <<a href="mailto:1101.debian@gmail.com">1101.debian@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">At a first glance your usage looks correct.<br>
Though, it seems like you are misusing the sizeof: I assume you want to see the number of functions in the module, while sizeof returns size of the iterator itself.<br>
Then, it seems there are no functions in the module, therefore you are dereferencing an iterator that points to invalid location, then your program terminates.<br>
<br>
One reason the module can be empty:<br>
<br>
Module *m = parseIRFile("t.ll", error, context).get();<br>
<br>
In this line, parseIRFile returns std::unique_ptr<Module>, but the result is not stored anywhere, the instance of unique_ptr deallocated right after the call to .get() at the end of the line.<br>
When you use the Module *m later on it already points to a deallocated memory.<br>
<br>
I hope it helps.<br>
<br>
Cheers,<br>
Alex.<br>
<br>
> On 10. Dec 2018, at 09:36, div code via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> <br>
> Any suggestions on this? I am sorry for the trouble this has caused, but the inconsistent APIs cause me a bunch of confusions..<br>
> <br>
> Best,<br>
> Irene<br>
> <br>
> On Sun, Dec 9, 2018 at 4:40 PM div code <<a href="mailto:divsubmission@gmail.com" target="_blank">divsubmission@gmail.com</a>> wrote:<br>
> Hello,<br>
> <br>
> I am a newbie to LLVM and right now I am on the hook to parse some IR code and do some instrumentations. However, my problem is that no matter how I tweak my parsing code, it simply cannot print out anything.<br>
> <br>
> So here is my C code:<br>
> <br>
> int your_fun(int arg2) {<br>
> int x = arg2;<br>
> return x+2;<br>
> }<br>
> <br>
> <br>
> And here is my parsing code:<br>
> #include <llvm/IR/Module.h><br>
> #include <llvm/IRReader/IRReader.h><br>
> #include <llvm/IR/LLVMContext.h><br>
> #include <llvm/Support/SourceMgr.h><br>
> #include <iostream><br>
> #include <llvm/Support/raw_ostream.h><br>
> <br>
> <br>
> using namespace llvm;<br>
> int main()<br>
> {<br>
> LLVMContext context;<br>
> SMDiagnostic error;<br>
> Module *m = parseIRFile("t.ll", error, context).get();<br>
> if(!m)<br>
> {<br>
> return 0;<br>
> }<br>
> std::cout << error.getMessage().str() << std::endl;<br>
> std::cout << sizeof(m->getFunctionList()) << std::endl;<br>
> auto iter1 = m->getFunctionList().begin();<br>
> std::cout << " Function: " << (*iter1).getName().str() << std::endl;<br>
> <br>
> for (auto iter1 = m->getFunctionList().begin();<br>
> iter1 != m->getFunctionList().end(); iter1++) {<br>
> Function &f = *iter1;<br>
> std::cout << " Function: " << std::endl;<br>
> std::cout << " Function: " << f.getName().str() << std::endl;<br>
> for (auto iter2 = f.getBasicBlockList().begin();<br>
> iter2 != f.getBasicBlockList().end(); iter2++) {<br>
> BasicBlock &bb = *iter2;<br>
> std::cout << " BasicBlock: " << bb.getName().str() << std::endl;<br>
> for (auto iter3 = bb.begin(); iter3 != bb.end(); iter3++) {<br>
> Instruction &inst = *iter3;<br>
> std::cout << " Instruction " << &inst << " : " << inst.getOpcodeName();<br>
> <br>
> unsigned int i = 0;<br>
> unsigned int opnt_cnt = inst.getNumOperands();<br>
> for(; i < opnt_cnt; ++i)<br>
> {<br>
> Value *opnd = inst.getOperand(i);<br>
> std::string o;<br>
> if (opnd->hasName()) {<br>
> o = opnd->getName();<br>
> std::cout << " " << o << "," ;<br>
> } else {<br>
> std::cout << " ptr" << opnd << ",";<br>
> }<br>
> }<br>
> std:: cout << std::endl;<br>
> }<br>
> }<br>
> }<br>
> return 1;<br>
> <br>
> }<br>
> <br>
> <br>
> And this is my output:<br>
> ➜ test git:(develop) ✗ clang -S -emit-llvm t.c<br>
> ➜ test git:(develop) ✗ clang++ parse.cpp -o reader `llvm-config --cxxflags --libs --ldflags --system-libs` -g<br>
> warning: unknown warning option '-Wno-maybe-uninitialized'; did you mean '-Wno-uninitialized'? [-Wunknown-warning-option]<br>
> 1 warning generated.<br>
> ➜ test git:(develop) ✗ ./reader<br>
> <br>
> 16<br>
> Function:<br>
> ➜ test git:(develop) ✗ clang --version<br>
> clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)<br>
> Target: x86_64-pc-linux-gnu<br>
> Thread model: posix<br>
> InstalledDir: /usr/bin<br>
> <br>
> <br>
> As you can see, I basically cannot get anything meaningful here. Could anyone shed some light on this? Thanks a lot.<br>
> <br>
> Irene<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
--<br>
AlexDenisov<br>
Software Engineer, <a href="https://lowlevelbits.org" rel="noreferrer" target="_blank">https://lowlevelbits.org</a><br>
<br>
</blockquote></div>