<eof> parser at end of file

I don’t know what inst is in your second example, nor where it came from. Why do you think you need an instruction, anyway? You have a Function, so get its users; a Function is a Value just like a Constant or an Instruction is.

Many thanks for your reply. I found the solution, but there is something I don’t understand. Here is my code to get the user function,

                        for(auto ui=F.use_begin();ui!=F.use_end();ui++){
                                errs()<<F.getName()<<" is called from this callInst: "<<*ui->getUser()<<"\n";
                                //errs()<<"Parent: "<<ui->getUser()->getParent()<<"\n";
                                //Instruction *I = *ui->getUser();
                                if(auto I = dyn_cast<Instruction>(ui->getUser())){
                                        errs()<<"Instruction: "<<*I<<"\n";
                                        errs()<<"Instruction Parent: "<<*I->getParent()<<"\n";
                                        auto *BB = I->getParent();
                                        Function* userFunction = BB->getParent();
                                        //errs()<<"User function: "<<*BB->getParent()<<"\n";

                                        errs()<<"User Function: "<<userFunction->getName()<<"\n";
                                        BasicBlock::iterator IP = BB->getFirstInsertionPt();
                                        errs()<<"first inst pointer: "<<*IP<<"\n";
                                        IRBuilder<> builder(&(*IP));

                                        std::vector<std::string> user;
                                        std::string format("\nUser: ");
                                        std::string s;
                                        raw_string_ostream rso(s);
                                        rso << userFunction->getName()<<" is calling "<<F.getName()<<"\n";
                                        rso << F.getName() << " is called from this callInst"<<*ui->getUser()<<"\n";
                                        user.push_back(rso.str());

                                        for (size_t i = 0; i < user.size(); ++i) {
                                                format += " %s\n";
                                        }
                                        Value *str = builder.CreateGlobalStringPtr(format, "");

                                        std::vector<Value *> argsV({str});

                                        for (auto &s : user) {
                                                argsV.push_back(builder.CreateGlobalStringPtr(s, ""));
                                        }
                                        builder.CreateCall(printfFunc, argsV, "calltmp");
                                }
                        }
  1. I am using use_begin and use_end to iterate over the uses of the function,
 for(auto ui=F.use_begin();ui!=F.use_end();ui++){
     ...
 }

In llvm use class there is no use_begin and use_end LLVM use. But it is present in llvm value As you can see previously I was trying to use user_begin() and user_end(). This is the most important function that I need to get the user. I just wanted to know how do I get the idea to use this function instead of user_begin. In this case, I was getting the use value from the function. But in most cases, how do I find it?

  1. getUser() is giving me the call instruction, and then I am getting the parent of this instruction, which is the basic block. Then I got the parent of this basic block, which is the function. What if this getParent() returns NULL and the basic block or the function is NULL? How do I check it for Basic Block? Though it seems that if my code passes the following code, then both the basic block and function are not null and my project code runs fine.
              errs()<<"Instruction: "<<*I<<"\n";
              errs()<<"Instruction Parent: "<<*I->getParent()<<"\n";
              auto *BB = I->getParent();
              Function* userFunction = BB->getParent();

By the way, it felt really good to find the solution. Following is a simple explanation with a demo code,
While compiling,

While running the demo binary,

In this simple example, I am calling a simple function subtract from the main and I am able to see who is calling this function with it’s arguments and values.

Using this code, I am getting a segmentation fault while retrieving the values. Following pass code is trying to access some values that it was not suppose to access in this file on this line,

 for (auto &F:M){
                std::vector<std::string> arg_strings;
                std::vector<Value*> arg_values;
                std::string s;
                raw_string_ostream rso(s);
                rso << F.getName() << " ";
                arg_strings.push_back(rso.str());
                for(auto i = F.arg_begin();i!=F.arg_end();++i){
                        //errs()<<"\narg_strings: "<<*i<< ", name = " << i->getName() <<"\n";
                        //std::string s;
                        //raw_string_ostream rso(s);
                        rso << *i <<"\n";
                        arg_strings.push_back(rso.str());
                        arg_values.push_back(i);
                }
                if(F.getName().contains("message_arrived") || F.getName().contains("publish") || F.getName().contains("requestOrder")){
                     if(!F.isDeclaration()){
                          {
                                        std::string format("arg_values: ");
                                        for (size_t i = 0; i < arg_values.size(); ++i) {
                                                format += " %s = %lu\n";
                                        }

                                        Value *str = builder.CreateGlobalStringPtr(format, "");
                                        std::vector<Value *> argsV({str});

                                        for (auto &v : arg_values) {
                                               // if(v != nullptr){
                                                        argsV.push_back(builder.CreateGlobalStringPtr(v->getName(), ""));
                                                        argsV.push_back(v);
                                               // }
                                        }
                                        builder.CreateCall(printfFunc, argsV, "calltmp");
                            }
                     }
          }
}

Here are the log error,

User:  main is calling _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l
_ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l is called from this callInst  invoke void @_ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l(%"class.ft::TxtMqttFactoryClient"* noundef nonnull align 4 dereferenceable(604) %788, %"class.std::__cxx11::basic_string"* noundef %59, double noundef %786, %"class.std::__cxx11::basic_string"* noundef %61, %"class.std::__cxx11::basic_string"* noundef %62, %"class.std::__cxx11::basic_string"* noundef %64, i32 noundef 60000)
          to label %803 unwind label %844, !dbg !29088


arguments:  _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l 
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0%"class.std::__cxx11::basic_string"* %1
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0%"class.std::__cxx11::basic_string"* %1double %2
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0%"class.std::__cxx11::basic_string"* %1double %2%"class.std::__cxx11::basic_string"* %3
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0%"class.std::__cxx11::basic_string"* %1double %2%"class.std::__cxx11::basic_string"* %3%"class.std::__cxx11::basic_string"* %4
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0%"class.std::__cxx11::basic_string"* %1double %2%"class.std::__cxx11::basic_string"* %3%"class.std::__cxx11::basic_string"* %4%"class.std::__cxx11::basic_string"* %5
 _ZN2ft20TxtMqttFactoryClient23publishStationBroadcastENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdS6_S6_S6_l %"class.ft::TxtMqttFactoryClient"* %0%"class.std::__cxx11::basic_string"* %1double %2%"class.std::__cxx11::basic_string"* %3%"class.std::__cxx11::basic_string"* %4%"class.std::__cxx11::basic_string"* %5i32 %6
arg_values:   = 3204445520
  = 3204444448
  = 3889515023
Segmentation fault

I am pretty sure this is happening because of the accessing the values that doesn’t exit. But don’t know how to skip those. I tried to ignore accessing the values by checking if the value is null but didn’t work. As the arg_values is returning a list of arguments, I can’t check if the whole list is null (As there can be some values for an argument) and also can’t check the values while iterating. Then how can I skip those nullable values jrtc27?