void testOneFunction( std::string funcParamName, vector<string> argvList, bool debug, int nrOfNodes, multimap <string, int> results, multimap <string, int> useresults) { if (debug) cout <<"\n\n------------------------------------------\ntesting ... " << argvList[1] << endl; // Build the AST used by ROSE SgProject* project = frontend(argvList); // Call the Def-Use Analysis DFAnalysis* defuse = new DefUseAnalysis(project); int val = defuse->run(debug); if (debug) std::cout << "Analysis run is : " << (val ? "failure" : "success" ) << " " << val << std::endl; if (val==1) exit(1); if (debug==false) defuse->dfaToDOT(); //std::list<SgNode*> vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition); //std::list<SgNode*>::const_iterator i = vars.begin(); NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition); NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin(); for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); std::string name = func->class_name(); string funcName = func->get_declaration()->get_qualified_name().str(); int maxNodes = defuse->getDefSize(); int nodeNr = defuse->getIntForSgNode(func); if (nodeNr == -1) continue; //cout << " checking function : " << funcName << endl; if (funcName!=funcParamName) continue; if (debug) cout << "\n------------------------\nchecking for " << name << " -- " << funcName << " -- " << nodeNr << endl; if (maxNodes!=nrOfNodes) { cerr << " Error: Test should have " << nrOfNodes << " nodes. found: " << maxNodes << endl; abort(); } if (debug) cout << " Test has nodes: " << nrOfNodes << endl; if (debug) cout <<"\nChecking all definitions ... " << endl; // check nodes in multimap std::vector <std::pair < SgInitializedName*, SgNode*> > map = defuse->getDefMultiMapFor(func); if (map.size()>0) { std::vector < std::pair <SgInitializedName*, SgNode*> >::const_iterator j = map.begin(); unsigned int hit=0; SgNode* node = NULL; string name=""; for (;j!=map.end();++j) { SgInitializedName* in_node = j->first; node = j->second; name= in_node->get_qualified_name().str(); if (debug) cout << " ... checking : " << name << endl; multimap <string, int>::const_iterator k =results.begin(); for (;k!=results.end();++k) { string resName = k->first; int resNr = k->second; int tableNr = defuse->getIntForSgNode(node); if (name==resName) if (debug) cout << " ... defNr: " << resNr << " inTable: " << tableNr << endl; if (name==resName && tableNr==resNr) { hit++; if (debug) cout << " Hit " << hit << "/" << results.size() << " - (" << name << "," << resNr << ")" << endl; } } } if (hit!=results.size()) { cerr << " Error: No hit! ... DFA values of node " << nodeNr << " are not correct! " << endl; exit(1); } } else { if (results.size()!=0) { cerr << " Error: Test node " << defuse->getIntForSgNode(func) << " should have a multimap. " << endl; exit(1); } } if (debug) cout <<"\nChecking all uses ... " << endl; // check nodes in multimap map = defuse->getUseMultiMapFor(func); if (map.size()>0) { std::vector <std::pair <SgInitializedName*, SgNode*> >::const_iterator j = map.begin(); size_t hit=0; for (;j!=map.end();++j) { SgInitializedName* in_node = j->first; SgNode* node = j->second; string name= in_node->get_qualified_name().str(); if (debug) cout << " ... checking : " << name << endl; multimap <string, int>::const_iterator k =useresults.begin(); for (;k!=useresults.end();++k) { string resName = k->first; int resNr = k->second; int tableNr = defuse->getIntForSgNode(node); if (name==resName) if (debug) cout << " ... defNr: " << resNr << " inTable: " << tableNr << endl; if (name==resName && tableNr==resNr) { hit++; if (debug) cout << " Hit " << hit << "/" << useresults.size() << " - (" << name << "," << resNr << ")" << endl; } } } if (hit!=useresults.size()) { cerr << " Error: No hit! ... DFA values of node " << nrOfNodes << " are not correct! " << endl; exit(1); } } // if } if (debug) std::cout << "Analysis test is success." << std::endl; }
void testOneFunction( std::string funcParamName, vector<string> argvList, bool debug, int nrOfNodes, multimap <int, vector<string> > resultsIn, multimap <int, vector<string> > resultsOut) { cout << " \n\n------------------------------------------\nrunning (variable)... " << argvList[1] << endl; // Build the AST used by ROSE SgProject* project = frontend(argvList); // Call the Def-Use Analysis DFAnalysis* defuse = new DefUseAnalysis(project); int val = defuse->run(debug); if (debug) std::cerr << ">Analysis run is : " << (val ? "failure" : "success" ) << " " << val << std::endl; if (val==1) exit(1); if (debug==false) defuse->dfaToDOT(); LivenessAnalysis* liv = new LivenessAnalysis(debug,(DefUseAnalysis*)defuse); std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions; NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition); NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin(); bool abortme=false; int hitIn=0; int hitOut=0; for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); std::string name = func->class_name(); string funcName = func->get_declaration()->get_qualified_name().str(); if (debug) cerr << " .. running live analysis for func : " << funcName << endl; FilteredCFGNode <IsDFAFilter> rem_source = liv->run(func,abortme); if (abortme) break; if (funcName!=funcParamName) { if (debug) cerr << " .. skipping live analysis check for func : " << funcName << endl; continue; } if (rem_source.getNode()!=NULL) dfaFunctions.push_back(rem_source); NodeQuerySynthesizedAttributeType nodes = NodeQuery::querySubTree(func, V_SgNode); // Edg3 mistakenly adds SgType nodes to the AST; Edg4 adds some also, but fewer. So we just remove them all. They // make no difference in the variable-liveness analysis anyway. nodes.erase(std::remove_if(nodes.begin(), nodes.end(), is_type_node), nodes.end()); SgFunctionDeclaration* decl = isSgFunctionDeclaration(func->get_declaration()); ROSE_ASSERT(decl); Rose_STL_Container<SgInitializedName*> args = decl->get_parameterList()->get_args(); if (debug) cerr <<"Found args : " << args.size() << endl; Rose_STL_Container<SgInitializedName*>::const_iterator it = args.begin(); for (;it!=args.end();++it) { nodes.push_back(*it); } if((int)nodes.size()-1!=nrOfNodes) { cerr << "Error :: Number of nodes = " << nodes.size()-1 << " should be : " << nrOfNodes << endl; exit(1); } else { if (debug) cerr << "Investigating nodes : " << nodes.size() << endl; } NodeQuerySynthesizedAttributeType::const_iterator nodesIt = nodes.begin(); for (; nodesIt!=nodes.end();++nodesIt) { SgNode* node = *nodesIt; ROSE_ASSERT(node); int tableNr = defuse->getIntForSgNode(node); std::vector<SgInitializedName*> in = liv->getIn(node); std::vector<SgInitializedName*> out = liv->getOut(node); std::vector<string> inName; std::vector<string> outName; std::vector<SgInitializedName*>::const_iterator itv = in.begin(); for (;itv!=in.end();++itv) { SgInitializedName* init = *itv; string name = init->get_name(); inName.push_back(name); } itv = out.begin(); for (;itv!=out.end();++itv) { SgInitializedName* init = *itv; string name = init->get_name(); outName.push_back(name); } std::sort(inName.begin(), inName.end()); std::sort(outName.begin(), outName.end()); multimap <int, vector<string> >::const_iterator k =resultsIn.begin(); for (;k!=resultsIn.end();++k) { int resNr = k->first; vector<string> results = k->second; if (debug) cerr << " ... containing nodes : " << results.size() << " node: " << node->class_name() << " tableNr : " << tableNr << " resNr : " << resNr << endl; if (tableNr==resNr) { std::sort(results.begin(), results.end()); if (results==inName) { if (debug) cerr <<"Contents in IN vector is correct! " << endl; } else { if (debug) { cerr << " >>>>>>>>>> Problem with contents for IN ! " << endl; cerr << " >>>>>>>>>> RESULT ... " << endl; } std::vector<string>::const_iterator itv = inName.begin(); for (;itv!=inName.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) { cerr << endl; cerr << " >>>>>>>>>> USER ... " << endl; } itv = results.begin(); for (;itv!=results.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) { cerr << endl; } exit(1); } if (results.size()==in.size()) { hitIn++; } if (debug) cout << " nodeNr: " << tableNr << ". ResultSize IN should be:" << results.size() << " - resultSize is: " << in.size() << " foundMatches == : " << hitIn << "/" << resultsIn.size() << endl; } } k =resultsOut.begin(); for (;k!=resultsOut.end();++k) { int resNr = k->first; vector<string> results = k->second; // cerr << " ... containing nodes : " << results.size() << " tableNr : " << tableNr << // " resNr : " << resNr << endl; if (tableNr==resNr) { std::sort(results.begin(), results.end()); if (results==outName) { if (debug) cerr <<"Contents in OUT vector is correct! " << endl; } else { if (debug) { cerr << " >>>>>>>>>> Problem with contents for OUT ! " << endl; cerr << " >>>>>>>>>> RESULT ... " << endl; } std::vector<string>::const_iterator itv = outName.begin(); for (;itv!=outName.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) { cerr << endl; cerr << " >>>>>>>>>> USER ... " << endl; } itv = results.begin(); for (;itv!=results.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) cerr << endl; exit(1); } if (results.size()==out.size()) { hitOut++; } if (debug) cout << " nodeNr: " << tableNr << ". ResultSize OUT should be:" << results.size() << " - resultSize is: " << out.size() << " foundMatches == : " << hitOut << "/" << resultsOut.size() << endl; } } if (hitIn==0 && hitOut==0) { if (debug) cout << " nodeNr: " << tableNr << " IN: " << hitIn << "/" << resultsIn.size() << " Out:" << hitOut << "/" << resultsOut.size() << endl; } } if (hitIn!=(int)resultsIn.size() || hitOut!=(int)resultsOut.size()) { cout << " Error: No hit! ... DFA values of node " << nrOfNodes << " are not correct! " << endl; exit(1); } } if (debug) cerr << "Writing out to var.dot... " << endl; std::ofstream f2("var.dot"); dfaToDot(f2, string("var"), dfaFunctions, (DefUseAnalysis*)defuse, liv); f2.close(); if (abortme) { cerr<<"ABORTING ." << endl; ROSE_ASSERT(false); // exit(1); } // iterate again and write second var.dot file i = vars.begin(); abortme=false; std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions2; for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); std::string name = func->class_name(); string funcName = func->get_declaration()->get_qualified_name().str(); if (debug) cerr << " .. running live analysis for func (fixupStatementsINOUT): " << funcName << endl; FilteredCFGNode <IsDFAFilter> rem_source = liv->run(func,abortme); liv->fixupStatementsINOUT(func); if (rem_source.getNode()!=NULL) dfaFunctions2.push_back(rem_source); } if (debug) cerr << "Writing out to varFix.dot... " << endl; std::ofstream f3("varFix.dot"); dfaToDot(f3, string("varFix"), dfaFunctions2, (DefUseAnalysis*)defuse, liv); f3.close(); if (debug) std::cout << "Analysis test is success." << std::endl; }