summaryrefslogtreecommitdiff
path: root/src/rebar_prv_dialyzer.erl
blob: a43999b4287a867863e86b0522d52578ad9bc40c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et

-module(rebar_prv_dialyzer).

-behaviour(provider).

-export([init/1,
         do/1,
         format_error/2]).

-include("rebar.hrl").

-define(PROVIDER, dialyzer).
-define(DEPS, [compile]).

%% ===================================================================
%% Public API
%% ===================================================================

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
    State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
                                                               {module, ?MODULE},
                                                               {bare, false},
                                                               {deps, ?DEPS},
                                                               {example, "rebar dialyzer"},
                                                               {short_desc, "Run the Dialyzer analyzer on the project."},
                                                               {desc, ""},
                                                               {opts, []}])),
    {ok, State1}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
    ?INFO("Dialyzer starting, this may take a while...", []),
    BuildDir = rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR),
    {ProjectPlt, _DepPlt} = get_plt_location(BuildDir),
    Apps = rebar_state:project_apps(State),
    ?INFO("Doing plt for project apps...", []),

    try
        update_dep_plt(State, ProjectPlt, Apps),
        WarningTypes = rebar_state:get(State, dialyzer_warnings, default_warnings()),
        Paths = [filename:join(rebar_app_info:dir(App), "ebin") || App <- Apps],
        Opts = [{analysis_type, succ_typings},
                {from, byte_code},
                {files_rec, Paths},
                {warnings, WarningTypes},
                {plts, [ProjectPlt]}],

        case dialyzer:run(Opts) of
            [] ->
                {ok, State};
            Warnings ->
                [?WARN(dialyzer:format_warning(Warning), []) || Warning <- Warnings],
                {ok, State}
        end
    catch
        _:{dialyzer_error, Error} ->
            {error, {?MODULE, {error_processing_apps, Error, Apps}}}
    end.

-spec format_error(any(), rebar_state:t()) ->  {iolist(), rebar_state:t()}.
format_error({error_processing_apps, Error, _Apps}, State) ->
    {io_lib:format("Error in dialyzing apps: ~s", [Error]), State};
format_error(Reason, State) ->
    {io_lib:format("~p", [Reason]), State}.

%% Internal functions

get_plt_location(BuildDir) ->
    {filename:join([BuildDir, ".project.plt"]),
     filename:join([BuildDir, ".deps.plt"])}.

update_dep_plt(_State, DepPlt, AppList) ->
    Opts0 =
        case filelib:is_file(DepPlt) of
            true ->
                ?INFO("Plt is built, checking/updating ...", []),
                [{analysis_type, plt_check},
                 {plts, [DepPlt]}];
            false ->
                ?INFO("Building the plt, this is really going to "
                               "take a long time ...", []),
                [{analysis_type, plt_build},
                 {output_plt, DepPlt}]
        end,
    Paths = [filename:join(rebar_app_info:dir(App), "ebin") || App <- AppList],
    Opts = [{files_rec, Paths},
            {from, byte_code}] ++ Opts0,

    dialyzer:run(Opts).

default_warnings() ->
    [no_return,
     no_unused,
     no_improper_lists,
     no_fun_app,
     no_match,
     no_opaque,
     no_fail_call,
     error_handling,
     race_conditions,
     unmatched_returns,
     underspecs].