/* * The function * queryNodePragmaDeclarationFromName() * takes as a first parameter a SgNode*. As a second parameter it takes * a SgNode* who must be of type SgName. The SgName contains a std::string which * should be the same as the left side in the pragma or a part of the left * side of the pragma. If the std::string is empty, * there will be an error message. * * #pragma std::stringInSgNode = information * */ Rose_STL_Container<SgNode*> NodeQuery::queryNodePragmaDeclarationFromName(SgNode* node, SgNode* nameNode){ ROSE_ASSERT( nameNode != NULL ); ROSE_ASSERT( node != NULL ); Rose_STL_Container<SgNode*> returnList; //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 ); if(node->variantT() == V_SgPragmaDeclaration){ SgPragmaDeclaration* sagePragmaDeclaration = isSgPragmaDeclaration(node); ROSE_ASSERT( sagePragmaDeclaration ); ROSE_ASSERT( sagePragmaDeclaration->get_pragma() != NULL ); // ROSE_ASSERT( sagePragmaDeclaration->get_pragma()->get_pragma() ); std::string pragmaDeclarationString = sagePragmaDeclaration->get_pragma()->get_pragma(); //extract the part before the leftmost = is pragmaDeclarationString pragmaDeclarationString = pragmaDeclarationString.substr(0,pragmaDeclarationString.find("=")); //if the name-criteria is met accept node if(pragmaDeclarationString.find( nameToMatch ) != pragmaDeclarationString.length() ){ cout << pragmaDeclarationString << endl; returnList.push_back(node); } } return returnList; }
/* * The function * queryPragmaString() * teakes a SgNode* as a parameter and return a list<string>. It is a NameQuery * which query the AST or a subtree of the AST for the string of all the * SgPragma's. * */ Rose_STL_Container< string > queryFindPragmaString (SgNode * node) { Rose_STL_Container< string > returnList; if (node->variantT () == V_SgPragmaDeclaration) { SgPragmaDeclaration *sagePragmaStatement = isSgPragmaDeclaration (node); ROSE_ASSERT (sagePragmaStatement); ROSE_ASSERT (sagePragmaStatement->get_pragma () != NULL); returnList.push_back (sagePragmaStatement->get_pragma ()-> get_pragma ()); } return returnList; }
void mlmFrontend::visit(SgNode* node) { if (isSgPragmaDeclaration(node) != NULL) { SgPragmaDeclaration * pragDecl = isSgPragmaDeclaration(node); char* end; int type = strtol(pragDecl->get_pragma()->get_pragma().c_str(),&end, 10); if(end == pragDecl->get_pragma()->get_pragma().c_str()) { cout << "pragma has no integer value" << endl; type = -1; } if(type >= 0) { //cout << "type = " << type << endl; mlmAttribute* newAttr = new mlmAttribute(type); mlmFrontend::attachAttribute(pragDecl, newAttr); DeletepragmasList.push_back(pragDecl); } // handle omp parallel, omp for, omp single, ..etc. //createOmpAttributeOld(node); } }
POETCode* ROSE_2_POET_list(PtrList& l, POETCode* res, SgTemplateInstantiationFunctionDecl* tmp) { static POETCode* tmpPars=0; if (l.size() == 1) { /* return the only element inside the list */ return POETAstInterface::Ast2POET(*l.begin()); } typename PtrList::const_reverse_iterator p = l.rbegin(); if (p == l.rend()) return (res==0)? EMPTY : res; SgNode* prev2 = (SgNode*)*p; POETCode* prev = POETAstInterface::Ast2POET(prev2); for (++p; p != l.rend(); ++p) { SgNode* cur = (SgNode*)*p; SgPragmaDeclaration* info = isSgPragmaDeclaration(cur); if (info!=0) { /* replace template parameters in pragma */ POETAstInterface::set_Ast2POET(prev2, EMPTY); std::string pragma_str = info->get_pragma()->get_pragma(); POETCode *content=STRING(pragma_str); POETCode* tokens = split_string("",content); if (tmp == 0) tmpPars = 0; else { const SgTemplateArgumentPtrList& args= tmp->get_templateArguments(); if (tmpPars == 0) tmpPars=get_template_parameters(tmp->get_templateDeclaration ()->get_string()); POETCode* pars = tmpPars; for (SgTemplateArgumentPtrList::const_iterator p2 = args.begin(); p2 != args.end(); p2++) { POETCode* from = get_head(pars); pars=get_tail(pars); POETCode* to = STRING((*p2)->unparseToString()); tokens = eval_replace(from, to, tokens); } } prev = new POETCode_ext(info, PAIR(tokens, prev)); POETAstInterface::set_Ast2POET(info,prev); } else { res = LIST(prev, res); prev = POETAstInterface::Ast2POET((SgNode*)cur); } } res = LIST(prev,res); return res; }
//! Parse failsafe directives and generate/attach attributes // follow the code in void attachOmpAttributeInfo(SgSourceFile *sageFilePtr) of ompAstConstruction.cpp void FailSafe::parse_directives_to_attributes(SgSourceFile* sageFilePtr) { ROSE_ASSERT(sageFilePtr != NULL); //TODO check language extension flag for turning on this support //if (sageFilePtr->get_failsafe() == false) return if (sageFilePtr->get_Fortran_only()||sageFilePtr->get_F77_only()||sageFilePtr->get_F90_only()| sageFilePtr->get_F95_only() || sageFilePtr->get_F2003_only()) { //TODO handle Fortran later } else { // For C/C++, search pragma declarations for failsafe directives std::vector <SgNode*> all_pragmas = NodeQuery::querySubTree (sageFilePtr, V_SgPragmaDeclaration); std::vector<SgNode*>::iterator iter; for(iter=all_pragmas.begin();iter!=all_pragmas.end();iter++) { SgPragmaDeclaration* pragmaDeclaration = isSgPragmaDeclaration(*iter); ROSE_ASSERT(pragmaDeclaration != NULL); SageInterface::replaceMacroCallsWithExpandedStrings(pragmaDeclaration); string pragmaString = pragmaDeclaration->get_pragma()->get_pragma(); istringstream istr(pragmaString); std::string key; istr >> key; if (key == "failsafe") { FailSafe::AttributeList * previous = FailSafe::getAttributeList (pragmaDeclaration); FailSafe::pragma_list.push_back(pragmaDeclaration); if (previous == NULL) { FailSafe::Attribute* attribute = parse_fail_safe_directive(pragmaDeclaration); FailSafe::addAttribute(attribute, pragmaDeclaration); } // end if NULL } // end if key is failsafe } // end for all_pragmas } // end if }
Rose_STL_Container< ControlStructureContainer * >queryFindCommentsInScope (const string stringPrefixToMatch, const string stringToMatch, SgScopeStatement * sageScopeStatement) { ROSE_ASSERT (stringPrefixToMatch.length () > 0); ROSE_ASSERT (stringToMatch.length () > 0); ROSE_ASSERT (sageScopeStatement != NULL); Rose_STL_Container< ControlStructureContainer * >returnList; //find all pragmas who match the stringToMatch //cout << "Before pragma search" << endl; /*list < SgNode * >pragmaStatements = NodeQuery::querySubTree (sageScopeStatement, new SgName (stringToMatch.c_str ()), NodeQuery::PragmaDeclarationFromName); */ //list<SGNode*> pragmaStatements ; //cout << "After pragma search" << endl; /* cout << "BEFORE LIST" << endl; list < SgNode * >pragmaStatements = queryNodePragmaStatementFromName2(sageScopeStatement, new SgName(stringToMatch.c_str())); cout << "AFTER LIST" << endl;*/ //return the pragmas in containers /* for (list < SgNode * >::iterator i = pragmaStatements.begin (); i != pragmaStatements.end (); ++i) { SgPragmaDeclaration *sagePragma = isSgPragmaDeclaration (*i); ROSE_ASSERT (sagePragma); ROSE_ASSERT (sagePragma->get_pragma () != NULL); ROSE_ASSERT (sagePragma->get_pragma ()->get_pragma ()); ControlStructureContainer *container = new ControlStructureContainer (); container->setPragmaString (sagePragma->get_pragma ()->get_pragma ()); container->setAssociatedStatement (sagePragma); returnList.push_back (container); } */ //find all statements in the current scope if (sageScopeStatement->variantT () == V_SgClassDefinition) { SgDeclarationStatementPtrList statementsInScope = sageScopeStatement->getDeclarationList (); SgDeclarationStatementPtrList::iterator i; for (i = statementsInScope.begin (); i != statementsInScope.end (); i++) { SgLocatedNode *locatedNode = isSgLocatedNode (*i); ROSE_ASSERT (locatedNode != NULL); //find all comments attached to current node. AttachedPreprocessingInfoType *comments = locatedNode->getAttachedPreprocessingInfo (); if(locatedNode->variantT() == V_SgPragmaDeclaration){ SgPragmaDeclaration* sagePragmaDeclaration = isSgPragmaDeclaration(locatedNode); ROSE_ASSERT( sagePragmaDeclaration ); ROSE_ASSERT( sagePragmaDeclaration->get_pragma() != NULL ); string pragmaDeclarationString = sagePragmaDeclaration->get_pragma()->get_pragma(); //extract the part before the leftmost = is pragmaDeclarationString pragmaDeclarationString = pragmaDeclarationString.substr(0,pragmaDeclarationString.find("=")); //if the name-criteria is met accept node if(pragmaDeclarationString.find( stringToMatch ) != string::npos ){ cout << pragmaDeclarationString << endl; ControlStructureContainer *container = new ControlStructureContainer (); container->setPragmaString (sagePragmaDeclaration->get_pragma ()->get_pragma ()); container->setAssociatedStatement (sagePragmaDeclaration); returnList.push_back (container); } } if (comments != NULL) { //We need to find comments which fits the criteria printf ("Found attached comments (at %p of type: %s): \n", locatedNode, locatedNode->sage_class_name ()); AttachedPreprocessingInfoType::iterator j; for (j = comments->begin (); j != comments->end (); j++) { ROSE_ASSERT ((*j) != NULL); string comment = (*j)->getString (); //see if comment begins with stringPrefixToMatch string tempString = comment.substr (0, comment.find (' ')); if (tempString == stringPrefixToMatch) { //+stringPrefixToMatch ){ //cout << "Found string" << endl; comment = StringUtility::copyEdit (comment, stringPrefixToMatch, ""); //see if the comment has an element which matches the stringToMatch if (comment.find (stringToMatch) != string::npos) { //puit the matching comment into a container ControlStructureContainer *container = new ControlStructureContainer (); container->setPragmaString (comment); container->setAssociatedStatement (locatedNode); returnList.push_back (container); } } // printf (" Attached Comment (relativePosition=%s):\n %s\n Next comment: \n", // ((*j)->relativePosition == PreprocessingInfo::before) ? "before" : "after",(*j)->getString()); } } } } else { // AS 12/18/03 PS!! The same as the above, but a different iterator. Will replace this when a // different solution has arisen. PS!! SgStatementPtrList statementsInScope = sageScopeStatement->getStatementList (); SgStatementPtrList::iterator i; for (i = statementsInScope.begin (); i != statementsInScope.end (); i++) { SgLocatedNode *locatedNode = isSgLocatedNode (*i); ROSE_ASSERT (locatedNode != NULL); if(locatedNode->variantT() == V_SgPragmaDeclaration){ SgPragmaDeclaration* sagePragmaDeclaration = isSgPragmaDeclaration(locatedNode); ROSE_ASSERT( sagePragmaDeclaration ); ROSE_ASSERT( sagePragmaDeclaration->get_pragma() != NULL ); string pragmaDeclarationString = sagePragmaDeclaration->get_pragma()->get_pragma(); //extract the part before the leftmost = is pragmaDeclarationString pragmaDeclarationString = pragmaDeclarationString.substr(0,pragmaDeclarationString.find("=")); //if the name-criteria is met accept node if(pragmaDeclarationString.find( stringToMatch ) != string::npos ){ cout << pragmaDeclarationString << endl; ControlStructureContainer *container = new ControlStructureContainer (); container->setPragmaString (sagePragmaDeclaration->get_pragma ()->get_pragma ()); container->setAssociatedStatement (sagePragmaDeclaration); returnList.push_back (container); } } //find all comments attached to current node. AttachedPreprocessingInfoType *comments = locatedNode->getAttachedPreprocessingInfo (); if (comments != NULL) { //We need to find comments which fits the criteria //printf ("Found attached comments (at %p of type: %s): \n",locatedNode,locatedNode->sage_class_name()); AttachedPreprocessingInfoType::iterator j; for (j = comments->begin (); j != comments->end (); j++) { ROSE_ASSERT ((*j) != NULL); string comment = (*j)->getString (); //see if comment begins with stringPrefixToMatch string tempString = comment.substr (0, comment.find (' ')); if (tempString == stringPrefixToMatch) { //+stringPrefixToMatch ){ comment = StringUtility::copyEdit (comment, stringPrefixToMatch, ""); cout << "And the string is: " << comment << endl; if (comment.find (stringToMatch) != string::npos) { cout << "And even the string is matched." << endl; //ROSE_ASSERT(comment.find('=') != comment.length()); //string variableName = comment.substr(comment.find(stringToMatch), comment.find('=')); cout << "And the string is a match" << endl; //puit the matching comment into a container ControlStructureContainer *container = new ControlStructureContainer (); container->setPragmaString (comment); container->setAssociatedStatement (locatedNode); returnList.push_back (container); } } /* printf (" Attached Comment (relativePosition=%s):\n %s\n Next comment: \n", ((*j)->relativePosition == PreprocessingInfo::before) ? "before" : "after", (*j)->getString ());*/ } } } } return returnList; } /* End function: queryFindCommentsInScope() */