Skip to content
This repository has been archived by the owner on May 12, 2018. It is now read-only.

Commit

Permalink
Add support for custom xref queries
Browse files Browse the repository at this point in the history
The custom queries are configured in rebar.config via the tuple
{xref_queries, [{query(), query_result()},...]}.  The implementation
passes the query() string to xref:q and compares the return value with
query_result(). It will result in an error if they do not match.

The following configuration, for example, is the same as running the
xref check undefined_function_calls. It additionally filters
ejabberd_logger:*_msg/4 from the result as these functions are generated
on execution by ejabberd and not available at compile time.

{xref_queries, [{"(XC - UC) || (XU - X - B -
                 (\"ejabberd_logger\":\".*_msg\"/\"4\"))",[]}]}.

This patch also modifies the build process of this package by running a
custom query instead of doing a diff against a static xref_warning file.
  • Loading branch information
xcurry authored and Tuncer Ayaz committed Apr 2, 2012
1 parent 26e1252 commit 0191806
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ debug:

check: debug xref dialyzer

xref: xref_warnings
@diff -U0 xref_reference xref_warnings

xref_warnings:
-@./rebar xref > xref_warnings
xref:
-@./rebar xref

dialyzer: dialyzer_warnings
@diff -U0 dialyzer_reference dialyzer_warnings
Expand Down
6 changes: 5 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

{app_bin, ["priv/rebar"]}.
{erl_opts, [warnings_as_errors]}.
{xref_checks, [undefined_function_calls]}.
{xref_checks, []}.
{xref_queries,
[{"(XC - UC) || (XU - X - B
- (\"escript\":\"foldl\"/\"3\")
- (\"abnfc\":\"file\"/\"2\"))",[]}]}.
10 changes: 10 additions & 0 deletions rebar.config.sample
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,15 @@
%% == xref ==

{xref_warnings, false}.

%% xref checks to run
{xref_checks, [exports_not_used, undefined_function_calls]}.

%% Optional custom xref queries (xref manual has details) specified as
%% {xref_queries, [{query_string(), expected_query_result()},...]}
%% The following for example removes all references to ejabberd:*_msg/4
%% functions from undefined external function calls as those are in a
%% generated module
{xref_queries,
[{"(XC - UC) || (XU - X - B"
" - (\"ejabberd_logger\":\".*_msg\"/\"4\"))",[]}]}.
19 changes: 18 additions & 1 deletion src/rebar_xref.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ xref(Config, _) ->
false ->
true
end,

%% Run custom queries
QueryChecks = rebar_config:get(Config, xref_queries, []),
QueryNoWarn = lists:all(fun check_query/1, QueryChecks),

%% Restore the original code path
true = code:set_path(OrigPath),

%% Stop xref
stopped = xref:stop(xref),

case lists:all(fun(NoWarn) -> NoWarn end, [ExportsNoWarn, UndefNoWarn]) of
case lists:all(fun(NoWarn) -> NoWarn end,
[ExportsNoWarn, UndefNoWarn, QueryNoWarn]) of
true ->
ok;
false ->
Expand Down Expand Up @@ -115,6 +121,17 @@ check_undefined_function_calls() ->
end, UndefinedCalls),
UndefinedCalls =:= [].

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.

code_path() ->
[P || P <- code:get_path(),
filelib:is_dir(P)] ++ [filename:join(rebar_utils:get_cwd(), "ebin")].
Expand Down
2 changes: 0 additions & 2 deletions xref_reference

This file was deleted.

0 comments on commit 0191806

Please sign in to comment.