anchor.diff:  %% =====================================================================
  -%% Refactoring: Rename a variable name.
  +%% Refactoring: Register a process.
   %%
   %% Copyright (C) 2006-2008  Huiqing Li, Simon Thompson
  -
  -
  +%%
   %% The contents of this file are subject to the Erlang Public License,
   %% Version 1.1, (the "License"); you may not use this file except in
   %% compliance with the License. You should have received a copy of the
   %% Erlang Public License along with this software. If not, it can be
   %% retrieved via the world wide web at http://www.erlang.org/.
  -
  -
  +%%
   %% Software distributed under the License is distributed on an "AS IS"
   %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   %% the License for the specific language governing rights and limitations
  
positive.code: %%
  %% =====================================================================
  %%
  %% @doc This refactoring register a process id, Pid say, with a name, RegName say, and replace
  %% the uses of Pid ! Msg with  RegName ! Msg if possible.
  %% @end
  -module(refac_register_pid).
  
  -export([register_pid/5, register_pid_eclipse/5, register_pid_1/5, register_pid_2/5]).
  
  -export([get_call_graph/1]).
  
  -include("../hrl/wrangler.hrl").
  
  %% Rationale for this refactoring: If we want to send a message to a process, then we need to know its PID. This is often inconvenient since the 
  %% PID has to be sent to all the processes in the system that want to comminicate with this process. Registering a process with a name allows any 
  %% process in the system to communicate with this process without knowing its PID.
  %%
  %% To perform this refactoring, the user needs to select a match expression whose right-hand side is a spawn expression, and whose left-hand side 
  %% is a Pid variable.
  %%
  
  %% Side conditions for this refactorings:
  %% 1. The process name provided by the user should be lexically valid. (done).
  %% 2. The name provided by the user should not have been used as a process name. (need to handle dynamically create process names) (done).
  
  %% 2. The same process cannot be registered more than once. (check the registration of the generated Pid, and self()). (done).
  
  %% 3. At any time during the running of the system, only one process can be associated with a particular process name.  Since statically we 
  %% cannot decide when a process starts/dies, we use a rather stronger condition, that is the function containing the registration is only 
  
negative.code: %%   3.2) the function is only called at one place of the program.
  %%   3.3) the function should not be used in a map/fold.
  %%   3.4) the registeration should not be in a receive expression, list comprehension (done)
  %% Why do I need slicing? mostly for reduced the number of unclear registration expressions.
  
  %% Side condition checking:
  %% So far, this cond-checking still cannot guarantee that only one process spawned by the 
  %% expression seleted exist during anytime of the running of the system.
  pre_cond_check(ModName, AnnAST, Start, MatchExpr, RegName, _Info, SearchPaths) ->		 
      {ok, FunDef} = refac_util:pos_to_fun_def(AnnAST, Start),
      FunName = refac_syntax:data(refac_syntax:function_name(FunDef)),
      Arity = refac_syntax:function_arity(FunDef),
      case is_recursive_fun(SearchPaths, {ModName, FunName, Arity, FunDef}) of 
  	true -> {error, "The function containing the spawn  expression is a recursive function"};
  	_ -> case pos_to_receive_expr(FunDef, Start) of 
  		 true -> {error, "Wrangler do not support registering a process spawned in a received expression\n"};
  		 _ -> case pos_to_list_comp_expr(FunDef, Start) of
  			  true -> {error, "The spawn expression selected in part of a list comprehension expression\n"};
  			  _ -> {RegPids, {ExistingProcessNames, UnKnowns}} = collect_registered_names_and_pids(SearchPaths),
  			     %%   io:format("registeredd:\n~p\n", [{ExistingProcessNames, UnKnowns}]),
  			       case lists:member(RegName, ExistingProcessNames) of 
  				   true -> {error, "The process name provided is already in use, please choose another name."};
  				   _ -> case UnKnowns of 
  					    [] ->
  						Res = check_registration(MatchExpr, SearchPaths, RegPids), 
  						case Res of 
  						    ok -> ok;
  						    {registered, RegExprs1} ->
  								{{_M, F, A}, _R} = hd(RegExprs1),
  							{error, "The process is already registered in function "++ atom_to_list(F)++"/"++integer_to_list(A)++"\n"};
  						    {unknown_pids, RegExprs} -> 
  							io:format("Wrangler could not decide the processe(s) registered by the followling expression(s):\n"),
  							 lists:foreach(fun({{M, F, A},PidExpr}) -> 
  									   {{Ln,_},_} = refac_util:get_range(PidExpr),
  									   io:format("Location: module:~p, function: ~p/~p, line: ~p\n ", [M, F, A, Ln])
  									  %% io:format(refac_prettypr:format(PidExpr)++"\n") 
  								   end, RegExprs),
  							{unknown_pids, RegExprs}
  						end;
  					    _ -> io:format("Wrangler could not decide the process name(s) used by the following register expression(s):\n"),
  						 UnKnowns1 = lists:map(fun({_, V}) -> V end, UnKnowns),
  						 lists:foreach(fun({M, F,A, {L,_}}) -> io:format("Location: module: ~p, function:~p/~p, line:~p\n", [M, F, A, L])
  							  end, UnKnowns1),
  						 {unknown_pnames, UnKnowns, RegPids}
  				       end
  			      end
  		     end
  	     end
      end.
  
      
     
  
  check_registration(MatchExpr, SearchPaths, RegPids) ->
      Pid = refac_syntax:match_expr_pattern(MatchExpr),
      {value, {pid, PidInfo}} = lists:keysearch(pid,1, refac_syntax:get_ann(Pid)),
      SpawnExpr = refac_syntax:match_expr_body(MatchExpr),
      %% functions reached from the intial function of the spawn expression.
      ReachedFuns = reached_funs(SpawnExpr, SearchPaths),
      F = fun({{M, F, A}, PidExpr}, {RegAcc, UnKnownAcc}) ->
  		Ann = refac_syntax:get_ann(PidExpr),
  		case lists:keysearch(pid,1, Ann) of 
  		    {value, {pid, PidInfo1}} ->
  			case PidInfo--PidInfo1=/= PidInfo of 
  			    true -> {[{{M,F, A}, PidExpr}|RegAcc], UnKnownAcc};
  			    false -> case lists:any(fun(P1) ->
  							    case P1 of
  								{self, Fun1} -> lists:member(Fun1, ReachedFuns);
  								_ -> false
  							    end
  						    end, PidInfo1) of 
  					 true -> {[{{M,F, A}, PidExpr}|RegAcc], UnKnownAcc};
  					 false -> {RegAcc, UnKnownAcc}
  				     end
  			end;
  		    _ ->{RegAcc, [{{M,F, A}, PidExpr}|UnKnownAcc]}
  		end
  	end,
      {Regs, UnKnowns} =lists:foldl(F, {[],[]}, RegPids),
      case Regs of 
  	[] -> case UnKnowns of 
  		  [] -> ok;
  		  _ -> {unknown_pids, UnKnowns}
  	      end;
  	_ -> {registered, Regs}
      end.
  
   
  reached_funs(SpawnExpr, SearchPaths) ->
       Args = refac_syntax:application_arguments(SpawnExpr),
       Arity = length(Args),
       InitialFuns = case (Arity==1) or (Arity==2) of 
  		       true -> Expr = lists:last(Args), 
  			       funs_called(Expr);			       
  		       _ ->  Args1 = list_to_tuple(lists:reverse(Args)),
  			     {M, F, A}= {element(3, Args1),element(2, Args1), element(1, Args1)},
  			     [{M, F, A}]
  		   end,
      {CallerCallee, _, _} = refac_callgraph_server:get_callgraph(SearchPaths), 
      InitialFuns++reached_funs_1(CallerCallee, InitialFuns).
  	 
  reached_funs_1(CallerCallee, Acc) ->
      Res = lists:usort(lists:concat(lists:map(fun({Mod, Fun, Args}) ->
  					 case lists:keysearch({Mod, Fun, Args}, 1, CallerCallee) of 
  					     {value, {{Mod, Fun, Args}, CalledFuns}} ->
  						 CalledFuns;
  					     _ ->[]
  					 end
  				 end, Acc))),
       case lists:usort(Res++Acc) == Acc of 
  	true -> Res;
  	_ -> reached_funs_1(CallerCallee, lists:usort(Res++Acc)) 
      end.	      
  		
      
  is_direct_recursive_fun(ModName, FunName, Arity, FunDef) ->
      F = fun(Node, {ModName1, FunName1, Arity1}) ->
  