2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2010-04-09 15:25:00 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% rebar: Erlang Build Tools
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
|
|
|
|
%%
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
%%
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%% This module borrows heavily from http://github.com/etnt/exrefcheck project as
|
|
|
|
%% written by Torbjorn Tornkvist <tobbe@kreditor.se>, Daniel Luna and others.
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
-module(rebar_xref).
|
|
|
|
|
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
-export([xref/2]).
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2011-12-12 18:08:40 +00:00
|
|
|
xref(Config, _) ->
|
2010-04-09 15:25:00 +00:00
|
|
|
%% Spin up xref
|
2010-10-10 21:24:20 +00:00
|
|
|
{ok, _} = xref:start(xref),
|
2010-04-09 15:25:00 +00:00
|
|
|
ok = xref:set_library_path(xref, code_path()),
|
2011-01-28 15:08:27 +00:00
|
|
|
|
|
|
|
xref:set_default(xref, [{warnings,
|
|
|
|
rebar_config:get(Config, xref_warnings, false)},
|
2010-04-09 15:25:00 +00:00
|
|
|
{verbose, rebar_config:is_verbose()}]),
|
2011-01-28 15:08:27 +00:00
|
|
|
|
2010-04-09 15:25:00 +00:00
|
|
|
{ok, _} = xref:add_directory(xref, "ebin"),
|
|
|
|
|
2010-04-23 15:27:50 +00:00
|
|
|
%% Save the code path prior to doing anything
|
|
|
|
OrigPath = code:get_path(),
|
2010-10-10 20:11:13 +00:00
|
|
|
true = code:add_path(filename:join(rebar_utils:get_cwd(), "ebin")),
|
2010-04-23 15:27:50 +00:00
|
|
|
|
2010-04-09 15:25:00 +00:00
|
|
|
%% Get list of xref checks we want to run
|
2011-01-28 15:08:27 +00:00
|
|
|
XrefChecks = rebar_config:get(Config, xref_checks,
|
|
|
|
[exports_not_used,
|
|
|
|
undefined_function_calls]),
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
%% Look for exports that are unused by anything
|
2011-03-29 13:43:53 +00:00
|
|
|
ExportsNoWarn =
|
|
|
|
case lists:member(exports_not_used, XrefChecks) of
|
|
|
|
true ->
|
|
|
|
check_exports_not_used();
|
|
|
|
false ->
|
|
|
|
true
|
|
|
|
end,
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
%% Look for calls to undefined functions
|
2011-03-29 13:43:53 +00:00
|
|
|
UndefNoWarn =
|
|
|
|
case lists:member(undefined_function_calls, XrefChecks) of
|
|
|
|
true ->
|
|
|
|
check_undefined_function_calls();
|
|
|
|
false ->
|
|
|
|
true
|
|
|
|
end,
|
2012-04-02 15:01:39 +00:00
|
|
|
|
|
|
|
%% Run custom queries
|
|
|
|
QueryChecks = rebar_config:get(Config, xref_queries, []),
|
|
|
|
QueryNoWarn = lists:all(fun check_query/1, QueryChecks),
|
|
|
|
|
2010-04-23 15:27:50 +00:00
|
|
|
%% Restore the original code path
|
2010-10-10 20:11:13 +00:00
|
|
|
true = code:set_path(OrigPath),
|
2010-04-23 15:27:50 +00:00
|
|
|
|
2011-03-26 11:14:52 +00:00
|
|
|
%% Stop xref
|
|
|
|
stopped = xref:stop(xref),
|
|
|
|
|
2012-04-02 15:01:39 +00:00
|
|
|
case lists:all(fun(NoWarn) -> NoWarn end,
|
|
|
|
[ExportsNoWarn, UndefNoWarn, QueryNoWarn]) of
|
2011-03-29 13:43:53 +00:00
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
?FAIL
|
|
|
|
end.
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2011-12-12 18:08:40 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2011-03-29 13:43:53 +00:00
|
|
|
check_exports_not_used() ->
|
2010-04-09 15:25:00 +00:00
|
|
|
{ok, UnusedExports0} = xref:analyze(xref, exports_not_used),
|
|
|
|
UnusedExports = filter_away_ignored(UnusedExports0),
|
|
|
|
|
|
|
|
%% Report all the unused functions
|
|
|
|
display_mfas(UnusedExports, "is unused export (Xref)"),
|
2011-03-29 13:43:53 +00:00
|
|
|
UnusedExports =:= [].
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2011-03-29 13:43:53 +00:00
|
|
|
check_undefined_function_calls() ->
|
2010-04-09 15:25:00 +00:00
|
|
|
{ok, UndefinedCalls0} = xref:analyze(xref, undefined_function_calls),
|
2011-01-28 15:08:27 +00:00
|
|
|
UndefinedCalls =
|
2011-03-29 13:43:53 +00:00
|
|
|
[{find_mfa_source(Caller), format_fa(Caller), format_mfa(Target)}
|
|
|
|
|| {Caller, Target} <- UndefinedCalls0],
|
2011-01-28 15:08:27 +00:00
|
|
|
|
|
|
|
lists:foreach(
|
|
|
|
fun({{Source, Line}, FunStr, Target}) ->
|
|
|
|
?CONSOLE("~s:~w: Warning ~s calls undefined function ~s\n",
|
|
|
|
[Source, Line, FunStr, Target])
|
|
|
|
end, UndefinedCalls),
|
2011-03-29 13:43:53 +00:00
|
|
|
UndefinedCalls =:= [].
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2012-04-02 15:01:39 +00:00
|
|
|
check_query({Query, Value}) ->
|
|
|
|
{ok, Answer} = xref:q(xref, Query),
|
|
|
|
case Answer =:= Value of
|
|
|
|
false ->
|
|
|
|
?CONSOLE("Query ~s~n answer ~p~n did not match ~p~n",
|
|
|
|
[Query, Answer, Value]),
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2010-04-09 15:25:00 +00:00
|
|
|
code_path() ->
|
|
|
|
[P || P <- code:get_path(),
|
2010-04-23 15:27:50 +00:00
|
|
|
filelib:is_dir(P)] ++ [filename:join(rebar_utils:get_cwd(), "ebin")].
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
%% Ignore behaviour functions, and explicitly marked functions
|
|
|
|
%%
|
|
|
|
filter_away_ignored(UnusedExports) ->
|
|
|
|
%% Functions can be ignored by using
|
|
|
|
%% -ignore_xref([{F, A}, ...]).
|
|
|
|
|
2011-01-07 16:24:27 +00:00
|
|
|
%% Setup a filter function that builds a list of behaviour callbacks and/or
|
2011-01-28 15:08:27 +00:00
|
|
|
%% any functions marked to ignore. We then use this list to mask any
|
|
|
|
%% functions marked as unused exports by xref
|
2010-04-09 15:25:00 +00:00
|
|
|
F = fun(Mod) ->
|
2010-10-10 20:11:13 +00:00
|
|
|
Attrs = kf(attributes, Mod:module_info()),
|
|
|
|
Ignore = kf(ignore_xref, Attrs),
|
2011-01-28 15:08:27 +00:00
|
|
|
Callbacks =
|
|
|
|
[B:behaviour_info(callbacks) || B <- kf(behaviour, Attrs)],
|
2010-04-09 15:25:00 +00:00
|
|
|
[{Mod, F, A} || {F, A} <- Ignore ++ lists:flatten(Callbacks)]
|
|
|
|
end,
|
2011-01-28 15:08:27 +00:00
|
|
|
AttrIgnore =
|
|
|
|
lists:flatten(
|
|
|
|
lists:map(F, lists:usort([M || {M, _, _} <- UnusedExports]))),
|
2011-01-09 10:54:40 +00:00
|
|
|
[X || X <- UnusedExports, not lists:member(X, AttrIgnore)].
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
|
2010-10-10 20:11:13 +00:00
|
|
|
kf(Key, List) ->
|
|
|
|
case lists:keyfind(Key, 1, List) of
|
|
|
|
{Key, Value} ->
|
2010-04-09 15:25:00 +00:00
|
|
|
Value;
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
display_mfas([], _Message) ->
|
|
|
|
ok;
|
2010-06-19 13:46:57 +00:00
|
|
|
display_mfas([{_Mod, Fun, Args} = MFA | Rest], Message) ->
|
2010-04-09 15:25:00 +00:00
|
|
|
{Source, Line} = find_mfa_source(MFA),
|
2011-01-28 15:08:27 +00:00
|
|
|
?CONSOLE("~s:~w: Warning: function ~s/~w ~s\n",
|
|
|
|
[Source, Line, Fun, Args, Message]),
|
2010-04-09 15:25:00 +00:00
|
|
|
display_mfas(Rest, Message).
|
|
|
|
|
|
|
|
format_mfa({M, F, A}) ->
|
|
|
|
?FMT("~s:~s/~w", [M, F, A]).
|
|
|
|
|
|
|
|
format_fa({_M, F, A}) ->
|
|
|
|
?FMT("~s/~w", [F, A]).
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Extract an element from a tuple, or undefined if N > tuple size
|
2011-01-09 10:54:40 +00:00
|
|
|
%%
|
2010-04-09 15:25:00 +00:00
|
|
|
safe_element(N, Tuple) ->
|
|
|
|
case catch(element(N, Tuple)) of
|
|
|
|
{'EXIT', {badarg, _}} ->
|
|
|
|
undefined;
|
|
|
|
Value ->
|
|
|
|
Value
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Given a MFA, find the file and LOC where it's defined. Note that
|
|
|
|
%% xref doesn't work if there is no abstract_code, so we can avoid
|
|
|
|
%% being too paranoid here.
|
|
|
|
%%
|
|
|
|
find_mfa_source({M, F, A}) ->
|
|
|
|
{M, Bin, _} = code:get_object_code(M),
|
2011-01-28 15:08:27 +00:00
|
|
|
AbstractCode = beam_lib:chunks(Bin, [abstract_code]),
|
|
|
|
{ok, {M, [{abstract_code, {raw_abstract_v1, Code}}]}} = AbstractCode,
|
2011-01-09 10:54:40 +00:00
|
|
|
%% Extract the original source filename from the abstract code
|
|
|
|
[{attribute, 1, file, {Source, _}} | _] = Code,
|
|
|
|
%% Extract the line number for a given function def
|
2011-09-22 13:01:22 +00:00
|
|
|
Fn = [E || E <- Code,
|
|
|
|
safe_element(1, E) == function,
|
|
|
|
safe_element(3, E) == F,
|
|
|
|
safe_element(4, E) == A],
|
|
|
|
case Fn of
|
|
|
|
[{function, Line, F, _, _}] -> {Source, Line};
|
|
|
|
%% do not crash if functions are exported, even though they
|
|
|
|
%% are not in the source.
|
|
|
|
%% parameterized modules add new/1 and instance/1 for example.
|
|
|
|
[] -> {Source, function_not_found}
|
|
|
|
end.
|