<commit_before>(* epicenter.ml *)
(* code for epicenter.mli *)

(* module maintainer: scott *)
(* see copyright at end of this file *)

open Callgraph
open Cil
open Trace
open Pretty
module H = Hashtbl


let sliceFile (f:file) (epicenter:string) (maxHops:int) : unit =
  (* compute the static call graph *)
  let graph:callgraph = (computeGraph f) in

  (* will accumulate here the set of names of functions already seen *)
  let seen: (string, unit) H.t = (H.create 117) in

  (* when removing "unused" symbols, keep all seen functions *)
  let isRoot : global -> bool = function
    | GFun ({svar = {vname = vname}}, _) ->
	H.mem seen vname
    | _ ->
	false
  in

  (* recursive depth-first search through the call graph, finding
   * all nodes within 'hops' hops of 'node' and marking them to
   * to be retained *)
  let rec dfs (node:callnode) (hops:int) : unit =
    (* only recurse if we haven't already marked this node *)
    if not (H.mem seen node.cnInfo.vname) then
      begin
        (* add this node *)
	H.add seen node.cnInfo.vname ();
	trace "epicenter" (dprintf "will keep %s\n" node.cnInfo.vname);
	
        (* if we cannot do any more hops, stop *)
	if (hops > 0) then

          (* recurse on all the node's callers and callees *)
	  let recurse (adjName:string) (adjacent:callnode) : unit =
	    (dfs adjacent (hops - 1)) in
	  Hashtbl.iter recurse node.cnCallees;
	  Hashtbl.iter recurse node.cnCallers
      end
  in
  dfs (Hashtbl.find graph epicenter) maxHops;

  (* finally, throw away anything we haven't decided to keep *)
  Util.sliceGlobal := true;
  Rmtmps.removeUnusedTemps ~isRoot:isRoot f

let doEpicenter = ref false
let epicenterName = ref ""
let epicenterHops = ref 0

let feature : featureDescr = 
  { fd_name = "epicenter";
    fd_enabled = doEpicenter;
    fd_description = "generation of code to log function calls";
    fd_extraopt = 
    [
    ("--epicenter-name", 
     Arg.String (fun s -> epicenterName := s),
               "<name>: do an epicenter slice starting from function <name>");
    ("--epicenter-hops", Arg.Int (fun n -> epicenterHops := n),
     "<n>: specify max # of hops for epicenter slice");
    ];

    fd_doit =
    (fun f -> 
      sliceFile f !epicenterName !epicenterHops);

    fd_post_check = true;
  } 


(*
 *
 * Copyright (c) 2001-2002 by
 *  <NAME>	<EMAIL>
 *  <NAME>        <EMAIL>
 *  <NAME>          <EMAIL>
 *  <NAME>          <EMAIL>
 *
 * All rights reserved.  Permission to use, copy, modify and distribute
 * this software for research purposes only is hereby granted,
 * provided that the following conditions are met:
 * 1. XSRedistributions of source code must retain the above copyright notice,<commit_msg>changed the description of the epicenter slicer (IGNORE)<commit_after>(* epicenter.ml *)
(* code for epicenter.mli *)

(* module maintainer: scott *)
(* see copyright at end of this file *)

open Callgraph
open Cil
open Trace
open Pretty
module H = Hashtbl


let sliceFile (f:file) (epicenter:string) (maxHops:int) : unit =
  (* compute the static call graph *)
  let graph:callgraph = (computeGraph f) in

  (* will accumulate here the set of names of functions already seen *)
  let seen: (string, unit) H.t = (H.create 117) in

  (* when removing "unused" symbols, keep all seen functions *)
  let isRoot : global -> bool = function
    | GFun ({svar = {vname = vname}}, _) ->
	H.mem seen vname
    | _ ->
	false
  in

  (* recursive depth-first search through the call graph, finding
   * all nodes within 'hops' hops of 'node' and marking them to
   * to be retained *)
  let rec dfs (node:callnode) (hops:int) : unit =
    (* only recurse if we haven't already marked this node *)
    if not (H.mem seen node.cnInfo.vname) then
      begin
        (* add this node *)
	H.add seen node.cnInfo.vname ();
	trace "epicenter" (dprintf "will keep %s\n" node.cnInfo.vname);
	
        (* if we cannot do any more hops, stop *)
	if (hops > 0) then

          (* recurse on all the node's callers and callees *)
	  let recurse (adjName:string) (adjacent:callnode) : unit =
	    (dfs adjacent (hops - 1)) in
	  Hashtbl.iter recurse node.cnCallees;
	  Hashtbl.iter recurse node.cnCallers
      end
  in
  dfs (Hashtbl.find graph epicenter) maxHops;

  (* finally, throw away anything we haven't decided to keep *)
  Util.sliceGlobal := true;
  Rmtmps.removeUnusedTemps ~isRoot:isRoot f

let doEpicenter = ref false
let epicenterName = ref ""
let epicenterHops = ref 0

let feature : featureDescr = 
  { fd_name = "epicenter";
    fd_enabled = doEpicenter;
    fd_description = "remove all functions except those within some number " ^
                     "of hops (in the call graph) from a given function";
    fd_extraopt = 
    [
    ("--epicenter-name", 
     Arg.String (fun s -> epicenterName := s),
               "<name>: do an epicenter slice starting from function <name>");
    ("--epicenter-hops", Arg.Int (fun n -> epicenterHops := n),
     "<n>: specify max # of hops for epicenter slice");
    ];

    fd_doit =
    (fun f -> 
      sliceFile f !epicenterName !epicenterHops);

    fd_post_check = true;
  } 


(*
 *
 * Copyright (c) 2001-2002 by
 *  <NAME>	<EMAIL>
 *  <NAME>        <EMAIL>
 *  <NAME>          <EMAIL>
 *  <NAME>          <EMAIL>
 *
 * All rights reserved.  Permission to use, copy, modify and distribute
 * this software for research purposes only is hereby granted,
 * provided that the following conditions are met:
 * 1. XSRedistributions of source code must retain the above copyright notice,
{'fraction_non_alphanumeric': 0.10998369269795252, 'fraction_numerical': 0.005435767349157456, 'mean_word_length': 2.9372325249643367, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7619185', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1749, 'n_words': 641}