Skip to content

Commit

Permalink
Do not write beam file if Werr and warnings /= []
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuncer Ayaz committed Sep 8, 2011
1 parent ab37957 commit 3d28c22
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
14 changes: 10 additions & 4 deletions lib/compiler/src/compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ noenv_forms(Forms, Opt) when is_atom(Opt) ->

noenv_output_generated(Opts) ->
{_,Passes} = passes(file, expand_opts(Opts)),
any(fun ({save_binary,_F}) -> true;
any(fun ({save_binary,_T,_F}) -> true;
(_Other) -> false
end, Passes).

Expand All @@ -122,6 +122,7 @@ noenv_output_generated(Opts) ->
%%

-define(pass(P), {P,fun P/1}).
-define(pass(P,T), {P,fun T/1,fun P/1}).

env_default_opts() ->
Key = "ERL_COMPILER_OPTIONS",
Expand Down Expand Up @@ -304,7 +305,7 @@ run_tc({Name,Fun}, St) ->
Val.

comp_ret_ok(#compile{code=Code,warnings=Warn0,module=Mod,options=Opts}=St) ->
case member(warnings_as_errors, Opts) andalso length(Warn0) > 0 of
case werror(St) of
true ->
case member(report_warnings, Opts) of
true ->
Expand Down Expand Up @@ -339,6 +340,11 @@ comp_ret_err(#compile{warnings=Warn0,errors=Err0,options=Opts}=St) ->
false -> error
end.

not_werror(St) -> not werror(St).

werror(#compile{options=Opts,warnings=Ws}) ->
member(warnings_as_errors, Opts) andalso Ws =/= [].

%% messages_per_file([{File,[Message]}]) -> [{File,[Message]}]
messages_per_file(Ms) ->
T = lists:sort([{File,M} || {File,Messages} <- Ms, M <- Messages]),
Expand Down Expand Up @@ -373,7 +379,7 @@ passes(Type, Opts) ->
%% insert a first pass to remove the file (unless the
%% source file is a BEAM file).
{Ext,case last(Passes) of
{save_binary,_Fun} ->
{save_binary,_TestFun,_Fun} ->
case Passes of
[{read_beam_file,_}|_] ->
%% The BEAM is both input and output.
Expand Down Expand Up @@ -655,7 +661,7 @@ asm_passes() ->

binary_passes() ->
[{native_compile,fun test_native/1,fun native_compile/1},
{unless,binary,?pass(save_binary)}].
{unless,binary,?pass(save_binary,not_werror)}].

%%%
%%% Compiler passes.
Expand Down
65 changes: 52 additions & 13 deletions lib/compiler/test/error_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,47 @@ get_compilation_errors(Config, Filename) ->
E.

warnings_as_errors(Config) when is_list(Config) ->
Ts = [{warnings_as_errors,
?line TestFile = test_filename(Config),
?line BeamFile = filename:rootname(TestFile, ".erl") ++ ".beam",
?line OutDir = ?config(priv_dir, Config),

Ts1 = [{warnings_as_errors,
<<"
t() ->
A = unused,
ok.
">>,
[export_all,warnings_as_errors],
{error,
[],
[{3,erl_lint,{unused_var,'A'}}]} }],
?line [] = run(Config, Ts),
[warnings_as_errors, export_all, {outdir, OutDir}],
{error,
[],
[{3,erl_lint,{unused_var,'A'}}]} }],
?line [] = run(Ts1, TestFile, write_beam),
?line false = filelib:is_regular(BeamFile),

Ts2 = [{warning_unused_var,
<<"
t() ->
A = unused,
ok.
">>,
[return_warnings, export_all, {outdir, OutDir}],
{warning,
[{3,erl_lint,{unused_var,'A'}}]} }],

?line [] = run(Ts2, TestFile, write_beam),
?line true = filelib:is_regular(BeamFile),
?line ok = file:delete(BeamFile),

ok.


run(Config, Tests) ->
?line File = test_filename(Config),
run(Tests, File, dont_write_beam).

run(Tests, File, WriteBeam) ->
F = fun({N,P,Ws,E}, BadL) ->
case catch run_test(Config, P, Ws) of
case catch run_test(P, File, Ws, WriteBeam) of
E ->
BadL;
Bad ->
Expand All @@ -211,8 +235,12 @@ run(Config, Tests) ->
lists:foldl(F, [], Tests).

run2(Config, Tests) ->
?line File = test_filename(Config),
run2(Tests, File, dont_write_beam).

run2(Tests, File, WriteBeam) ->
F = fun({N,P,Ws,E}, BadL) ->
case catch filter(run_test(Config, P, Ws)) of
case catch filter(run_test(P, File, Ws, WriteBeam)) of
E ->
BadL;
Bad ->
Expand All @@ -231,12 +259,19 @@ filter(X) ->

%% Compiles a test module and returns the list of errors and warnings.

run_test(Conf, Test0, Warnings) ->
Filename = 'errors_test.erl',
?line DataDir = ?config(priv_dir, Conf),
test_filename(Conf) ->
Filename = "errors_test.erl",
DataDir = ?config(priv_dir, Conf),
filename:join(DataDir, Filename).

run_test(Test0, File, Warnings, WriteBeam) ->
?line Test = ["-module(errors_test). ", Test0],
?line File = filename:join(DataDir, Filename),
?line Opts = [binary,return_errors|Warnings],
?line Opts = case WriteBeam of
dont_write_beam ->
[binary,return_errors|Warnings];
write_beam ->
[return_errors|Warnings]
end,
?line ok = file:write_file(File, Test),

%% Compile once just to print all errors and warnings.
Expand All @@ -252,6 +287,10 @@ run_test(Conf, Test0, Warnings) ->
%io:format("compile:file(~s,~p) ->~n~p~n",
% [File,Opts,Ws]),
[];
{ok,errors_test,[{_File,Ws}]} ->
{warning,Ws};
{ok,errors_test,[]} ->
[];
{error,[{XFile,Es}],Ws} = _ZZ when is_list(XFile) ->
%io:format("compile:file(~s,~p) ->~n~p~n",
% [File,Opts,_ZZ]),
Expand Down

0 comments on commit 3d28c22

Please sign in to comment.