/* * Fix OP function calls and inject debug names */ void OPSource::fixOpStructs(SgNode *n) { SgInitializedName* initname = isSgInitializedName(n); if(initname) { string var_name = initname->get_name().getString(); SgConstructorInitializer *initer = isSgConstructorInitializer(initname->get_initializer()); if(initer) { string class_name = initer->get_class_decl()->get_name().getString(); if(class_name.find("op_dat") != string::npos || class_name.find("op_dat_gbl") != string::npos || class_name.compare("_op_ptr") == 0 || class_name.compare("_op_set") == 0 || class_name.compare("_op_dat_const") == 0) { cout << "---Injecting Debug Name: " << var_name << "---" << endl; SgExprListExp* list = initer->get_args(); SgExpressionPtrList &exprs = list->get_expressions(); if( isSgStringVal(exprs.back()) == NULL ) { list->append_expression(buildStringVal(var_name)); } } } } }
/* * 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)); } } } }
/** * Beware of multiple environment variables with the same effective name */ bool ENV02_C( const SgNode *node ) { static std::set<std::string> origStrs; static std::set<std::string> normStrs; static std::map<std::string, const SgFunctionRefExp *> strToNode; const SgFunctionRefExp *fnRef = isSgFunctionRefExp(node); if (!fnRef) return false; if (!(isCallOfFunctionNamed(fnRef, "getenv") ||isCallOfFunctionNamed(fnRef, "setenv") ||isCallOfFunctionNamed(fnRef, "putenv"))) { return false; } const SgStringVal *strVal = isSgStringVal(removeImplicitPromotions(getFnArg(fnRef,0))); if (!strVal) return false; std::string str = strVal->get_value(); if (const unsigned int eq = str.find_first_of("=")) str = str.substr(0,eq); if (origStrs.find(str) != origStrs.end()) return false; origStrs.insert(str); std::transform(str.begin(), str.end(), str.begin(), tolower); if (normStrs.find(str) != normStrs.end()) { print_error(node, "ENV02-C", "Beware of multiple environment variables with the same effective name", true); print_error(strToNode[str], "ENV02-C", "Previous reference was here", true); return true; } normStrs.insert(str); strToNode[str] = fnRef; return false; }
void CompassAnalyses::VariableNameEqualsDatabaseName::Traversal:: visit(SgNode* node) { if( isSgAssignInitializer(node) != NULL ) assignExp = node; if( isSgAssignOp(node) != NULL ) assignExp = node; SgFunctionCallExp* funcCall = isSgFunctionCallExp(node); // See if we have a dot expression or arrow expression which // accesses the desired member function in the class we are looking for. if ( funcCall != NULL ) { SgExpression* funcExp = funcCall->get_function(); if ( ( isSgDotExp(funcExp) != NULL ) | ( isSgArrowExp(funcExp) != NULL ) ) { SgBinaryOp* binOp = isSgBinaryOp(funcExp); SgExpression* rhsOp = binOp->get_rhs_operand(); // SgExpression* lhsOp = binOp->get_lhs_operand(); if ( SgMemberFunctionRefExp* funcRef = isSgMemberFunctionRefExp(rhsOp) ) { // std::cout << "c1\n" ; SgMemberFunctionSymbol* funcSymbol = funcRef->get_symbol(); ROSE_ASSERT(funcSymbol->get_declaration() != NULL); // DQ (1/16/2008): Note that the defining declaration need not exist (see test2001_11.C) // ROSE_ASSERT(funcSymbol->get_declaration()->get_definingDeclaration() != NULL); if (funcSymbol->get_declaration()->get_definingDeclaration() != NULL) { SgMemberFunctionDeclaration* funcDecl = isSgMemberFunctionDeclaration(funcSymbol->get_declaration()->get_definingDeclaration()); ROSE_ASSERT( funcDecl != NULL ); SgClassDefinition* clDef = isSgClassDefinition(funcDecl->get_scope()); SgClassDeclaration* clDecl = isSgClassDeclaration(clDef->get_declaration()); // SgClassDeclaration* clDecl = funcDecl->get_associatedClassDeclaration(); ROSE_ASSERT( clDecl != NULL ); std::string className = clDecl->get_name().getString(); ROSE_ASSERT(funcDecl != NULL); std::string functionName = funcDecl->get_name().getString(); // If the class is the class we are looking for see if the member function // access is to the member function we are interested in. // std::cout << "className = " << className << std::endl; // std::cout << "functionName = " << functionName << std::endl; if ( (className == classToLookFor) && ( functionName == memberFunctionToLookFor ) ) { SgExprListExp* actualArgs = funcCall->get_args(); SgExpressionPtrList& actualExpArgs = actualArgs->get_expressions (); ROSE_ASSERT(actualExpArgs.size() == 1); Rose_STL_Container<SgNode*> nodeLst = NodeQuery::querySubTree(*actualExpArgs.begin(), V_SgStringVal); ROSE_ASSERT( nodeLst.size() > 0); SgStringVal* actualArg = isSgStringVal(*nodeLst.begin()); ROSE_ASSERT(actualArg != NULL); std::string stringArg = actualArg->get_value(); std::cout << "arg:" << stringArg << std::endl; std::string varName; // SgInitializedName* initName = NULL; if ( SgAssignInitializer* assignInit = isSgAssignInitializer(assignExp) ) { SgInitializedName* initName = isSgInitializedName(assignInit->get_parent()); ROSE_ASSERT(initName != NULL); varName = initName->get_name().getString(); } else { if ( SgAssignOp* assignOp = isSgAssignOp(assignExp) ) { SgExpression* lhsOp = assignOp->get_lhs_operand(); SgVarRefExp* varRef = isSgVarRefExp(lhsOp); ROSE_ASSERT(varRef!=NULL); SgVariableSymbol* varSymbol = varRef->get_symbol(); ROSE_ASSERT(varSymbol != NULL); SgInitializedName* initName = varSymbol->get_declaration(); varName = initName->get_name().getString(); } } if (varName != "") { // we are only interested in the part of the argument after the last ":" // Database scopes in ALE3D are separated by ":" size_t posCol = stringArg.find_last_of(':'); if (posCol != std::string::npos) stringArg = stringArg.substr(posCol+1); //Find violations to the rule if ( stringArg != varName) { output->addOutput(new CheckerOutput(assignExp)); std::cout << "violation" << varName << std::endl; } else { std::cout << "non=violation" << varName << std::endl; } } } } } } } } // End of the visit function.