/********************************************************** * get the InitName for a sgNode *********************************************************/ std::string DefUseAnalysis::getInitName(SgNode* sgNode){ SgInitializedName* initName = NULL; string name = "none"; if (isSgVarRefExp(sgNode)) { SgVarRefExp* varRefExp = isSgVarRefExp(sgNode); initName = varRefExp->get_symbol()->get_declaration(); name = initName->get_qualified_name().str(); } else if (isSgInitializedName(sgNode)) { initName =isSgInitializedName(sgNode); name = initName->get_qualified_name().str(); } else { name = sgNode->class_name(); } return name; }
void DefUseAnalysis::printMultiMap(const multitype* multi) { cout<<"\tmultitype element count:" << multi->size() <<endl; for (multitype::const_iterator j = multi->begin(); j != multi->end(); ++j) { SgInitializedName* sgInitMM = (*j).first; SgNode* sgNodeMM = (*j).second; ROSE_ASSERT(sgInitMM); ROSE_ASSERT(sgNodeMM); cout <<"\t" <<sgInitMM->class_name()<<" "<<sgInitMM << " " << sgInitMM->get_qualified_name().str() << " id ( " << ToString(getIntForSgNode(sgInitMM)) <<" ) - "; if (sgInitMM!=sgNodeMM) cout <<sgNodeMM->class_name()<<" "<<sgNodeMM << " id ( " << ToString(getIntForSgNode(sgNodeMM)) <<" ) "<< endl; else cout << "same self node" <<endl; } }
void runCurrentFile(vector<string> &argvList, bool debug, bool debug_map) { // Build the AST used by ROSE if (debug) std::cout << ">>>> Starting ROSE frontend ... " << endl; SgProject* project = frontend(argvList); if (debug) std::cout << ">>>> generate PDF " << endl; generatePDF ( *project ); if (debug) std::cout << ">>>> start def-use analysis ... " << endl; // Call the Def-Use Analysis DFAnalysis* defuse = new DefUseAnalysis(project); int val = defuse->run(debug); if (debug) std::cout << "Analysis is : " << (val ? "failure" : "success" ) << " " << val <<std::endl; if (val==1) exit(1); if (debug==false) defuse->dfaToDOT(); //example usage // testing NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgInitializedName); NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin(); for (; i!=vars.end();++i) { SgInitializedName* initName = isSgInitializedName(*i); std::string name = initName->get_qualified_name().str(); vector<SgNode* > vec = defuse->getDefFor(initName, initName); if (vec.size()>0) if (debug) std::cout << " DEF>> Vector entries for " << name << " ( " << initName << " ) : " << vec.size() << std::endl; } // testing vars = NodeQuery::querySubTree(project, V_SgReturnStmt); i = vars.begin(); typedef std::vector <std::pair <SgInitializedName*, SgNode* > > maptype; for (; i!=vars.end();++i) { SgReturnStmt* ret = isSgReturnStmt(*i); ROSE_ASSERT(ret); maptype map = defuse->getUseMultiMapFor(ret); maptype::const_iterator j; j = map.begin(); for (; j!=map.end();++j) { SgInitializedName* initName = isSgInitializedName(j->first); ROSE_ASSERT(initName); std::string name = initName->get_qualified_name().str(); vector<SgNode* > vec = defuse->getUseFor(ret, initName); if (vec.size()>0) std::cout << " USE>> Vector entries for " << name << " ( " << ret << " ) : " << vec.size() << std::endl; } } // print resulting table if (debug_map) { cout << "\nDEFMAP" << endl; defuse->printDefMap(); cout << "\nUSEMAP" << endl; defuse->printUseMap(); } delete project; delete defuse; }