123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143(**************************************************************************)(* *)(* SPDX-License-Identifier LGPL-2.1 *)(* Copyright (C) *)(* CEA (Commissariat à l'énergie atomique et aux énergies alternatives) *)(* *)(**************************************************************************)openServerletpackage=Package.package~plugin:"slicing"~name:"slicing"~title:"Slicing"()(* ----- Slicing functions -------------------------------------------------- *)letmk_selectionfselect=fselectApi.Select.empty_selects~spare:falseletkf_of_varinfovi=tryGlobals.Functions.getviwithNot_found->Data.failure"%a is not a function"Printer.pp_varinfoviletselect_kf_auxfselectmarker=match(marker:Printer_tag.localizable)with|PVDecl(_,Kglobal,vi)|PLval(_,_,(Varvi,NoOffset))->mk_selectionfselect(kf_of_varinfovi)|marker->Data.failure"Marker %a is not a function"Printer_tag.pp_localizablemarkerletselect_calls_to=select_kf_auxApi.Select.select_func_calls_toletselect_calls_into=select_kf_auxApi.Select.select_func_calls_intoletselect_result=select_kf_auxApi.Select.select_func_return(* ----- Slicing statements ------------------------------------------------- *)letselect_stmt_auxfselectmarker=letkinstr=Printer_tag.ki_of_localizablemarkerinletkf=Printer_tag.kf_of_localizablemarkerinmatchkf,kinstrwith|Somekf,Kstmtstmt->mk_selectionfselectstmtkf|_->Data.failure"No statement related to marker %a"Printer_tag.pp_localizablemarkerletselect_stmt=select_stmt_auxApi.Select.select_stmtletselect_stmt_control=select_stmt_auxApi.Select.select_stmt_ctrl(* ----- Slicing lvalues ---------------------------------------------------- *)letlval_of_marker=function|Printer_tag.PLval(Somekf,Kstmtstmt,lval)->(* For dubious reasons, Api.Select requires strings instead of the lvalue.
Thus, we convert the lval into string, so that it may be parsed back… *)letlval_str=Pretty_utils.to_stringPrinter.pp_lvallvalinletlval_str_set=Datatype.String.Set.singletonlval_strin(kf,stmt,lval_str_set)|marker->Data.failure"Marker %a is not an lvalue"Printer_tag.pp_localizablemarkerletmk_selection_lvalfselect=letpdg_mark=Api.Mark.make~ctrl:true~addr:true~data:trueinfselectApi.Select.empty_selectspdg_markletselect_lvalmarker=letkf,stmt,lval=lval_of_markermarkerinmk_selection_lvalApi.Select.select_stmt_lvallval~before:truestmt~eval:stmtkfletempty=Datatype.String.Set.emptyletselect_lval_readsmarker=letkf,stmt,lval=lval_of_markermarkerinmk_selection_lvalApi.Select.select_func_lval_rw~rd:lval~wr:empty~eval:stmtkfletselect_lval_writesmarker=letkf,stmt,lval=lval_of_markermarkerinmk_selection_lvalApi.Select.select_func_lval_rw~rd:empty~wr:lval~eval:stmtkf(* ----- Slicing requests --------------------------------------------------- *)letmk_slicebuild_selection=funmarker->letselection=build_selectionmarkerinApi.Project.reset_slicing();Api.Request.add_persistent_selectionselection;Api.Request.apply_all_internal();ifSlicingParameters.Mode.Callers.get()thenApi.Slice.remove_uncalled();letproject_name=SlicingParameters.ProjectName.get()inletsuffix=SlicingParameters.ExportedProjectPostfix.get()inletproject=Api.Project.extract(project_name^suffix)inproject.name,project.pidmoduleOutput=Data.Jpair(Data.Jstring)(Data.Jint)(* All requests below are EXEC requests from an AST marker to the name and id
of the new project containing the sliced AST. *)letregister_request~name~descrselect=Request.register~package~kind:`EXEC~name~descr:(Markdown.plaindescr)~input:(moduleKernel_ast.Marker)~output:(moduleOutput)(mk_sliceselect)let()=register_request~name:"sliceCallsTo"~descr:"Slice effects of the given function"select_calls_tolet()=register_request~name:"sliceCallsInto"~descr:"Slice entrance into the given function"select_calls_intolet()=register_request~name:"sliceResult"~descr:"Slice the returned value of the given function"select_resultlet()=register_request~name:"sliceStmt"~descr:"Slice effects of the given statement"select_stmtlet()=register_request~name:"sliceStmtCtrl"~descr:"Slice accessibility of the given statement"select_stmt_controllet()=register_request~name:"sliceLval"~descr:"Slice the given lvalue"select_lvallet()=register_request~name:"sliceLvalReads"~descr:"Slice read accesses of the given lvalue"select_lval_readslet()=register_request~name:"sliceLvalWrites"~descr:"Slice write accesses of the given lvalue"select_lval_writes