support keeping/renaming function being replaced

This commit is contained in:
Ulf Wiger 2014-11-30 23:44:49 +01:00
parent 82cc00264a
commit aa74eee285
2 changed files with 32 additions and 4 deletions

View File

@ -124,7 +124,7 @@ Performs a transform of <code>Forms</code> using the fun <code>Fun(Form)</code>.
Reads debug_info from the beam file Beam and returns a string containing
the pretty-printed corresponding erlang source code.</td></tr><tr><td valign="top"><a href="#pp_beam-2">pp_beam/2</a></td><td>
Reads debug_info from the beam file Beam and pretty-prints it as
Erlang source code, storing it in the file Out.</td></tr><tr><td valign="top"><a href="#pp_src-2">pp_src/2</a></td><td>Pretty-prints the erlang source code corresponding to Forms into Out.</td></tr><tr><td valign="top"><a href="#replace_function-4">replace_function/4</a></td><td></td></tr><tr><td valign="top"><a href="#return-2">return/2</a></td><td>Checks the transformed result for errors and warnings.</td></tr><tr><td valign="top"><a href="#revert-1">revert/1</a></td><td>Reverts back from Syntax Tools format to Erlang forms.</td></tr><tr><td valign="top"><a href="#revert_form-1">revert_form/1</a></td><td>Reverts a single form back from Syntax Tools format to Erlang forms.</td></tr><tr><td valign="top"><a href="#top-3">top/3</a></td><td></td></tr><tr><td valign="top"><a href="#transform-4">transform/4</a></td><td>
Erlang source code, storing it in the file Out.</td></tr><tr><td valign="top"><a href="#pp_src-2">pp_src/2</a></td><td>Pretty-prints the erlang source code corresponding to Forms into Out.</td></tr><tr><td valign="top"><a href="#replace_function-4">replace_function/4</a></td><td></td></tr><tr><td valign="top"><a href="#replace_function-5">replace_function/5</a></td><td></td></tr><tr><td valign="top"><a href="#return-2">return/2</a></td><td>Checks the transformed result for errors and warnings.</td></tr><tr><td valign="top"><a href="#revert-1">revert/1</a></td><td>Reverts back from Syntax Tools format to Erlang forms.</td></tr><tr><td valign="top"><a href="#revert_form-1">revert_form/1</a></td><td>Reverts a single form back from Syntax Tools format to Erlang forms.</td></tr><tr><td valign="top"><a href="#top-3">top/3</a></td><td></td></tr><tr><td valign="top"><a href="#transform-4">transform/4</a></td><td>
Makes one pass.</td></tr></table>
@ -486,6 +486,13 @@ Pretty-prints the erlang source code corresponding to Forms into Out
`replace_function(F, Arity, NewForm, Forms) -> any()`
<a name="replace_function-5"></a>
### replace_function/5 ###
`replace_function(F, Arity, NewForm, Forms, Opts) -> any()`
<a name="return-2"></a>
### return/2 ###

View File

@ -60,6 +60,7 @@
-export([do_insert_forms/4,
replace_function/4,
replace_function/5,
export_function/3]).
-export([
@ -351,19 +352,39 @@ top(F, Forms, Options) ->
end.
replace_function(F, Arity, NewForm, Forms) ->
replace_function(F, Arity, NewForm, Forms, []).
replace_function(F, Arity, NewForm, Forms, Opts) ->
{NewForms, _} =
do_transform(
fun(function, Form, _Ctxt, Acc) ->
case erl_syntax:revert(Form) of
{function, _, F, Arity, _} ->
{NewForm, false, Acc};
{function, _, F, Arity, _} = RevForm ->
{[], NewForm, with_original_f(RevForm, Opts),
false, Acc};
_ ->
{Form, false, Acc}
end;
(_, Form, _Ctxt, Acc) ->
{Form, false, Acc}
end, false, Forms, initial_context(Forms, [])),
revert(NewForms).
revert(maybe_export_renamed(NewForms, Arity, Opts)).
with_original_f({function,_,_,_,_} = Form, Opts) ->
case lists:keyfind(rename_original, 1, Opts) of
{_, NewName} when is_atom(NewName) ->
[setelement(3, Form, NewName)];
_ ->
[]
end.
maybe_export_renamed(Forms, Arity, Opts) ->
case lists:keyfind(rename_original, 1, Opts) of
{_, NewName} when is_atom(NewName) ->
export_function(NewName, Arity, Forms);
_ ->
Forms
end.
export_function(F, Arity, Forms) ->
do_insert_forms(above, [{attribute, 1, export, [{F, Arity}]}], Forms,