123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427openCilopenCabs2cilletis_equal_funname_funidvarinfonameid=ifString.comparevarinfo.vnamename=0||varinfo.vid=idthentrueelsefalseletrecdelete_elemlists=matchlistwith|x::xs->ifString.comparexs=0thendelete_elemxsselsex::delete_elemxss|[]->[]letrecdelete_duplicateslistacc=matchlistwith|x::xs->delete_duplicates(delete_elemxsx)(x::acc)|[]->accletmap_gfunf=functionGFun(dec,loc)->fdecloc|_->Noneletfind_all_with_orignamen=letns=Hashtbl.find_allenvironmentninBatList.filter_map(function|(EnvVarv,_)->Somev.vname|_->None)nsclassfun_find_returnsfunnamefunidresult:nopCilVisitor=objectinheritnopCilVisitormethod!vfuncfundec=ifis_equal_funname_funidfundec.svarfunnamefunidthenDoChildrenelseSkipChildrenmethod!vstmtstmt=matchstmt.skindwith|Return(Someexp,loc)->result:=!result@[("",loc,String.trim(Pretty.sprint~width:1(d_type()(typeOfexp))),-1);];DoChildren|Return(None,loc)->result:=!result@[("",loc,"void",-1)];DoChildren|_->DoChildrenend(* Finds all returns of a function *)letfind_returnsfunnamefunidfile=letresult=ref[]inletvisitor=newfun_find_returnsfunnamefunidresultinignore(visitCilFileSameGlobalsvisitorfile);!result(* Finds all returns in all functions *)letfind_returns_allfile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_returns""fundec.svar.vidfile)))file.globalsclassfun_find_sigfunnamefunidresult:nopCilVisitor=objectinheritnopCilVisitormethod!vfuncfundec=ifis_equal_funname_funidfundec.svarfunnamefunidthenDoChildrenelseSkipChildrenmethod!vstmtstmt=matchstmt.skindwith|Return(Someexp,loc)->result:=!result@[("",loc,String.trim(Pretty.sprint~width:1(d_type()(typeOfexp))),-1);];SkipChildren|Return(None,loc)->result:=!result@[("",loc,"void",-1)];SkipChildren|_->DoChildrenendletcreate_sigfundecfile=letresult=ref[]inletreturn_type=matchvisitCilFileSameGlobals(newfun_find_sigfundec.svar.vnamefundec.svar.vidresult)file;!resultwith|(_,_,typ,_)::_->typ|[]->Printf.printf"This should never happen\n";""inletrecinput_typelist=matchlistwith|[x]->String.trim(Pretty.sprint~width:1(d_type()x.vtype))^" "^x.vname|x::xs->String.trim(Pretty.sprint~width:1(d_type()x.vtype))^" "^x.vname^", "^input_typexs|[]->""inreturn_type^" "^fundec.svar.vname^" ("^input_typefundec.sformals^")"(* Finds all definitions of a function *)letfind_deffunnamefunidfile=letfnfundecloc=ifis_equal_funname_funidfundec.svarfunnamefunidthenSome(fundec.svar.vname,loc,create_sigfundecfile,fundec.svar.vid)elseNoneinBatList.filter_map(map_gfunfn)file.globals(* Finds all definitions of all functions *)letfind_def_allfile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_def""fundec.svar.vidfile)))file.globalsletfind_fundecfunnamefunidlist=letgfun=BatList.find_opt(funx->matchxwith|GFun(dec,_)->is_equal_funname_funiddec.svarfunnamefunid|_->false)listinmatchgfunwithSome(GFun(dec,_))->Somedec|_->Noneclassfun_find_usesfunnamefunidfileresult:nopCilVisitor=objectinheritnopCilVisitormethod!vinstinstr=matchinstrwith|Call(_,Lval(Varvarinfo,NoOffset),_,loc)->ifis_equal_funname_funidvarinfofunnamefunidthen(matchfind_fundecfunnamefunidfile.globalswith|None->SkipChildren|Somedec->result:=!result@[(varinfo.vname,loc,create_sigdecfile,varinfo.vid)];SkipChildren)elseSkipChildren|_->SkipChildrenend(* Finds all calls of a function in all functions *)letfind_usesfunnamefunidfile=letresult=ref[]inletvisitor=newfun_find_usesfunnamefunidfileresultinignore(visitCilFileSameGlobalsvisitorfile);!result(* Find all calls of all functions in all functions *)letfind_uses_allfile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_uses""fundec.svar.vidfile)))file.globalsclassfun_find_uses_in_funfunnamefunidfunstrucnamefileresult:nopCilVisitor=objectinheritnopCilVisitormethod!vfuncfundec=ifis_equal_funname_funidfundec.svarfunstrucname(-1)thenDoChildrenelseSkipChildrenmethod!vinstinstr=matchinstrwith|Call(_,Lval(Varvarinfo,NoOffset),_,loc)->ifis_equal_funname_funidvarinfofunnamefunidthen(matchfind_fundecfunnamefunidfile.globalswith|None->SkipChildren|Somedec->result:=!result@[(varinfo.vname,loc,create_sigdecfile,varinfo.vid)];SkipChildren)elseSkipChildren|_->SkipChildrenend(* Finds calls of a function in a function *)letfind_uses_in_funfunnamefunidfunstrucnamefile=letresult=ref[]inletvisitor=newfun_find_uses_in_funfunnamefunidfunstrucnamefileresultinignore(visitCilFileSameGlobalsvisitorfile);!result(* Finds all calls of all functions in a function *)letfind_uses_in_fun_allfunstrucnamefile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_uses_in_fun""fundec.svar.vidfunstrucnamefile)))file.globalsletloc_default={line=-1;file="";byte=-1;column=-1}classfun_find_usesvar_in_funfundecfunstrucnamevarnamevaridfileresult:nopCilVisitor=objectinheritnopCilVisitormethod!vfuncdec=ifis_equal_funname_funiddec.svarfunstrucname(-1)thenDoChildrenelseSkipChildrenmethod!vinstinstr=matchinstrwith|Call(_,exp,list,loc)->(matchexpwith|Lval(Varvarinfo,_)->ifis_equal_funname_funidvarinfofundec.svar.vnamefundec.svar.vidthenifList.length(FuncVar.search_expression_listlistvarnameloc_defaultvaridtrue)>0then(result:=!result@[(varinfo.vname,loc,create_sigfundecfile,varinfo.vid);];SkipChildren)elseSkipChildrenelseSkipChildren|_->SkipChildren)|_->SkipChildrenend(* Finds calls of a function with a var in argument in a function *)letfind_usesvar_in_funfunnamefunidfunstrucnamevarnamefile=matchfind_fundecfunnamefunidfile.globalswith|None->[]|Somefundec->letresult=ref[]inletdedup=delete_duplicates(find_all_with_orignamevarname)[]inList.iter(funx->visitCilFileSameGlobals(newfun_find_usesvar_in_funfundecfunstrucnamex(-1)fileresult)file)dedup;!result(* Finds calls of all function with a var in argument in a function *)letfind_usesvar_in_fun_allfunstrucnamevarnamefile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_usesvar_in_fun""fundec.svar.vidfunstrucnamevarnamefile)))file.globals(* Finds all calls of a function with a var in argument in all functions *)letfind_usesvarfunnamefunidvarnamefile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_usesvar_in_funfunnamefunidfundec.svar.vnamevarnamefile)))file.globals(* Finds all calls of all functions with a var in argument in all functions *)letfind_usesvar_allvarnamefile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_usesvar""fundec.svar.vidvarnamefile)))file.globalsletis_temporaryid=Inthash.memallTempVarsidclassfind_calls_with_tmpresultfunnamefunid:nopCilVisitor=objectinheritnopCilVisitormethod!vinstinstr=matchinstrwith|Call(lval_opt,Lval(Varvarinfo,_),_,_)->ifis_equal_funname_funidvarinfofunnamefunidthenmatchlval_optwith|Some(Vartmpinfo,_)->ifis_temporarytmpinfo.vidthenresult:=!result@[(tmpinfo.vid,varinfo.vid)];SkipChildren|_->SkipChildrenelseSkipChildren|_->SkipChildrenendletfind_lval_of_callsfunnamefunidfile=letresult=ref[]inletvisitor=newfind_calls_with_tmpresultfunnamefunidinvisitCilFileSameGlobalsvisitorfile;!resultletcreate_fun_resnameidfileloc=letfundec_opt=find_fundecnameidfile.globalsinmatchfundec_optwith|None->("",loc_default,"",-1)|Somefundec->(fundec.svar.vname,loc,create_sigfundecfile,fundec.svar.vid)(* Finds all calls of a function in a condition in all functions *)letfind_uses_condfunnamefunidfile=letid_list=find_lval_of_callsfunnamefunidfileinBatList.filter_map(fun(tmp,func)->matchFuncVar.find_uses_in_cond""tmpfiletruewith|(_,loc,_,_)::_->Some(create_fun_res""funcfileloc)|_->None)id_list(* Finds all calls of all functions in a condition in all functions *)letfind_uses_cond_allfile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_uses_cond""fundec.svar.vidfile)))file.globals(* Finds calls of a function in non-condition in all functions *)letfind_uses_noncondfunnamefunidfile=letuses_cond=find_uses_condfunnamefunidfileinletall_uses=find_usesfunnamefunidfileinList.filter(funx->not(List.memxuses_cond))all_uses(* Finds calls of all functions in non-condition in all functions *)letfind_uses_noncond_allfile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_uses_noncond""fundec.svar.vidfile)))file.globalsclassfind_calls_usesvar_with_tmpresultfunnamefunidvarname:nopCilVisitor=objectinheritnopCilVisitormethod!vinstinstr=matchinstrwith|Call(lval_opt,Lval(Varvarinfo,_),arg_list,loc)->ifis_equal_funname_funidvarinfofunnamefunid&&List.length(List.flatten(List.map(funx->FuncVar.search_expression_listarg_listxloc(-1)true)(find_all_with_orignamevarname)))>0thenmatchlval_optwith|Some(Vartmpinfo,_)->ifString.lengthtmpinfo.vname>2&&String.compare"tmp"(String.subtmpinfo.vname03)=0thenresult:=!result@[(tmpinfo.vid,varinfo.vid)];SkipChildren|_->SkipChildrenelseSkipChildren|_->SkipChildrenendletfind_lval_of_calls_usesvarfunnamefunidvarnamefile=letresult=ref[]inletvisitor=newfind_calls_usesvar_with_tmpresultfunnamefunidvarnameinvisitCilFileSameGlobalsvisitorfile;!result(* Finds calls of a function with a variable as argument in conditions *)letfind_usesvar_condfunnamefunidvarnamefile=letid_list=find_lval_of_calls_usesvarfunnamefunidvarnamefileinBatList.filter_map(fun(tmp,func)->matchFuncVar.find_uses_in_cond""tmpfiletruewith|(_,loc,_,_)::_->Some(create_fun_res""funcfileloc)|_->None)id_list(* Finds calls of all functions with a variable as argument in conditions *)letfind_usesvar_cond_allvarnamefile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_usesvar_cond""fundec.svar.vidvarnamefile)))file.globals(* Finds calls of a function with a variable as argument in non-conditions *)letfind_usesvar_noncondfunnamefunidvarnamefile=letuses_cond=find_usesvar_condfunnamefunidvarnamefileinletall_uses=find_usesvarfunnamefunidvarnamefileinList.filter(funx->not(List.memxuses_cond))all_uses(* Finds calls of all functions with a variable as argument in non-conditions *)letfind_usesvar_noncond_allvarnamefile=List.flatten@@BatList.filter_map(map_gfun(funfundec_->Some(find_usesvar_noncond""fundec.svar.vidvarnamefile)))file.globals