Hi, I am working on a profiler project but whenever I call my report function, I get a segmentation fault. I debugged using gdb and it says my problem is somewhere within the second while function. I attached the code I'm trying.
void AST::mainReport(std::vector<std::string>& profileName) {
//Find the main - function with name of "main"
//Then start from the end() of this function and iterate
// backwards until you find a return stmt. You'll want
// to insert the report statements before this return.
// You can assume there is only one return for the main.
std::list<AST*>::iterator it = child.begin();
while(it != child.end()) {
if((*it)->tag == "function") {
AST*blockptr = (*it)->getChild("block");
std::list<AST*>::iterator blockIt = blockptr->child.begin();
while((*blockIt)->tag != "return") {++blockIt;}
--blockIt;
AST*output = new AST(token);
std::string outputCode;
for(std::vector<std::string>::iterator i = profileName.begin(); i !=profileName.end(); ++i) {
outputCode = outputCode + ("std::cout<< " + *i + " <<std::endl; \n");
}
output->text = outputCode;
blockptr->child.insert(blockIt,output);
}
++it;
}
}
> std::list<AST*>::iterator blockIt = blockptr->child.begin();
> while((*blockIt)->tag != "return") {++blockIt;}
> --blockIt;
You're assuming that your magic tag will ALWAYS be found.
You need a blockIt != blockptr->child.end() test, and a plan to deal with that eventuality.
> I debugged using gdb and it says my problem is somewhere within the second while function.
Yeah, and if you'd used the 'p' command to examine blockIt, you would have found out why.