NodeQuerySynthesizedAttributeType queryIsImportantForSliceTypeWithCalls(SgNode * astNode) { NodeQuerySynthesizedAttributeType retVal; if (IsImportantForSliceSgFilter(astNode) || isSgFunctionCallExp(astNode)) retVal.push_back(astNode); return retVal; }
NodeQuerySynthesizedAttributeType queryIsImportantForSliceType(SgNode * astNode) { NodeQuerySynthesizedAttributeType retVal; if (IsImportantForSliceSgFilter(astNode)) retVal.push_back(astNode); return retVal; }
NodeQuerySynthesizedAttributeType NodeQuery::queryNodeClassDeclarationFromName(SgNode* node, SgNode* nameNode){ NodeQuerySynthesizedAttributeType returnList; ROSE_ASSERT( nameNode != NULL ); ROSE_ASSERT( node != NULL ); //finds the name which should be matched to SgName* sageName = isSgName(nameNode); ROSE_ASSERT( sageName != NULL ); std::string nameToMatch = sageName->str(); ROSE_ASSERT( nameToMatch.length() > 0 ); SgClassDeclaration *sageClassDeclaration = isSgClassDeclaration (node); if (sageClassDeclaration != NULL) { std::string name = sageClassDeclaration->get_name ().str (); if( name == nameToMatch ) returnList.push_back(node); } return returnList; } /* End function:queryNodeCLassDeclarationFromName() */
// search for for statements NodeQuerySynthesizedAttributeType forStatementNodeQuery(SgNode* node) { ROSE_ASSERT(node != 0); NodeQuerySynthesizedAttributeType returnNodeList; if(isSgForStatement(node)) returnNodeList.push_back(node); return returnNodeList; }
NodeQuerySynthesizedAttributeType NodeQuery::queryNodeList ( NodeQuerySynthesizedAttributeType nodeList, VariantVector targetVariantVector) { NodeQuerySynthesizedAttributeType returnList; AstQueryNamespace::queryRange(nodeList.begin(), nodeList.end(), boost::bind(querySolverGrammarElementFromVariantVector, _1,targetVariantVector,&returnList)); return returnList; }
NodeQuerySynthesizedAttributeType NodeQuery::querySolverVariableDeclarations (SgNode * astNode) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; switch (astNode->variantT ()) { case V_SgVariableDeclaration: returnNodeList.push_back (astNode); break; case V_SgFunctionDeclaration: case V_SgMemberFunctionDeclaration: { SgFunctionDeclaration * sageFunctionDeclaration = isSgFunctionDeclaration (astNode); ROSE_ASSERT (sageFunctionDeclaration != NULL); SgInitializedNamePtrList sageInitializedNameList = sageFunctionDeclaration->get_args (); typedef SgInitializedNamePtrList::iterator variableIterator; for (variableIterator variableListElement = sageInitializedNameList.begin (); variableListElement != sageInitializedNameList.end (); ++variableListElement) { SgInitializedName* elmVar = *variableListElement; //SgVariableDeclaration* sageVariableDeclaration = isSgVariableDeclaration((elmVar.get_declaration())->copy()); //ROSE_ASSERT(sageVariableDeclaration != NULL); //if( sageVariableDeclaration != NULL) //AS (9/26/03) I must put an object of type const SgDeclarationStatement into the list because // I have no other way of finding the SgVariableDeclaration in the arguments. This is safe // because arguments are Variable Declarations, but puts unwelcome limits on use of returned // list because of it's constantness. ROSE_ASSERT (elmVar != NULL); returnNodeList.push_back (elmVar->get_declaration ()); //returnNodeList.push_back (sageVariableDeclaration); } break; } default: { // DQ (8/20/2005): Added default to avoid compiler warnings about unrepresented cases } } /* End switch-case */ return returnNodeList; } /* End function querySolverVariableDeclarations() */
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(); LivenessAnalysis* liv = new LivenessAnalysis(true,(DefUseAnalysis*)defuse); //example usage // testing std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions; bool abortme=false; NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition); NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin(); for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); 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 (rem_source.getNode()!=NULL) dfaFunctions.push_back(rem_source); if (abortme) break; } std::ofstream f2("var.dot"); dfaToDot(f2, string("var"), dfaFunctions, (DefUseAnalysis*)defuse, liv); f2.close(); if (abortme) { cerr<<"ABORTING ." << endl; exit(1); } delete project; delete defuse; }
NodeQuerySynthesizedAttributeType NodeQuery::querySolverMemberFunctionDeclarations (SgNode * astNode) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; if (isSgMemberFunctionDeclaration (astNode)) returnNodeList.push_back (astNode); return returnNodeList; } /* End function querySolverMemberFunctionDeclarations() */
int main(int argc, char *argv[]) { SgProject* sageProject = frontend(argc,argv); AstTests::runAllTests(sageProject); NodeQuerySynthesizedAttributeType functions = NodeQuery::querySubTree(sageProject, V_SgFunctionDefinition); for (NodeQuerySynthesizedAttributeType::const_iterator i = functions.begin(); i != functions.end(); ++i) { SgFunctionDefinition* proc = isSgFunctionDefinition(*i); ROSE_ASSERT (proc); testCFG(proc); } return 0; }
/* * The function * queryNodeAnonymousTypedef() * is a NodeQuery which finds all Anonymous Typedefs is the scope. */ NodeQuerySynthesizedAttributeType NodeQuery::queryNodeAnonymousTypedef(SgNode* node) { NodeQuerySynthesizedAttributeType returnList; ROSE_ASSERT( node != NULL ); SgTypedefDeclaration* sageTypedefDeclaration = isSgTypedefDeclaration(node); if (sageTypedefDeclaration != NULL) if(isSgClassType(sageTypedefDeclaration->get_base_type())) returnList.push_back(node); return returnList; } /* End function:queryNodeCLassDeclarationFromName() */
int main(int argc, char *argv[]) { std::string filename; SgProject *project = frontend(argc, argv); std::vector<InterproceduralInfo*> ip; // list < SgNode * >functionDeclarations = NodeQuery::querySubTree(project, V_SgFunctionDeclaration); NodeQuerySynthesizedAttributeType functionDeclarations = NodeQuery::querySubTree(project, V_SgFunctionDeclaration); // for (list < SgNode * >::iterator i = functionDeclarations.begin(); i != functionDeclarations.end(); i++) for (NodeQuerySynthesizedAttributeType::iterator i = functionDeclarations.begin(); i != functionDeclarations.end(); i++) { ControlDependenceGraph *cdg; InterproceduralInfo *ipi; SgFunctionDeclaration *fD = isSgFunctionDeclaration(*i); // SGFunctionDefinition * fDef; ROSE_ASSERT(fD != NULL); // CI (01/08/2007): A missing function definition is an indicator to a // // // librarycall. // * An other possibility would be a programmer-mistake, which we // don't treat at this point. // I assume librarycall if (fD->get_definition() == NULL) { } else { // get the control depenence for this function ipi=new InterproceduralInfo(fD); ROSE_ASSERT(ipi != NULL); // get control dependence for this function defintion cdg = new ControlDependenceGraph(fD->get_definition(), ipi); cdg->computeAdditionalFunctioncallDepencencies(); // cdg->computeInterproceduralInformation(ipi); // cdg->debugCoutNodeList(); // Liao, Feb. 7/2008, //strip off absolute path to avoid polluting the source tree with generated .dot files filename = StringUtility::stripPathFromFileName((fD->get_definition()->get_file_info()->get_filenameString ())) + "." + (fD->get_name().getString()) + ".cdg.dot"; cdg->writeDot((char *)filename.c_str()); } } }
void mergeList ( NodeQuerySynthesizedAttributeType & nodeList, const Rose_STL_Container<SgNode*> & localList ) { // Supporting function for querySolverGrammarElementFromVariantVector unsigned localListSize = localList.size(); unsigned nodeListSize = nodeList.size(); for (Rose_STL_Container<SgNode*>::const_iterator i = localList.begin(); i != localList.end(); i++) { // printf ("Adding node to list (%s) \n",(*i)->sage_class_name()); nodeList.push_back(*i); } ROSE_ASSERT (nodeList.size() == nodeListSize+localListSize); }
// DQ (3/25/2004): Added to support more general lookup of data in the AST NodeQuerySynthesizedAttributeType NodeQuery::querySolverGrammarElementFromVariant (SgNode * astNode, VariantT targetVariant) { ROSE_ASSERT (astNode != NULL); NodeQuerySynthesizedAttributeType returnNodeList; // printf ("Looking for a targetVariant = %d = %s \n",targetVariant,getVariantName(targetVariant).c_str()); if ( astNode->variantT() == targetVariant ) { //printf ("astNode = %s FOUND \n",astNode->sage_class_name()); returnNodeList.push_back (astNode); } return returnNodeList; } /* End function querySolverUnionFields() */
NodeQuerySynthesizedAttributeType NodeQuery::querySolverUnionDeclarations (SgNode * astNode) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; SgClassDeclaration *sageClassDeclaration = isSgClassDeclaration (astNode); if (sageClassDeclaration != NULL) if (sageClassDeclaration->get_class_type () == SgClassDeclaration::e_union) returnNodeList.push_back (astNode); return returnNodeList; } /* End function querySolverUnionDeclarations() */
/** Checks that all nodes in the list are unique. Emit errors about duplicate items. Returns the number of duplicates. */ static size_t check_unique(const NodeQuerySynthesizedAttributeType &nodes, const std::string &title) { std::set<SgNode*> set; std::vector<SgNode*> dups; for (NodeQuerySynthesizedAttributeType::const_iterator ni=nodes.begin(); ni!=nodes.end(); ++ni) { if (!set.insert(*ni).second) dups.push_back(*ni); } if (!dups.empty()) { std::cerr <<"Duplicate nodes returned for the \"" <<title <<"\" test:\n"; for (std::vector<SgNode*>::const_iterator di=dups.begin(); di!=dups.end(); ++di) emit_node_mesg(*di, "appears multiple times in list"); } return dups.size(); }
// Function querySolverAccessFunctions() // find access functions (function name starts with "get_" or "set_") NodeQuerySynthesizedAttributeType querySolverAccessFunctions (SgNode * astNode) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; SgFunctionDeclaration* funcDecl = isSgFunctionDeclaration(astNode); if (funcDecl != NULL) { string functionName = funcDecl->get_name().str(); if ( (functionName.length() >= 4) && ((functionName.substr(0,4) == "get_") || (functionName.substr(0,4) == "set_")) ) returnNodeList.push_back (astNode); } return returnNodeList; }
NodeQuerySynthesizedAttributeType NodeQuery::querySolverClassFields (SgNode * astNode) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; /* cout << "The name of the node is: \" " << astNode->sage_class_name() << "\"\n"; SgLocatedNode* sageLocatedNode = isSgLocatedNode(astNode); if(sageLocatedNode != NULL){ cout << "The filename is: " << sageLocatedNode->getFileName() << " At line number :" << sageLocatedNode->get_file_info()->get_line() << "\n"; } */ // SgNode *sageReturnNode = NULL; SgClassDefinition *sageClassDefinition = isSgClassDefinition (astNode); if (sageClassDefinition != NULL) { ROSE_ASSERT (sageClassDefinition->get_declaration () != NULL); if (sageClassDefinition->get_declaration ()->get_class_type () == SgClassDeclaration::e_class) { SgDeclarationStatementPtrList declarationStatementPtrList = sageClassDefinition->get_members (); typedef SgDeclarationStatementPtrList::iterator LI; for (LI i = declarationStatementPtrList.begin (); i != declarationStatementPtrList.end (); ++i) { SgNode *listElement = *i; if (isSgVariableDeclaration (listElement) != NULL){ /* if(isSgVariableDeclaration(listElement)->get_name().str() != NULL) cout << "The name of the variable declaration is: \"" << isSgVariableDeclaration(listElement)->get_name().str() << "\"\n"; else cout << "The name of the variable declaration is: \"\"\n";*/ returnNodeList.push_back (listElement); } } } } return returnNodeList; } /* End function querySolverClassFields() */
NodeQuerySynthesizedAttributeType queryNodeClassDeclarationFromTypedefName (SgNode * astNode, SgNode * nameNode) { NodeQuerySynthesizedAttributeType returnList; ROSE_ASSERT (nameNode != NULL); ROSE_ASSERT (nameNode != NULL); //finds the name which should be matched to SgName *sageName = isSgName (nameNode); ROSE_ASSERT (sageName != NULL); string nameToMatch = sageName->str (); ROSE_ASSERT (nameToMatch.length () > 0); if (isSgType (astNode) != NULL) { /*SgTypedefType* sageTypedefType = isSgTypedefType(astNode); string name = TransformationSupport::getTypeName(sageTypedefType); ROSE_ASSERT( nameToMatch.length() > 0 ); cout << nameToMatch << endl; */ #ifdef DEBUG_CGRAPHPP cout << TransformationSupport::getTypeName (isSgType (astNode)) << endl; #endif if (TransformationSupport::getTypeName (isSgType (astNode)) == nameToMatch) { returnList.push_back (astNode); } /* if(nameToMatch == name){ SgClassDeclaration *sageClassDeclaration = isSgClassDeclaration (sageTypedefType->get_declaration()); ROSE_ASSERT( sageClassDeclaration != NULL ); returnList.push_back(sageClassDeclaration); }*/ } return returnList; }
NodeQuerySynthesizedAttributeType NodeQuery::querySolverStructDefinitions (SgNode * astNode) { ROSE_ASSERT (astNode != 0); SgClassDefinition *sageClassDefinition = isSgClassDefinition (astNode); NodeQuerySynthesizedAttributeType returnNodeList; if (sageClassDefinition != NULL) { SgClassDeclaration *sageClassDeclaration = isSgClassDeclaration (sageClassDefinition->get_parent ()); ROSE_ASSERT (sageClassDeclaration != NULL); if (sageClassDeclaration->get_class_type () == SgClassDeclaration::e_struct) returnNodeList.push_back (astNode); } return returnNodeList; } /* End function querySolverClassFields() */
SgVariableSymbol* MintArrayInterface::getSymbolFromName(SgBasicBlock* block, string varStr) { NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(block, V_SgScopeStatement); NodeQuerySynthesizedAttributeType::const_iterator it = vars.begin(); for(it= vars.begin(); it != vars.end(); it++) { SgScopeStatement* outer_scope = isSgScopeStatement(*(it)); SgVariableSymbol* sym = outer_scope->lookup_var_symbol(varStr); if(sym!= NULL) return sym; } return NULL; }
NodeQuerySynthesizedAttributeType NodeQuery::querySolverUnionFields (SgNode * astNode) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; // SgNode *sageReturnNode = NULL; SgClassDefinition *sageClassDefinition = isSgClassDefinition (astNode); if (sageClassDefinition != NULL) { ROSE_ASSERT (sageClassDefinition->get_declaration () != NULL); if (sageClassDefinition->get_declaration ()->get_class_type () == SgClassDeclaration::e_union) { SgDeclarationStatementPtrList declarationStatementPtrList = sageClassDefinition->get_members (); typedef SgDeclarationStatementPtrList::iterator LI; for (LI i = declarationStatementPtrList.begin (); i != declarationStatementPtrList.end (); ++i) { SgNode *listElement = *i; if (isSgVariableDeclaration (listElement) != NULL) returnNodeList.push_back (listElement); } } } return returnNodeList; } /* End function querySolverUnionFields() */
NodeQuerySynthesizedAttributeType NodeQuery::querySolverFunctionDeclarationFromDefinition (SgNode * astNode, SgNode * functionDefinition) { ROSE_ASSERT (astNode != 0); NodeQuerySynthesizedAttributeType returnNodeList; // SgNode *sageReturnNode = NULL; SgMemberFunctionDeclaration *sageMemberFunctionDeclaration = isSgMemberFunctionDeclaration (astNode); if (sageMemberFunctionDeclaration != NULL) if (isSgClassDefinition (sageMemberFunctionDeclaration->get_parent ())) if (isSgNode (sageMemberFunctionDeclaration->get_definition ()) == functionDefinition) returnNodeList.push_back (astNode); return returnNodeList; } /* End function querySolverUnionFields() */
int main(int argc, char *argv[]) { struct timespec ts_now; long mtime_0, mtime_1, mtime_2, mtime_3; SgProject* sageProject = frontend(argc,argv); clock_gettime(CLOCK_MONOTONIC, &ts_now) ; mtime_0 = ts_now.tv_sec * 1000000 + ts_now.tv_nsec/1000; AstTests::runAllTests(sageProject); clock_gettime(CLOCK_MONOTONIC, &ts_now) ; mtime_1 = ts_now.tv_sec * 1000000 + ts_now.tv_nsec/1000; NodeQuerySynthesizedAttributeType functions = NodeQuery::querySubTree(sageProject, V_SgFunctionDefinition); clock_gettime(CLOCK_MONOTONIC, &ts_now) ; mtime_2 = ts_now.tv_sec * 1000000 + ts_now.tv_nsec/1000; for (NodeQuerySynthesizedAttributeType::const_iterator i = functions.begin(); i != functions.end(); ++i) { SgFunctionDefinition* proc = isSgFunctionDefinition(*i); ROSE_ASSERT (proc); testCFG(proc); } clock_gettime(CLOCK_MONOTONIC, &ts_now) ; mtime_3 = ts_now.tv_sec * 1000000 + ts_now.tv_nsec/1000; cout << "--------------------------------------------------" << endl; cout << "runAllTests: " << mtime_1 - mtime_0 << endl; cout << "pre-process: " << mtime_2 - mtime_1 << endl; cout << "cfg: " << mtime_3 - mtime_2 << endl; cout << "--------------------------------------------------" << endl; return 0; }
int main(int argc, char *argv[]) { std::string filename; SgProject *project = frontend(argc, argv); std::vector<InterproceduralInfo*> ip; #ifdef NEWDU // Create the global def-use analysis EDefUse *defUseAnalysis=new EDefUse(project); if (defUseAnalysis->run(false)==0) { std::cerr<<"DFAnalysis failed!"<<endl; } #endif string outputFileName=project->get_fileList().front()->get_sourceFileNameWithoutPath (); SystemDependenceGraph *sdg = new SystemDependenceGraph; // for all function-declarations in the AST NodeQuerySynthesizedAttributeType functionDeclarations = NodeQuery::querySubTree(project, V_SgFunctionDeclaration); for (NodeQuerySynthesizedAttributeType::iterator i = functionDeclarations.begin(); i != functionDeclarations.end(); i++) { ControlDependenceGraph *cdg; DataDependenceGraph *ddg; // FunctionDependenceGraph * pdg; InterproceduralInfo *ipi; SgFunctionDeclaration *fDec = isSgFunctionDeclaration(*i); ROSE_ASSERT(fDec != NULL); // CI (01/08/2007): A missing function definition is an indicator to a // // // librarycall. // * An other possibility would be a programmer-mistake, which we // don't treat at this point. // I assume librarycall if (fDec->get_definition() == NULL) {/* // if (fDec->get_file_info()->isCompilerGenerated()) continue; // treat librarycall -> iterprocedualInfo must be created... // make all call-parameters used and create a function stub for // the graph ipi=new InterproceduralInfo(fDec); ipi->addExitNode(fDec); sdg->addInterproceduralInformation(ipi); if (sdg->isKnownLibraryFunction(fDec)) { sdg->createConnectionsForLibaryFunction(fDec); } else { sdg->createSafeConfiguration(fDec); } ip.push_back(ipi);*/ // This is somewhat a waste of memory and a more efficient approach might generate this when needed, but at the momenent everything is created... } else { // get the control depenence for this function ipi=new InterproceduralInfo(fDec); ROSE_ASSERT(ipi != NULL); // get control dependence for this function defintion cdg = new ControlDependenceGraph(fDec->get_definition(), ipi); cdg->computeAdditionalFunctioncallDepencencies(); cdg->computeInterproceduralInformation(ipi); // get the data dependence for this function ddg = new DataDependenceGraph(fDec->get_definition(), defUseAnalysis,ipi); sdg->addFunction(cdg,ddg); sdg->addInterproceduralInformation(ipi); ip.push_back(ipi); } // else if (fD->get_definition() == NULL) } // now all function-declarations have been process as well have all function-definitions filename = (outputFileName) + ".no_ii.sdg.dot"; sdg->writeDot((char *)filename.c_str()); // perform interproceduralAnalysys sdg->performInterproceduralAnalysis(); filename = (outputFileName)+".deadEnds.sdg.dot"; sdg->writeDot((char *)filename.c_str()); set<SgNode*> preserve; if (sdg->getMainFunction()!=NULL) { preserve.insert(sdg->getMainFunction()); sdg->cleanUp(preserve); } filename = (outputFileName)+".final.sdg.dot"; sdg->writeDot((char *)filename.c_str()); }
int main(int argc, char *argv[]) { std::string filename; SgProject *project = frontend(argc, argv); #ifdef NEWDU EDefUse *edu=new EDefUse(project); // Create the global def-use analysis if (edu->run(false)==0) { std::cerr<<"DFAnalysis failed!"<<endl; } #endif std::vector<InterproceduralInfo*> ip; // list < SgNode * >functionDeclarations = NodeQuery::querySubTree(project, V_SgFunctionDeclaration); NodeQuerySynthesizedAttributeType functionDeclarations = NodeQuery::querySubTree(project, V_SgFunctionDeclaration); // for (list < SgNode * >::iterator i = functionDeclarations.begin(); i != functionDeclarations.end(); i++) for (NodeQuerySynthesizedAttributeType::iterator i = functionDeclarations.begin(); i != functionDeclarations.end(); i++) { DataDependenceGraph *ddg; InterproceduralInfo *ipi; SgFunctionDeclaration *fD = isSgFunctionDeclaration(*i); // SGFunctionDefinition * fDef; ROSE_ASSERT(fD != NULL); // CI (01/08/2007): A missing function definition is an indicator to a // // // librarycall. // * An other possibility would be a programmer-mistake, which we // don't treat at this point. // I assume librarycall if (fD->get_definition() == NULL) { } else { // get the control depenence for this function ipi=new InterproceduralInfo(fD); ROSE_ASSERT(ipi != NULL); // get the data dependence for this function #ifdef NEWDU ddg = new DataDependenceGraph(fD->get_definition(),edu); #else ddg = new DataDependenceGraph(fD->get_definition()); #endif //printf("DDG for %s:\n", fD->get_name().str()); // Liao, Feb. 7/2008, //strip off absolute path to avoid polluting the source tree with generated .dot files //filename = (fD->get_definition()->get_file_info()->get_filenameString ()) filename = StringUtility::stripPathFromFileName((fD->get_definition()->get_file_info()->get_filenameString ())) + "." + (fD->get_name().getString()) + ".ddg.dot"; ddg->writeDot((char *)filename.c_str()); } } return 0; }
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; }
NodeQuerySynthesizedAttributeType NodeQuery::querySolverVariableTypes (SgNode * astNode) { ROSE_ASSERT (astNode != NULL); NodeQuerySynthesizedAttributeType returnNodeList; /* SgVariableDeclaration* sageVariableDeclaration = isSgVariableDeclaration(astNode); if(sageVariableDeclaration != NULL) { SgInitializedNamePtrList sageInitializedNameList = sageVariableDeclaration->get_variables(); printf ("\nHere is a function declaration :Line = %d Columns = %d \n", ROSE:: getLineNumber (isSgLocatedNode(astNode) ), ROSE:: getColumnNumber ( isSgLocatedNode(astNode) )); cout << "The filename is:" << ROSE::getFileName(isSgLocatedNode(astNode)) << endl; typedef SgInitializedNamePtrList::iterator LI; for ( LI i = sageInitializedNameList.begin(); i != sageInitializedNameList.end(); ++i) { SgType* sageElementType = i->get_type(); ROSE_ASSERT( sageElementType != NULL); cout << "The class name is: " << sageElementType->sage_class_name() << endl; returnNodeList.push_back( sageElementType ); } cout << endl << "End printout of this Initialized Name list" << endl; } */ // SgVarRefExp *sageVarRefExp = isSgVarRefExp (astNode); switch (astNode->variantT ()) { case V_SgVariableDeclaration: { SgVariableDeclaration *sageVariableDeclaration = isSgVariableDeclaration (astNode); ROSE_ASSERT (sageVariableDeclaration != NULL); SgInitializedNamePtrList sageInitializedNameList = sageVariableDeclaration->get_variables (); #if DEBUG_NODEQUERY printf ("\nIn filename: %s ", ROSE::getFileName (isSgLocatedNode (astNode))); printf ("\nHere is a variable :Line = %d Columns = %d \n", ROSE::getLineNumber (isSgLocatedNode (astNode)), ROSE::getColumnNumber (isSgLocatedNode (astNode))); //cout << "The typename of the variable is: " << typeName << endl; #endif typedef SgInitializedNamePtrList::iterator variableIterator; SgType *typeNode; for (variableIterator variableListElement = sageInitializedNameList.begin (); variableListElement != sageInitializedNameList.end (); ++variableListElement) { SgInitializedName* elmVar = *variableListElement; ROSE_ASSERT (elmVar != NULL); typeNode = elmVar->get_type (); ROSE_ASSERT (typeNode != NULL); returnNodeList.push_back (typeNode); } break; } /* End case V_SgVariableDeclaration */ case V_SgFunctionDeclaration: case V_SgMemberFunctionDeclaration: { SgFunctionDeclaration * sageFunctionDeclaration = isSgFunctionDeclaration (astNode); ROSE_ASSERT (sageFunctionDeclaration != NULL); SgInitializedNamePtrList sageInitializedNameList = sageFunctionDeclaration->get_args (); SgType *typeNode; typedef SgInitializedNamePtrList::iterator variableIterator; for (variableIterator variableListElement = sageInitializedNameList.begin (); variableListElement != sageInitializedNameList.end (); ++variableListElement) { SgInitializedName* elmVar = *variableListElement; ROSE_ASSERT (elmVar != NULL); typeNode = elmVar->get_type (); ROSE_ASSERT (typeNode != NULL); returnNodeList.push_back (typeNode); } break; } default: { // DQ (8/20/2005): Added default to avoid compiler warnings about unrepresented cases } } /* End switch case astNode */ return returnNodeList; } /* End function querySolverType() */
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; }
NodeQuerySynthesizedAttributeType NodeQuery::queryNodeClassDeclarationsFromTypeName(SgNode* node, SgNode* nameNode) { NodeQuerySynthesizedAttributeType returnList; ROSE_ASSERT( nameNode != NULL ); ROSE_ASSERT( node != NULL ); // finds the name which should be matched to SgName* sageName = isSgName(nameNode); ROSE_ASSERT( sageName != NULL ); std::string nameToMatch = sageName->str(); ROSE_ASSERT( nameToMatch.length() > 0 ); SgClassDeclaration *sageClassDeclaration = isSgClassDeclaration (node); if (sageClassDeclaration != NULL) { if(TransformationSupport::getTypeName(sageClassDeclaration->get_type()) == nameToMatch) returnList.push_back(node); else { SgClassDefinition* classDefinition = isSgClassDefinition(sageClassDeclaration->get_definition()); ROSE_ASSERT( classDefinition != NULL ); // SgBaseClassList baseClassList = classDefinition->get_inheritances(); SgBaseClassPtrList baseClassList = classDefinition->get_inheritances(); typedef SgBaseClassPtrList::iterator SgBaseClassPtrListIterator; for( SgBaseClassPtrListIterator baseClassElm = baseClassList.begin(); baseClassElm != baseClassList.end(); ++baseClassElm) { // SgBaseClass baseClass = *baseClassElm; SgBaseClass* baseClass = *baseClassElm; // sageClassDeclaration = baseClass.get_base_class(); sageClassDeclaration = baseClass->get_base_class(); std::string typeName = TransformationSupport::getTypeName ( sageClassDeclaration->get_type() ); if( typeName == nameToMatch ) returnList.push_back(node); } } /* SgType* typeNode = sageClassDeclaration->get_type (); ROSE_ASSERT (typeNode != NULL); string currentTypeName = ""; string previousTypeName = ""; do{ previousTypeName = currentTypeName; currentTypeName = TransformationSupport::getTypeName (typeNode); typeNode = typeNode->findBaseType(); ROSE_ASSERT( typeNode != NULL ); if( currentTypeName == nameToMatch ){ returnList.push_back(node); break; } cout<< "\n\n The typenames is : " << currentTypeName << "\n\n" << previousTypeName << "\n\n"; }while( previousTypeName != currentTypeName); */ } return returnList; } /* End function:queryNodeCLassDeclarationFromName() */
int main(int argc, char *argv[]) { SgProject* project = frontend(argc,argv); AstTests::runAllTests(project); // run internal consistency tests on the AST size_t nerrors = 0; std::string separator = std::string(80, '-') + "\n"; std::cerr <<separator <<"Testing NodeQuery::querySubTree for all SgFunctionDeclaration nodes\n"; NodeQuerySynthesizedAttributeType funcDecls = NodeQuery::querySubTree(project, V_SgFunctionDeclaration); std::cerr <<"found " <<funcDecls.size() <<" function declaration nodes\n"; nerrors += check_unique(funcDecls, "querySubTree SgFunctionDeclaration"); for (NodeQuerySynthesizedAttributeType::const_iterator ni=funcDecls.begin(); ni!=funcDecls.end(); ++ni) { if (!isSgFunctionDeclaration(*ni)) { emit_node_mesg(*ni, "not a function declaration"); ++nerrors; } } ROSE_ASSERT(0==nerrors); // optional, to exit early std::cerr <<separator <<"Testing NodeQuery::queryNodeList for all SgFunctionDeclaration nodes\n"; NodeQuerySynthesizedAttributeType funcDecls2 = NodeQuery::queryNodeList(funcDecls, V_SgFunctionDeclaration); std::cerr <<"found " <<funcDecls2.size() <<" function declaration nodes\n"; nerrors += check_unique(funcDecls2, "queryNodeList SgFunctionDeclaration"); for (NodeQuerySynthesizedAttributeType::const_iterator ni=funcDecls2.begin(); ni!=funcDecls2.end(); ++ni) { if (!isSgFunctionDeclaration(*ni)) { emit_node_mesg(*ni, "not a function declaration"); ++nerrors; } } ROSE_ASSERT(0==nerrors); // optional, to exit early std::cerr <<separator <<"Testing NameQuery::querySubTree for FunctionDeclarationNames\n"; NameQuerySynthesizedAttributeType funcNames = NameQuery::querySubTree(project, NameQuery::FunctionDeclarationNames); std::cerr <<"found " <<funcNames.size() <<" function declaration names\n"; if (funcNames.size() != funcDecls.size()) { std::cerr <<"number of function declaration names (" <<funcNames.size() <<")" <<" does not match number of function declaration nodes (" <<funcDecls.size() <<")\n"; ++nerrors; } ROSE_ASSERT(0==nerrors); // optional, to exit early std::cerr <<separator <<"Testing NameQuery::queryNodeList for FunctionDeclarationNames\n"; NameQuerySynthesizedAttributeType funcNames2 = NameQuery::queryNodeList(funcDecls, NameQuery::FunctionDeclarationNames); std::cerr <<"found " <<funcNames2.size() <<" function declaration names\n"; if (funcNames2.size() != funcDecls.size()) { std::cerr <<"number of function declaration names (" <<funcNames2.size() <<")" <<" does not match number of function declaration nodes (" <<funcDecls.size() <<")\n"; ++nerrors; } ROSE_ASSERT(0==nerrors); // optional, to exit early std::cerr <<separator <<"Testing NumberQuery::querySubTree for NumberOfArgsInConstructor\n"; NumberQuerySynthesizedAttributeType ctorArgCounts = NumberQuery::querySubTree(project, NumberQuery::NumberOfArgsInConstructor); std::cerr <<"found " <<ctorArgCounts.size() <<" results\n"; if (ctorArgCounts.size() != 1) { std::cerr <<"should have found only one result\n"; ++nerrors; } ROSE_ASSERT(0==nerrors); // optional, to exit early // It is not necessary to call backend for this test; that functionality is tested elsewhere. return nerrors ? 1 : 0; }