Skip to content

Commit

Permalink
Allow the source to be set when compiling forms
Browse files Browse the repository at this point in the history
This commit adds a source option to compile:forms() that
sets the source value returned by module_info(compile).
  • Loading branch information
José Valim authored and proxyles committed Apr 10, 2012
1 parent 5573888 commit 2d785c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/compiler/doc/src/compile.xml
Expand Up @@ -294,6 +294,12 @@ module.beam: module.erl \
describing what it is doing.</p>
</item>

<tag><c>{source,FileName}</c></tag>
<item>
<p>Sets the value of the source, as returned by
<c>module_info(compile)</c>.</p>
</item>

<tag><c>{outdir,Dir}</c></tag>
<item>
<p>Sets a new directory for the object code. The current
Expand Down
10 changes: 6 additions & 4 deletions lib/compiler/src/compile.erl
Expand Up @@ -247,10 +247,12 @@ internal(Master, Input, Opts) ->
catch error:Reason -> {error, Reason}
end}.

internal({forms,Forms}, Opts) ->
{_,Ps} = passes(forms, Opts),
internal_comp(Ps, "", "", #compile{code=Forms,options=Opts,
mod_options=Opts});
internal({forms,Forms}, Opts0) ->
{_,Ps} = passes(forms, Opts0),
Source = proplists:get_value(source, Opts0, ""),
Opts1 = proplists:delete(source, Opts0),
Compile = #compile{code=Forms,options=Opts1,mod_options=Opts1},
internal_comp(Ps, Source, "", Compile);
internal({file,File}, Opts) ->
{Ext,Ps} = passes(file, Opts),
Compile = #compile{options=Opts,mod_options=Opts},
Expand Down
19 changes: 17 additions & 2 deletions lib/compiler/test/compile_SUITE.erl
Expand Up @@ -25,7 +25,7 @@
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
app_test/1,
file_1/1, module_mismatch/1, big_file/1, outdir/1,
file_1/1, forms_2/1, module_mismatch/1, big_file/1, outdir/1,
binary/1, makedep/1, cond_and_ifdef/1, listings/1, listings_big/1,
other_output/1, package_forms/1, encrypted_abstr/1,
bad_record_use1/1, bad_record_use2/1, strict_record/1,
Expand All @@ -42,7 +42,7 @@ suite() -> [{ct_hooks,[ts_install_cth]}].

all() ->
test_lib:recompile(?MODULE),
[app_test, file_1, module_mismatch, big_file, outdir,
[app_test, file_1, forms_2, module_mismatch, big_file, outdir,
binary, makedep, cond_and_ifdef, listings, listings_big,
other_output, package_forms, encrypted_abstr,
{group, bad_record_use}, strict_record,
Expand Down Expand Up @@ -105,6 +105,21 @@ file_1(Config) when is_list(Config) ->
?line test_server:timetrap_cancel(Dog),
ok.

forms_2(Config) when is_list(Config) ->
{ok, simple, Binary} = compile:forms([{attribute,1,module,simple}], [binary, {source,"/foo/bar"}]),
code:load_binary(simple, "/foo/bar", Binary),
Info = simple:module_info(compile),

%% Test proper source is returned.
"/foo/bar" = proplists:get_value(source, Info),
%% Ensure options is not polluted with the source.
[] = proplists:get_value(options, Info),

%% Cleanup.
true = code:delete(simple),
false = code:purge(simple),
ok.

module_mismatch(Config) when is_list(Config) ->
?line DataDir = ?config(data_dir, Config),
?line File = filename:join(DataDir, "wrong_module_name.erl"),
Expand Down

0 comments on commit 2d785c0

Please sign in to comment.