#include <Metric.h>
Definition at line 196 of file Metric.h.
Public Member Functions | |
NumberSunkDiffuseInputs () | |
NumberSunkDiffuseInputs (const Ast *astPtr, const std::string &defASTStr, const std::list< std::string > &sinks) | |
virtual | ~NumberSunkDiffuseInputs () |
virtual MetricResult | operator() (const tree< AstNode > &, const MapClasses *classes=0, const MapVariables *vars=0, const MapFunctions *func=0, const MapAssignments *assigns=0, const MapVarEquivalent *equiv=0) |
Data Fields | |
Ast | defAST |
const Ast * | ast |
std::list< std::string > | sensitive |
NumberSunkDiffuseInputs::NumberSunkDiffuseInputs | ( | ) | [inline] |
NumberSunkDiffuseInputs::NumberSunkDiffuseInputs | ( | const Ast * | astPtr, | |
const std::string & | defASTStr, | |||
const std::list< std::string > & | sinks | |||
) | [inline] |
virtual NumberSunkDiffuseInputs::~NumberSunkDiffuseInputs | ( | ) | [inline, virtual] |
MetricResult NumberSunkDiffuseInputs::operator() | ( | const tree< AstNode > & | tr, | |
const MapClasses * | classes = 0 , |
|||
const MapVariables * | vars = 0 , |
|||
const MapFunctions * | func = 0 , |
|||
const MapAssignments * | assigns = 0 , |
|||
const MapVarEquivalent * | equiv = 0 | |||
) | [virtual] |
Implements Metric.
Definition at line 302 of file Metric.cpp.
References ast, tree< T, tree_node_allocator >::begin(), tree< T, tree_node_allocator >::begin_leaf(), tree< T, tree_node_allocator >::child(), defAST, tree< T, tree_node_allocator >::end(), tree< T, tree_node_allocator >::end_leaf(), Ast::getBoxedFunctions(), Ast::getSubVariables(), Ast::getTreeConstPtr(), MetricResult::main, tree< T, tree_node_allocator >::number_of_children(), tree< T, tree_node_allocator >::parent(), sensitive, and utils::start_with().
00304 { 00305 MetricResult result; 00306 unsigned num = 0; 00307 00308 // Get the pointer on the tree with input entry defintion 00309 list<string> scanned, varsList; 00310 list<VarBase> varBaseList; 00311 string temp,fullvarname; 00312 const tree<AstNode>* def = defAST.getTreeConstPtr(); 00313 00314 std::map<std::string, BoxedFunction> boxedFunctions = ast->getBoxedFunctions(); 00315 for (std::map<std::string, BoxedFunction>::iterator iter=boxedFunctions.begin();iter!=boxedFunctions.end();++iter) 00316 { 00317 // iterate through the boxedFunctions output 00318 bool add = false; 00319 for(MapFunctions::iterator jter=iter->second.output.begin();jter!=iter->second.output.end();++jter) { 00320 if (find(sensitive.begin(), sensitive.end(), jter->first) != sensitive.end()) { 00321 add = true; 00322 cout << "Add this function into sensitive sinks: '" << iter->first << "' because of -- " << jter->first <<endl; 00323 break; 00324 } 00325 } 00326 if (add) { 00327 if (find(sensitive.begin(), sensitive.end(), iter->first) == sensitive.end()) 00328 sensitive.push_back(iter->first); 00329 } 00330 } 00331 00332 // Go through all variables 00333 for(MapVariables::const_iterator iter=vars->begin(); iter!=vars->end();++iter) 00334 { 00335 for (AstVarBaseList::const_iterator jter=iter->second.begin();jter!=iter->second.end();++jter) 00336 { 00337 temp = jter->name; 00338 fullvarname = jter->name; 00339 if (jter->index.length() > 0) { 00340 fullvarname += ('[' + jter->index + ']'); 00341 } 00342 for (tree<AstNode>::leaf_iterator lter=def->begin_leaf();lter!=def->end_leaf();++lter) 00343 { 00344 if (lter->getValue() == temp) { 00345 if (find(scanned.begin(), scanned.end(), fullvarname) == scanned.end()) { 00346 // Am I looking for an array somewhere? 00347 tree<AstNode>::iterator tter=tr.parent(tr.begin(jter->position)); 00348 //if (tr.number_of_children(tter) > 1) 00349 // num++; 00350 scanned.push_back(fullvarname); 00351 varsList.push_back(fullvarname); 00352 varBaseList.push_back(*jter); 00353 } 00354 } 00355 } 00356 } 00357 } 00358 00359 // Then, add the assignments variables recursivly of the scanned listen 00360 string asname, bsname; 00361 00362 list<string> curVars, forbidden; 00363 00364 for (list<string>::const_iterator jter=varsList.begin(); jter!=varsList.end();++jter) 00365 if (find(curVars.begin(),curVars.end(),*jter) == curVars.end()) 00366 curVars.push_back(*jter); 00367 while(curVars.size() > 0) 00368 { 00369 // take the first element on the stack 00370 string elmt = *(curVars.begin()); 00371 // look at all assignement 00372 for (MapAssignments::const_iterator iter = assigns->begin(); iter != assigns->end(); ++iter) 00373 { 00374 asname = iter->first.name; 00375 if (iter->first.index.length() > 0) { 00376 asname += ('[' + iter->first.index + ']'); 00377 } 00378 bsname = iter->second.name; 00379 if (iter->second.index.length() > 0) { 00380 bsname += ('[' + iter->second.index + ']'); 00381 } 00382 if (bsname == elmt && find(forbidden.begin(),forbidden.end(),asname) == forbidden.end()) { 00383 if (find(curVars.begin(),curVars.end(),asname) == curVars.end()) 00384 curVars.push_back(asname); 00385 if (find(varsList.begin(),varsList.end(),asname) == varsList.end()) { 00386 varsList.push_back(asname); 00387 varBaseList.push_back(iter->first); 00388 } 00389 } 00390 } 00391 curVars.pop_front(); 00392 forbidden.push_back(elmt); 00393 } 00394 00395 // What about the equivalences now? 00396 for (MapVarEquivalent::const_iterator iter = equiv->begin(); iter != equiv->end(); ++iter) 00397 { 00398 asname = iter->first.name; 00399 if (iter->first.index.length() > 0) { 00400 asname += ('[' + iter->first.index + ']'); 00401 } 00402 // does the variable has an equivalence? 00403 for (AstVarBaseList::const_iterator jter=iter->second.begin(); jter!=iter->second.end();++jter) { 00404 bsname = jter->name; 00405 if (jter->index.length() > 0) { 00406 bsname += ('[' + jter->index + ']'); 00407 } 00408 00409 if (find(varsList.begin(),varsList.end(),bsname) != varsList.end()) { 00410 if (find(varsList.begin(), varsList.end(), asname) == varsList.end()){ 00411 //num++; 00412 varsList.push_back(asname); 00413 varBaseList.push_back(iter->first); 00414 } 00415 } 00416 } 00417 } 00418 // So, what do we have in the varsList? 00419 cerr << endl << "####################" << endl << "List of variables:" << endl; 00420 for(list<string>::const_iterator k=varsList.begin();k!=varsList.end();++k) 00421 cerr << *k << ','; 00422 cerr << endl << "####################" << endl << endl; 00423 00424 string type,function; 00425 tree<AstNode>::iterator child, parent; 00426 // Now, for now, for all variables, look if there are called in sensitive sinks 00427 for (tree<AstNode>::iterator iter=tr.begin();iter!=tr.end();++iter) 00428 { 00429 type = iter->getType(); 00430 // 00431 if (type == "function_call" 00432 || (utils::start_with(type, "T_") && (type != "T_CONSTANT_ENCAPSED_STRING" && type != "T_VARIABLE" && type != "T_STRING") )) { 00433 // we have a possible position 00434 if (type == "function_call") 00435 { 00436 // get the T_STRING value 00437 for (tree<AstNode>::iterator jter=iter;jter!=tr.end(iter);++jter) { 00438 if (jter->getType() == "T_STRING") { 00439 // gotcha! 00440 child = jter; 00441 break; 00442 } 00443 } 00444 // get the function name 00445 if (tr.number_of_children(child) == 1) { 00446 function = tr.child(child,0)->getValue(); 00447 if (find(sensitive.begin(),sensitive.end(),function) != sensitive.end()) 00448 { 00449 // we now have a sensitive sinks, let's look in the parameters 00450 list<VarBase> subVariables= ast->getSubVariables(iter); 00451 // print subvariable's 00452 //cerr << function << '('; 00453 //for (list<VarBase>::const_iterator vter=subVariables.begin();vter!=subVariables.end();++vter) 00454 // cerr << *vter << ','; 00455 //cerr << ')' << endl; 00456 00457 for (list<VarBase>::const_iterator vter=subVariables.begin();vter!=subVariables.end();++vter) 00458 { 00459 string tname = vter->name; 00460 if (vter->index.length() > 0) { 00461 tname += ('[' + vter->index + ']'); 00462 } 00463 if (find(varsList.begin(), varsList.end(), tname) != varsList.end()) { 00464 // found one! 00465 num++; 00466 cerr << "(function_call) Sink |Diffuse input : " << function << " (" << tname << ")" << endl; 00467 break; 00468 } 00469 } 00470 } 00471 } 00472 // skipping... 00473 iter = child; 00474 } 00475 else 00476 { 00477 function = ""; 00478 // get name of the T_ call ? die, echo, print, assert ? 00479 tree<AstNode>::iterator jter = tr.begin(iter); 00480 for (;jter!=tr.end(iter);++jter) { 00481 if (jter->getType() == "text") {// gotcha! 00482 function = jter->getValue(); 00483 break; 00484 } 00485 } 00486 if (find(sensitive.begin(),sensitive.end(),function) != sensitive.end()) 00487 { 00488 00489 // we now have a sensitive sinks, let's look in the parameters 00490 list<VarBase> subVariables= ast->getSubVariables(tr.parent(iter)); 00491 // print subvariable's 00492 //cerr << function << '('; 00493 //for (list<VarBase>::const_iterator vter=subVariables.begin();vter!=subVariables.end();++vter) 00494 // cerr << *vter << ','; 00495 //cerr << ')' << endl; 00496 00497 for (list<VarBase>::const_iterator vter=subVariables.begin();vter!=subVariables.end();++vter) 00498 { 00499 string tname = vter->name; 00500 if (vter->index.length() > 0) { 00501 tname += ('[' + vter->index + ']'); 00502 } 00503 //cerr << tname << endl; 00504 if (find(varsList.begin(), varsList.end(), tname) != varsList.end()) { 00505 // found one! 00506 num++; 00507 cerr << "(language_dependant) Sink |Diffuse input : " << function << " | " << tname << endl; 00508 break; 00509 } 00510 else 00511 { 00512 // maybe we can count it. 00513 // echo $array[$i] in a loop or with the index 00514 if (vter->index.length() > 0) 00515 { 00516 // look at if there is a var 00517 for (list<VarBase>::const_iterator bter=varBaseList.begin();bter!=varBaseList.end();++bter) 00518 { 00519 if (bter->name == vter->name && find(varsList.begin(), varsList.end(), vter->index) == varsList.end()) { 00520 // if the index is a variable? 00521 if (vter->index[0] == '$') { 00522 num++; 00523 cerr << "(language_dependant) Sink |Diffuse input : " << function << " | " << tname << endl; 00524 } 00525 break; 00526 } 00527 } 00528 } 00529 } 00530 } 00531 } 00532 // skipping... 00533 iter = jter; 00534 } 00535 } 00536 } 00537 00538 00539 00540 result.main = num; 00541 return result; 00542 }
const Ast* NumberSunkDiffuseInputs::ast |
std::list<std::string> NumberSunkDiffuseInputs::sensitive |