summaryrefslogtreecommitdiff
path: root/src/rebar_prv_dialyzer.erl
blob: 20a58f3dac3834e04b9c29613b91b95a48cf67fc (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
106
107
108
%% -*- 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/1]).

-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),
    Deps = rebar_state:get(State, all_deps, []),

    try
        ?INFO("Doing plt for dependencies...", []),
        update_dep_plt(State, DepPlt, Deps),
        ?INFO("Doing plt for project apps...", []),
        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, DepPlt]}],

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

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

%% 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 will take a while...", []),
                [{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].