CPPImperialOpConstDefinition::CPPImperialOpConstDefinition ( SgExprListExp * parameters) { using boost::lexical_cast; using std::string; dimension = isSgIntVal (parameters->get_expressions ()[indexDimenson])->get_value (); SgAddressOfOp * addressOfOperator = isSgAddressOfOp ( parameters->get_expressions ()[indexData]); if (addressOfOperator != NULL) { SgVarRefExp * operandExpression = isSgVarRefExp ( addressOfOperator->get_operand ()); ROSE_ASSERT (operandExpression != NULL); variableName = operandExpression->get_symbol ()->get_name ().getString (); } else { variableName = isSgVarRefExp (parameters->get_expressions ()[indexData])->get_symbol ()->get_name ().getString (); } ROSE_ASSERT (dimension > 0); ROSE_ASSERT (variableName.empty () == false); Debug::getInstance ()->debugMessage ("Found an OP_CONST declaration: '" + variableName + "' Its dimension is " + lexical_cast <string> (dimension), Debug::FUNCTION_LEVEL, __FILE__, __LINE__); }
int main(int argc, char* argv[]) { SgProject* project = frontend(argc, argv); std::vector<SgIfStmt*> ifs = SageInterface::querySubTree<SgIfStmt>(project, V_SgIfStmt); BOOST_FOREACH(SgIfStmt* if_stmt, ifs) { if (SgExpression *se = isSgExprStatement(if_stmt->get_conditional())->get_expression()) { cout << "--->" << se->unparseToString() << "<---" << endl; Rose_STL_Container<SgNode*> variableList = NodeQuery::querySubTree(se, V_SgVarRefExp); for (Rose_STL_Container<SgNode*>::iterator i = variableList.begin(), end = variableList.end(); i != end; i++) { SgVarRefExp *varRef = isSgVarRefExp(*i); SgVariableSymbol *currSym = varRef->get_symbol(); cout << "Looking at: --|" << currSym->get_name().str() << "|--" << endl; SgDeclarationStatement *decl = currSym->get_declaration()->get_declaration(); cout << "declaration: " << decl->unparseToString() << endl; SgConstVolatileModifier cvm = decl->get_declarationModifier().get_typeModifier().get_constVolatileModifier(); bool constness = cvm.isConst(); cout << "constness via isConst(): " << constness << endl; cout << cvm.displayString() << endl; } } } return 0; }
/* * Case to handle interleaveArrayOption * C = interleaveAcrossArrays(A,B); * i/p = A,B ; o/p = C ; operation = interleaveAcrossArrays */ void specificationTraversal::handleInterLeaveAcrossArrays(SgFunctionCallExp* funcCallExp) { ROSE_ASSERT(isSgFunctionCallExp(funcCallExp) != NULL); SgExpressionPtrList& args = funcCallExp->get_args()->get_expressions(); vector < string > inputArgs; vector < string > outputArgs; // Extract the argument and put it into the input list for (SgExpressionPtrList::iterator expr = args.begin(); expr != args.end(); expr++) { SgVarRefExp* varRefExp = isSgVarRefExp(*expr); ROSE_ASSERT(varRefExp != NULL); string argName = varRefExp->get_symbol()->get_declaration()->get_name().getString(); inputArgs.push_back(argName); #if DEBUG cout << " Input Arg: " << argName << endl; #endif } // Extract the output SgAssignOp* assignOp = isSgAssignOp(funcCallExp->get_parent()); SgVarRefExp* outputVarRefExp = isSgVarRefExp(assignOp->get_lhs_operand()); ROSE_ASSERT(outputVarRefExp != NULL); string outputName = outputVarRefExp->get_symbol()->get_declaration()->get_name().getString(); outputArgs.push_back(outputName); #if DEBUG cout << " Output Arg: " << outputName << endl; #endif // Add to worklist transformationWorklist.addToWorklist(LayoutOptions::InterleaveAcrossArrays, inputArgs, outputArgs); }
// Within sc, is the variable toCheck modified between the declaration of // lifetime and its last use? This is used to determine whether, whenever // toCheck and lifetime are equal, one can be used as a substitute for the // other. bool isPotentiallyModifiedDuringLifeOf(SgBasicBlock* sc, SgInitializedName* toCheck, SgInitializedName* lifetime) { SgStatementPtrList& stmts = sc->get_statements(); bool inLiveRange = false; for (SgStatementPtrList::reverse_iterator i = stmts.rbegin(); i != stmts.rend(); ++i) { if (containsVariableReference(*i, lifetime)) inLiveRange = true; SgVariableSymbol* toCheckSym = new SgVariableSymbol(toCheck); SgVarRefExp* toCheckVr = new SgVarRefExp(SgNULL_FILE, toCheckSym); bool result = false; if (inLiveRange && isPotentiallyModified(toCheckVr, *i)) { result = true; } delete toCheckSym; toCheckSym = NULL; toCheckVr->set_symbol(NULL); delete toCheckVr; toCheckVr = NULL; if (result) return true; if (isSgVariableDeclaration(*i) && isDeclarationOf(isSgVariableDeclaration(*i), lifetime)) return false; // This must be last } return false; }
// Check if this is an A++ Array Reference bool isAPPArray(SgNode *astNode) { SgVarRefExp* varRefExp = isSgVarRefExp(astNode); if (varRefExp == NULL) return false; SgVariableSymbol* variableSymbol = varRefExp->get_symbol(); ROSE_ASSERT(variableSymbol != NULL); SgInitializedName* initializedName = variableSymbol->get_declaration(); ROSE_ASSERT(initializedName != NULL); SgName variableName = initializedName->get_name(); // Now compute the offset to the index objects (form a special query for this???) SgType* type = variableSymbol->get_type(); ROSE_ASSERT(type != NULL); string typeName = TransformationSupport::getTypeName(type); ROSE_ASSERT(typeName.c_str() != NULL); // Recognize only these types at present if (typeName == "intArray" || typeName == "floatArray" || typeName == "doubleArray") { return true; } return false; }
// Are any variables in syms modified anywhere within n, or is n a declaration // of one of them? // FIXME: move to inliner bool anyOfListPotentiallyModifiedIn(const vector<SgVariableSymbol*>& syms, SgNode* n) { bool modified = false; for (vector<SgVariableSymbol*>::const_iterator j = syms.begin(); j != syms.end(); ++j) { SgVarRefExp* vr = new SgVarRefExp(SgNULL_FILE, *j); vr->set_endOfConstruct(SgNULL_FILE); if (isPotentiallyModified(vr, n)) { modified = true; } delete vr; if (modified) break; if (isSgVariableDeclaration(n)) { SgVariableDeclaration* decl = isSgVariableDeclaration(n); for (SgInitializedNamePtrList::const_iterator i = decl->get_variables().begin(); i != decl->get_variables().end(); ++i) { if (*i == (*j)->get_declaration()) { modified = true; break; } } } if (modified) break; } return modified; }
int BasicProgmemTransform::getBuffersizeNeededForFunction(SgFunctionDeclaration *func) { int maxSize = 0; Rose_STL_Container<SgNode *> funcCalls = NodeQuery::querySubTree(func, V_SgFunctionCallExp); for(auto &funcCall: funcCalls) { SgFunctionCallExp *fcall = isSgFunctionCallExp(funcCall); Function callee(fcall); // printf("function called: %s\n", callee.get_name().str()); if(isArduinoProgmemSafeFunction(callee)) { continue; } param_pos_list ignoredPositions = getPositionsToIgnore(callee.get_name().getString()); SgExpressionPtrList params = fcall->get_args()->get_expressions(); int size = 0; for(int pos = 0; pos < params.size(); pos++) { if(ignoredPositions.find(pos) != ignoredPositions.end()) { continue; } SgVarRefExp* var = isSgVarRefExp(params[pos]); if(var) { SgInitializedName *initName = var->get_symbol()->get_declaration(); if(isVarDeclToRemove(initName)) { SgExpression *rhs = getRHSOfVarDecl(initName); if(rhs && isSgStringVal(rhs)) { size += isSgStringVal(rhs)->get_value().size() + 1; } } } } if(size > maxSize) { maxSize = size; } } return maxSize; }
/* * Fix op structure calls and inject debug names */ void OPSource::fixOpFunctions(SgNode *n) { SgName var_name; SgFunctionCallExp *fn = isSgFunctionCallExp(n); if(fn != NULL) { string fn_name = fn->getAssociatedFunctionDeclaration()->get_name().getString(); if(fn_name.compare("op_decl_const")==0) { SgExprListExp* exprList = fn->get_args(); SgExpressionPtrList &exprs = exprList->get_expressions(); if( isSgStringVal(exprs.back()) == NULL ) { SgVarRefExp* varExp = isSgVarRefExp(exprs[1]); if(!varExp) { varExp = isSgVarRefExp(isSgAddressOfOp(exprs[1])->get_operand_i()); } if(varExp) { var_name = varExp->get_symbol()->get_name(); } cout << "---Injecting Debug Name for const: " << var_name.getString() << "---" << endl; exprList->append_expression(buildStringVal(var_name)); } } } }
void CompassAnalyses::SizeOfPointer::Traversal:: visit(SgNode* node) { int starCount; SgSizeOfOp *szOf = isSgSizeOfOp(node); if(!szOf) return; Rose_STL_Container<SgNode*> pointers = NodeQuery::querySubTree(node,V_SgPointerType); Rose_STL_Container<SgNode*> deRefs = NodeQuery::querySubTree(node,V_SgPointerDerefExp); Rose_STL_Container<SgNode*> varRefs = NodeQuery::querySubTree(node,V_SgVarRefExp); for (Rose_STL_Container<SgNode *>::iterator i = varRefs.begin(); i != varRefs.end(); i++) { SgVarRefExp *vRef = isSgVarRefExp((*i)); if (!vRef) return; SgType *t = vRef->get_type(); std::string typeName = t->unparseToString(); //std::cout << countStars(typeName) << std::endl; starCount = countStars(typeName); if (!starCount or (starCount == pointers.size() and deRefs.size() == (starCount - 1))) { //std::cout << "IT'S OK!" << std::endl; return; } } output->addOutput(new CheckerOutput(node)); } //End of the visit function.
static void run(Compass::Parameters parameters, Compass::OutputObject* output) { // We only care about source code in the user's space, not, // for example, Boost or system files. string target_directory = parameters["general::target_directory"].front(); CompassAnalyses::ByteByByteStructureComparison::source_directory.assign(target_directory); // Use the pre-built ROSE AST SgProject* sageProject = Compass::projectPrerequisite.getProject(); // perform AST matching here AstMatching match_functions; MatchResult result_functions = match_functions.performMatching("$r = SgFunctionCallExp(SgFunctionRefExp,SgExprListExp(_,_,_))", sageProject); BOOST_FOREACH(SingleMatchVarBindings match, result_functions) { SgFunctionCallExp* function_call = (SgFunctionCallExp*)match["$r"]; AstMatching match_vars; MatchResult result_vars; if ("memcmp" == function_call->getAssociatedFunctionDeclaration()->get_name().getString()) result_vars = match_vars.performMatching("$s = SgVarRefExp", function_call); else continue; bool struct_used = false; BOOST_FOREACH(SingleMatchVarBindings var_match, result_vars) { SgVarRefExp* var = (SgVarRefExp*)var_match["$s"]; if (isSgClassType(var->get_type()->findBaseType()) != NULL) struct_used = true; }
virtual void visit(SgNode* n) { SgVarRefExp* vr = isSgVarRefExp(n); if (!vr) return; SgInitializedName* in = vr->get_symbol()->get_declaration(); paramMapType::const_iterator iter = paramMap.find(in); if (iter == paramMap.end()) return; // This is not a parameter use vr->set_symbol(iter->second); }
virtual void visit(SgNode* n) { if (isSgThisExp(n)) { SgVarRefExp* vr = new SgVarRefExp(SgNULL_FILE, sym); vr->set_endOfConstruct(SgNULL_FILE); isSgExpression(n->get_parent())-> replace_expression(isSgExpression(n), vr); } }
virtual void visit(SgNode* n) { if (isSgBasicBlock(n)) { SgBasicBlock* bb = isSgBasicBlock(n); SgStatementPtrList& stmts = bb->get_statements(); size_t initi; for (size_t decli = 0; decli < stmts.size(); ++decli) { if (isSgVariableDeclaration(stmts[decli])) { SgVariableDeclaration* decl = isSgVariableDeclaration(stmts[decli]); SgInitializedNamePtrList& vars = decl->get_variables(); for (size_t vari = 0; vari != vars.size(); ++vari) { SgInitializedName* in = vars[vari]; if (in->get_initializer() == 0) { bool used = false; for (initi = decli + 1; initi < stmts.size(); used |= containsVariableReference(stmts[initi], in), ++initi) { SgExprStatement* initExprStmt = isSgExprStatement(stmts[initi]); if (initExprStmt) { SgExpression* top = initExprStmt->get_expression(); if (isSgAssignOp(top)) { SgVarRefExp* vr = isSgVarRefExp(isSgAssignOp(top)->get_lhs_operand()); ROSE_ASSERT(isSgAssignOp(top) != NULL); SgExpression* newinit = isSgAssignOp(top)->get_rhs_operand(); if (!used && vr && vr->get_symbol()->get_declaration() == in) { ROSE_ASSERT(newinit != NULL); // printf ("MoveDeclarationsToFirstUseVisitor::visit(): newinit = %p = %s \n",newinit,newinit->class_name().c_str()); ROSE_ASSERT(newinit->get_type() != NULL); SgAssignInitializer* i = new SgAssignInitializer(SgNULL_FILE,newinit,newinit->get_type()); i->set_endOfConstruct(SgNULL_FILE); // printf ("Built a SgAssignInitializer #1 \n"); vars[vari]->set_initializer(i); stmts[initi] = decl; newinit->set_parent(i); // DQ (6/23/2006): Set the parent and file_info pointers // printf ("Setting parent of i = %p = %s to parent = %p = %s \n",i,i->class_name().c_str(),in,in->class_name().c_str()); i->set_parent(in); ROSE_ASSERT(i->get_parent() != NULL); i->set_file_info(new Sg_File_Info(*(newinit->get_file_info()))); ROSE_ASSERT(i->get_file_info() != NULL); // Assumes only one var per declaration FIXME ROSE_ASSERT (vars.size() == 1); stmts.erase(stmts.begin() + decli); --decli; // To counteract ++decli in loop header break; // To get out of initi loop } } } } } } } } } }
InheritedAttribute BugSeeding::evaluateInheritedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute ) { // Use this if we only want to seed bugs in loops bool isLoop = inheritedAttribute.isLoop || (isSgForStatement(astNode) != NULL) || (isSgWhileStmt(astNode) != NULL) || (isSgDoWhileStmt(astNode) != NULL); // Add Fortran support isLoop = isLoop || (isSgFortranDo(astNode) != NULL); // Mark future noes in this subtree as being part of a loop inheritedAttribute.isLoop = isLoop; // To test this on simple codes, optionally allow it to be applied everywhere bool applyEveryWhere = true; if (isLoop == true || applyEveryWhere == true) { // The inherited attribute is true iff we are inside a loop and this is a SgPntrArrRefExp. SgPntrArrRefExp *arrayReference = isSgPntrArrRefExp(astNode); if (arrayReference != NULL) { // Mark as a vulnerability inheritedAttribute.isVulnerability = true; // Now change the array index (to seed the buffer overflow bug) SgVarRefExp* arrayVarRef = isSgVarRefExp(arrayReference->get_lhs_operand()); ROSE_ASSERT(arrayVarRef != NULL); ROSE_ASSERT(arrayVarRef->get_symbol() != NULL); SgInitializedName* arrayName = isSgInitializedName(arrayVarRef->get_symbol()->get_declaration()); ROSE_ASSERT(arrayName != NULL); SgArrayType* arrayType = isSgArrayType(arrayName->get_type()); ROSE_ASSERT(arrayType != NULL); SgExpression* arraySize = arrayType->get_index(); SgTreeCopy copyHelp; // Make a copy of the expression used to hold the array size in the array declaration. SgExpression* arraySizeCopy = isSgExpression(arraySize->copy(copyHelp)); ROSE_ASSERT(arraySizeCopy != NULL); // This is the existing index expression SgExpression* indexExpression = arrayReference->get_rhs_operand(); ROSE_ASSERT(indexExpression != NULL); // Build a new expression: "array[n]" --> "array[n+arraySizeCopy]", where the arraySizeCopy is a size of "array" SgExpression* newIndexExpression = buildAddOp(indexExpression,arraySizeCopy); // Substitute the new expression for the old expression arrayReference->set_rhs_operand(newIndexExpression); } } return inheritedAttribute; }
// Propagate definitions of a variable to its uses. // Assumptions: var is only assigned at the top level of body // nothing var depends on is assigned within body // Very simple algorithm designed to only handle simplest cases void simpleUndoFiniteDifferencingOne(SgBasicBlock* body, SgExpression* var) { SgExpression* value = 0; SgStatementPtrList& stmts = body->get_statements(); vector<SgStatement*> stmts_to_remove; for (SgStatementPtrList::iterator i = stmts.begin(); i != stmts.end(); ++i) { // cout << "Next statement: value = " << (value ? value->unparseToString() : "(null)") << endl; // cout << (*i)->unparseToString() << endl; if (isSgExprStatement(*i) && isSgAssignOp(isSgExprStatement(*i)->get_expression())) { SgAssignOp* assignment = isSgAssignOp(isSgExprStatement(*i)->get_expression()); // cout << "In assignment statement " << assignment->unparseToString() << endl; if (value) replaceCopiesOfExpression(var, value, assignment->get_rhs_operand()); if (isSgVarRefExp(assignment->get_lhs_operand()) && isSgVarRefExp(var)) { SgVarRefExp* vr = isSgVarRefExp(assignment->get_lhs_operand()); if (vr->get_symbol()->get_declaration() == isSgVarRefExp(var)->get_symbol()->get_declaration()) { value = assignment->get_rhs_operand(); stmts_to_remove.push_back(*i); } } } else { if (value) replaceCopiesOfExpression(var, value, *i); } } for (vector<SgStatement*>::iterator i = stmts_to_remove.begin(); i != stmts_to_remove.end(); ++i) { stmts.erase(std::find(stmts.begin(), stmts.end(), *i)); } if (value) { // DQ (12/17/2006): Separate out the construction of the SgAssignOp from the SgExprStatement to support debugging and testing. // stmts.push_back(new SgExprStatement(SgNULL_FILE, new SgAssignOp(SgNULL_FILE, var, value))); var->set_lvalue(true); SgAssignOp* assignmentOperator = new SgAssignOp(SgNULL_FILE, var, value); var->set_parent(assignmentOperator); value->set_parent(assignmentOperator); printf ("In simpleUndoFiniteDifferencingOne(): assignmentOperator = %p \n",assignmentOperator); // DQ: Note that the parent of the SgExprStatement will be set in AST post-processing (or it should be). SgExprStatement* es = new SgExprStatement(SgNULL_FILE, assignmentOperator); assignmentOperator->set_parent(es); stmts.push_back(es); es->set_parent(body); } }
void HaloRefSearch::visit(SgNode * node) { switch (node->variantT()) { case V_SgVarRefExp: SgVarRefExp * var = isSgVarRefExp(node); if (var->get_symbol()->getAttribute("halo_attr") != NULL) { found = true; } break; } }
virtual void visit(SgNode* n) { if (isSgVarRefExp(n)) { SgVarRefExp* vr = isSgVarRefExp(n); assert (vr->get_symbol()); if (vr->get_symbol()->get_declaration() == initname) { if (inSimpleContext(vr) || !needSimpleContext) { SgTreeCopy tc; isSgExpression(n->get_parent())->replace_expression( vr, isSgExpression(initexpr->copy(tc))); } } } }
FortranOpSetDefinition::FortranOpSetDefinition (SgExprListExp * parameters) { /* * ====================================================== * Get name of OP_SET dimension variable * ====================================================== */ if (isSgPntrArrRefExp ( parameters->get_expressions ()[index_setCardinalityName]) != NULL) { dimensionName = isSgPntrArrRefExp ( parameters->get_expressions ()[index_setCardinalityName])->unparseToString (); } else { dimensionName = isSgVarRefExp ( parameters->get_expressions ()[index_setCardinalityName])->get_symbol ()->get_name ().getString (); } /* * ====================================================== * Get name of OP_SET * ====================================================== */ SgVarRefExp * opSetVariableReference; if (isSgDotExp (parameters->get_expressions ()[index_OpSetName]) != NULL) { opSetVariableReference = isSgVarRefExp (isSgDotExp ( parameters->get_expressions ()[index_OpSetName])->get_rhs_operand ()); } else { opSetVariableReference = isSgVarRefExp ( parameters->get_expressions ()[index_OpSetName]); } variableName = opSetVariableReference->get_symbol ()->get_name ().getString (); ROSE_ASSERT (dimensionName.empty () == false); ROSE_ASSERT (variableName.empty () == false); Debug::getInstance ()->debugMessage ("Found an OP_SET definition: '" + variableName + "'. Its dimension is contained in '" + dimensionName + "'", Debug::FUNCTION_LEVEL, __FILE__, __LINE__); }
/* * Create LHS of a loop dependent statement */ void createFirstLoop(SgExprStatement* exprStatement, ArrayAssignmentStatementQueryInheritedAttributeType & arrayAssignmentStatementQueryInheritedData, OperandDataBaseType & operandDataBase) { //transformArrayRefAPP(exprStatement, arrayAssignmentStatementQueryInheritedData, operandDataBase); // Change the LHS, it will the first PntrArrayRef SgScopeStatement* scope = exprStatement->get_scope(); // Figure out the dimensionality of the statement globally vector<SgExpression*> parameters; int maxNumberOfIndexOffsets = 6; // default value for A++/P++ arrays ROSE_ASSERT(arrayAssignmentStatementQueryInheritedData.arrayStatementDimensionDefined == TRUE); if (arrayAssignmentStatementQueryInheritedData.arrayStatementDimensionDefined == TRUE) { // The the globally computed array dimension from the arrayAssignmentStatementQueryInheritedData maxNumberOfIndexOffsets = arrayAssignmentStatementQueryInheritedData.arrayStatementDimension; } // Then we want the minimum of all the dimensions accesses (or is it the maximum?) for (int n = 0; n < maxNumberOfIndexOffsets; n++) { parameters.push_back(buildVarRefExp("_" + StringUtility::numberToString(n + 1), scope)); } vector<SgVarRefExp*> varRefList = querySubTree<SgVarRefExp> (exprStatement, V_SgVarRefExp); ROSE_ASSERT(varRefList.size() > 0); SgVarRefExp* lhsVarRefExp = (*varRefList.begin()); // LHS should be the first ref SgExpression* replaceExp; if(isFunctionParenthesisOperator(isSgFunctionCallExp(lhsVarRefExp->get_parent()->get_parent()))) { replaceExp = isSgFunctionCallExp(lhsVarRefExp->get_parent()->get_parent()); } else { replaceExp = lhsVarRefExp; } SgVarRefExp* newVarRefExp = buildVarRefExp("__T_pointer", scope); string functionName = "SC_"+lhsVarRefExp->get_symbol()->get_declaration()->get_name(); SgFunctionCallExp* functionCallExp = buildFunctionCallExp(functionName, buildIntType(), buildExprListExp(parameters), scope); SgPntrArrRefExp* pntrArrRefExp2 = buildPntrArrRefExp(newVarRefExp, functionCallExp); ROSE_ASSERT(replaceExp != NULL); replaceExpression(replaceExp, pntrArrRefExp2); return; }
/** * Matches assignment statements (including pointer association) */ void FortranAnalysis::visit(SgExprStatement * expr_stmt) { if (matchRegionAssignment(expr_stmt)) { SgBinaryOp * bin_op = isSgBinaryOp(expr_stmt->get_expression()); SgVarRefExp * var = isSgVarRefExp(bin_op->get_lhs_operand()); if (var == NULL) return; var->get_symbol()->setAttribute("halo_attr", new AstTextAttribute("HALO_VAR")); printf("FortranAnalysis:: adding halo attr to %s\n", var->get_symbol()->get_name().getString().c_str()); } else if (HaloRefSearch::findHaloRef(expr_stmt)) { expr_stmt->setAttribute("halo_ref", new AstTextAttribute("HAS_HALO_REF")); printf("FortranAnalysis:: adding halo attr to statement\n"); } }
/********************************************************** * 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 FortranAnalysis::visit(SgFunctionCallExp * fcall) { SgFunctionRefExp * fref = isSgFunctionRefExp(fcall->get_function()); if (fref != NULL) { SgExpressionPtrList::iterator it = fcall->get_args()->get_expressions().begin(); std::string name = fref->get_symbol()->get_name().getString(); if (name == "interior" && it != fcall->get_args()->get_expressions().end()) { SgVarRefExp * var = isSgVarRefExp(*it); SgSymbol * sym = var->get_symbol(); sym->setAttribute("halo_attr", new AstTextAttribute("HALO_VAR")); debug("SgFunctionCallExp: adding halo attribute to %s\n", sym->get_name().getString().c_str()); } } }
bool TaintAnalysis::magic_tainted(SgNode *node, FiniteVarsExprsProductLattice *prodLat) { if (isSgInitializedName(node)) { SgInitializedName *iname = isSgInitializedName(node); std::string vname = iname->get_name().getString(); TaintLattice *tlat = dynamic_cast<TaintLattice*>(prodLat->getVarLattice(varID(iname))); if (tlat && 0==vname.compare(0, 7, "TAINTED")) { bool modified = tlat->set_vertex(TaintLattice::VERTEX_TAINTED); if (debug) { *debug <<"TaintAnalysis::magic_tainted: lattice is magically " <<tlat->to_string() <<(modified?" (modified)":" (not modified)") <<"\n"; } return modified; } } else if (isSgVarRefExp(node)) { SgVarRefExp *vref = isSgVarRefExp(node); std::string vname = vref->get_symbol()->get_name().getString(); TaintLattice *tlat = dynamic_cast<TaintLattice*>(prodLat->getVarLattice(varID(vref))); if (tlat && 0==vname.compare(0, 7, "TAINTED")) { bool modified = tlat->set_vertex(TaintLattice::VERTEX_TAINTED); if (debug) { *debug <<"TaintAnalysis::magic_tainted: lattice is magically " <<tlat->to_string() <<(modified?" (modified)":" (not modified)") <<"\n"; } return modified; } } else if (isSgVariableDeclaration(node)) { SgVariableDeclaration *vdecl = isSgVariableDeclaration(node); const SgInitializedNamePtrList &inames = vdecl->get_variables(); for (size_t i=0; i<inames.size(); ++i) { std::string vname = inames[i]->get_name().getString(); TaintLattice *tlat = dynamic_cast<TaintLattice*>(prodLat->getVarLattice(varID(inames[i]))); if (tlat && 0==vname.compare(0, 7, "TAINTED")) { bool modified = tlat->set_vertex(TaintLattice::VERTEX_TAINTED); if (debug) { *debug <<"TaintAnalysis::magic_tainted: lattice is magically " <<tlat->to_string() <<(modified?" (modified)":" (not modified)") <<"\n"; } return modified; } } } return false; }
void BasicProgmemTransform::transformCharArrayInitialization(SgFunctionDeclaration *func) { /* * * Translates statements of the form: * char arr[n] = "some string"; to: * char arr[n]; * strcpy_P(arr, <progmem placeholder>); * */ Rose_STL_Container<SgNode *> initNames = NodeQuery::querySubTree(func, V_SgInitializedName); for(auto &item: initNames) { SgInitializedName *initName = isSgInitializedName(item); if(initName->get_initializer() == NULL) { continue; } SgVariableDeclaration * varDecl = isSgVariableDeclaration(initName->get_declaration()); if(varDecl == NULL) { continue; } SgAssignInitializer *assignInit = isSgAssignInitializer(initName->get_initializer()); if(assignInit == NULL) { continue; } SgType *type = initName->get_type(); SgType *eleType = SageInterface::getElementType(type); if(isSgArrayType(type) && eleType != NULL && isSgTypeChar(eleType)) { SgStringVal* strVal = isSgStringVal(assignInit->get_operand()); std::string str = strVal->get_value(); int arrSize = getDeclaredArraySize(isSgArrayType(type)); if(arrSize == 0) { //char arr[] = "something"; int size = str.length() + 1; SgArrayType *type = SageBuilder::buildArrayType(SageBuilder::buildCharType(), SageBuilder::buildIntVal(size)); initName->set_type(type); } varDecl->reset_initializer(NULL); SgVariableDeclaration *placeholder = getVariableDeclPlaceholderForString(str); SgVarRefExp *ref = SageBuilder::buildVarRefExp(placeholder); std::stringstream instr; instr << "\n strcpy_P(" << initName->get_name().getString(); instr << ", " << ref->get_symbol()->get_name().getString() << ");\n"; SageInterface::attachComment(varDecl, instr.str(), PreprocessingInfo::after); printf("transformed %s\n", initName->unparseToString().c_str()); } } }
std::set<varID> BasicProgmemTransform::getVarsBoundToNonPlaceholderPointers() { std::set<varID> results; Rose_STL_Container<SgNode *> varRefs = NodeQuery::querySubTree(project, V_SgVarRefExp); for(auto &ref: varRefs) { SgVarRefExp *var = isSgVarRefExp(ref); if(isFromLibrary(var->get_symbol()->get_declaration())){ continue; } varID varRef = SgExpr2Var(var); if(varRef.str().find(STRING_LITERAL_PREFIX) == std::string::npos) { std::set<varID> aliases = aliasAnalysis->getAliasesForVariableAtNode(var, varRef); if(aliases.size() == 0) { aliases.insert(varRef); } results.insert(aliases.begin(), aliases.end()); } } return results; }
// Same as doSubexpressionExpansion, but requires exactly one use of // initname, and this use must be in a simple context void doSubexpressionExpansionSmart(SgInitializedName* initname) { SgNode* root = initname->get_parent()->get_parent(); assert (root); int count = countVariableReferences(root, initname); // cout << "Initname " << initname->get_name().str() << " was used " << count << " time(s)" << endl; if (count != 1) return; bool doExpansion = true; SgVariableSymbol* initnameSym = new SgVariableSymbol(initname); SgVarRefExp* initnameVr = new SgVarRefExp(SgNULL_FILE, initnameSym); if (isPotentiallyModified(initnameVr, root)) { doExpansion = false; } delete initnameSym; initnameSym = NULL; initnameVr->set_symbol(NULL); delete initnameVr; initnameVr = NULL; if (doExpansion) { doSubexpressionExpansion(initname, true); } }
int main(int argc, char * argv[]) { SgProject *project = frontend (argc, argv); SgFunctionDeclaration* func_decl = SageInterface::findDeclarationStatement<SgFunctionDeclaration> (project, "foo", NULL, true); Rose_STL_Container<SgNode*> nodeList = NodeQuery::querySubTree(func_decl->get_definition(), V_SgVarRefExp); for (Rose_STL_Container<SgNode *>::iterator i = nodeList.begin(); i != nodeList.end(); i++) { SgVarRefExp *vRef = isSgVarRefExp((*i)); cout<<"varRefExp: "<< vRef->unparseToString()<<endl; } // We expect two references // from input_ompVariableCollecting.C if (nodeList.size() !=2) { cerr<<"Error. We should find exactly two variable references."<<endl; } ROSE_ASSERT (nodeList.size() ==2); return backend(project); }
bool FortranAnalysis::matchRegionAssignment(SgExprStatement * expr_stmt) { SgBinaryOp * bin_op = isSgBinaryOp(expr_stmt->get_expression()); if (bin_op == NULL) return false; SgFunctionCallExp * fcall = isSgFunctionCallExp(bin_op->get_rhs_operand()); if (fcall == NULL) return false; SgFunctionRefExp * fref = isSgFunctionRefExp(fcall->get_function()); if (fref == NULL) return false; SgExpressionPtrList::iterator it = fcall->get_args()->get_expressions().begin(); std::string name = fref->get_symbol()->get_name().getString(); if (name == "interior" && it != fcall->get_args()->get_expressions().end()) { SgVarRefExp * var = isSgVarRefExp(*it); if (var == NULL) return false; AstTextAttribute * attr = (AstTextAttribute *) var->get_symbol()->getAttribute("dummy_attr"); if (attr == NULL) return false; if (attr->toString() != "DUMMY_ARRAY_ARG") return false; } return true; }
foreach(SgAssignOp* op, assigns) { SgVarRefExp* var = isSgVarRefExp(op->get_lhs_operand()); SgInitializedName* decl = var->get_symbol()->get_declaration(); std::cout << "Found assignment to " << var->get_symbol()->get_name().str(); if (decl->get_protected_declaration()) { std::cout << ", which is protected."; } std::cout << "\t\t" << op->unparseToString(); std::cout << std::endl; SgNode* assign_scope = SageInterface::getScope(op); SgNode* var_scope = decl->get_scope(); if (SageInterface::isAncestor(var_scope, assign_scope)) { std::cout << "\tAnd both are in the same scope.\n"; } else { std::cout << "\tIn different scopes.\n"; } }
Rose_STL_Container<SgVarRefExp*> buildListOfVariableReferenceExpressionsUsingGlobalVariables ( SgNode* node ) { // This function builds a list of "uses" of variables (SgVarRefExp IR nodes) within the AST. // return variable Rose_STL_Container<SgVarRefExp*> globalVariableUseList; // list of all variables (then select out the global variables by testing the scope) Rose_STL_Container<SgNode*> nodeList = NodeQuery::querySubTree ( node, V_SgVarRefExp ); Rose_STL_Container<SgNode*>::iterator i = nodeList.begin(); while(i != nodeList.end()) { SgVarRefExp *variableReferenceExpression = isSgVarRefExp(*i); assert(variableReferenceExpression != NULL); assert(variableReferenceExpression->get_symbol() != NULL); assert(variableReferenceExpression->get_symbol()->get_declaration() != NULL); assert(variableReferenceExpression->get_symbol()->get_declaration()->get_scope() != NULL); // Note that variableReferenceExpression->get_symbol()->get_declaration() returns the // SgInitializedName (not the SgVariableDeclaration where it was declared)! SgInitializedName* variable = variableReferenceExpression->get_symbol()->get_declaration(); SgScopeStatement* variableScope = variable->get_scope(); // Check if this is a variable declared in global scope, if so, then save it if (isSgGlobal(variableScope) != NULL) { globalVariableUseList.push_back(variableReferenceExpression); } i++; } return globalVariableUseList; }