1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2000-2020. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19
20-module(xref_SUITE).
21
22%-define(debug, true).
23
24-ifdef(debug).
25-define(format(S, A), io:format(S, A)).
26-define(line, put(line, ?LINE), ).
27-define(config(X,Y), "./log_dir/").
28-define(datadir, "xref_SUITE_data").
29-define(privdir, "xref_SUITE_priv").
30-define(copydir, "xref_SUITE_priv/datacopy").
31-else.
32-include_lib("common_test/include/ct.hrl").
33-define(format(S, A), ok).
34-define(datadir, proplists:get_value(data_dir, Conf)).
35-define(privdir, proplists:get_value(priv_dir, Conf)).
36-define(copydir, proplists:get_value(copy_dir, Conf)).
37-endif.
38
39-export([all/0, suite/0, groups/0,
40         init_per_suite/1, end_per_suite/1]).
41
42-export([addrem/1, convert/1, intergraph/1, lines/1, loops/1,
43         no_data/1, modules/1]).
44
45-export([add/1, default/1, info/1, lib/1, read/1, read2/1, remove/1,
46         replace/1, update/1, deprecated/1, trycatch/1,
47         fun_mfa/1,
48         fun_mfa_vars/1, qlc/1]).
49
50-export([analyze/1, basic/1, md/1, q/1, variables/1, unused_locals/1,
51         behaviour/1]).
52
53-export([format_error/1, otp_7423/1, otp_7831/1, otp_10192/1, otp_13708/1,
54         otp_14464/1, otp_14344/1]).
55
56-import(lists, [append/2, flatten/1, keysearch/3, member/2, sort/1, usort/1]).
57
58-import(sofs, [converse/1, from_term/1, intersection/2, is_sofs_set/1,
59               range/1, relation_to_family/1, set/1, to_external/1,
60               union/2]).
61
62%% Checks some info counters of a server and some relations that should hold.
63-export([check_count/1, check_state/1]).
64
65-include_lib("kernel/include/file.hrl").
66-include_lib("tools/src/xref.hrl").
67
68suite() ->
69    [{ct_hooks,[ts_install_cth]},
70     {timetrap,{minutes,2}}].
71
72all() ->
73    [{group, xref}, {group, files}, {group, analyses},
74     {group, misc}].
75
76groups() ->
77    [{xref, [],
78      [addrem, convert, intergraph, lines, loops, no_data,
79       modules]},
80     {files, [],
81      [add, default, info, lib, read, read2, remove, replace,
82       update, deprecated, trycatch, fun_mfa,
83       fun_mfa_vars, qlc]},
84     {analyses, [],
85
86      [analyze, basic, md, q, variables, unused_locals, behaviour]},
87     {misc, [], [format_error, otp_7423, otp_7831, otp_10192, otp_13708,
88                 otp_14464, otp_14344]}].
89
90
91init_per_suite(Conf) when is_list(Conf) ->
92    DataDir = ?datadir,
93    PrivDir = ?privdir,
94    CopyDir = fname(PrivDir, "datacopy"),
95    ok = file:make_dir(CopyDir),
96    TarFile = fname(PrivDir, "datacopy.tgz"),
97    ok = file:set_cwd(DataDir),
98    ok = erl_tar:create(TarFile, ["."], [compressed]),
99    ok = erl_tar:extract(TarFile, [compressed, {cwd,CopyDir}]),
100    ok = file:delete(TarFile),
101    [{copy_dir, CopyDir}|Conf].
102
103end_per_suite(Conf) when is_list(Conf) ->
104    ok.
105
106%% Seems a bit short...
107%% Simple test of removing modules
108addrem(Conf) when is_list(Conf) ->
109    S0 = new(),
110
111    F1 = {m1,f1,1},
112    F2 = {m2,f1,2},
113
114    E1 = {F1,F2},
115    E2 = {F2,F1},
116
117    D1 = {F1,12},
118    DefAt_m1 = [D1],
119    X_m1 = [F1],
120    % L_m1 = [],
121    XC_m1 = [E1],
122    LC_m1 = [],
123    LCallAt_m1 = [],
124    XCallAt_m1 = [{E1,13}],
125    Info1 = #xref_mod{name = m1, app_name = [a1]},
126    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
127                    XC_m1, LC_m1),
128
129    D2 = {F2,7},
130    DefAt_m2 = [D2],
131    X_m2 = [F2],
132    % L_m2 = [],
133    XC_m2 = [E2],
134    LC_m2 = [],
135    LCallAt_m2 = [],
136    XCallAt_m2 = [{E2,96}],
137    Info2 = #xref_mod{name = m2, app_name = [a2]},
138    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
139                    XC_m2, LC_m2),
140
141    S5 = set_up(S2),
142
143    {ok, XMod1, S6} = remove_module(S5, m1),
144    [a1] = XMod1#xref_mod.app_name,
145    {ok, XMod2, S6a} = remove_module(S6, m2),
146    [a2] = XMod2#xref_mod.app_name,
147    S7 = set_up(S6a),
148
149    AppInfo1 = #xref_app{name = a1, rel_name = [r1]},
150    S9 = add_application(S7, AppInfo1),
151    S10 = set_up(S9),
152    AppInfo2 = #xref_app{name = a2, rel_name = [r1]},
153    _S11 = add_application(S10, AppInfo2),
154    ok.
155
156%% Coercion of data
157convert(Conf) when is_list(Conf) ->
158    S0 = new(),
159
160    F1 = {m1,f1,1},
161    F6 = {m1,f2,6}, % X
162    F2 = {m2,f1,2},
163    F3 = {m2,f2,3}, % X
164    F7 = {m2,f3,7}, % X
165    F4 = {m3,f1,4}, % X
166    F5 = {m3,f2,5},
167
168    UF1 = {m1,f12,17},
169    UF2 = {m17,f17,177},
170
171    E1 = {F1,F3}, % X
172    E2 = {F6,F7}, % X
173    E3 = {F2,F6}, % X
174    E4 = {F1,F4}, % X
175    E5 = {F4,F5},
176    E6 = {F7,F4}, % X
177
178    UE1 = {F2,UF2}, % X
179    UE2 = {F5,UF1}, % X
180
181    D1 = {F1,12},
182    D6 = {F6,3},
183    DefAt_m1 = [D1,D6],
184    X_m1 = [F6],
185    % L_m1 = [F1],
186    XC_m1 = [E1,E2,E4],
187    LC_m1 = [],
188    LCallAt_m1 = [],
189    XCallAt_m1 = [{E1,13},{E2,17},{E4,7}],
190    Info1 = #xref_mod{name = m1, app_name = [a1]},
191    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
192                    XC_m1, LC_m1),
193
194    D2 = {F2,7},
195    D3 = {F3,9},
196    D7 = {F7,19},
197    DefAt_m2 = [D2,D3,D7],
198    X_m2 = [F3,F7],
199    % L_m2 = [F2],
200    XC_m2 = [E3,E6,UE1],
201    LC_m2 = [],
202    LCallAt_m2 = [],
203    XCallAt_m2 = [{E3,96},{E6,12},{UE1,77}],
204    Info2 = #xref_mod{name = m2, app_name = [a2]},
205    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
206                    XC_m2, LC_m2),
207
208    D4 = {F4,6},
209    D5 = {F5,97},
210    DefAt_m3 = [D4,D5],
211    X_m3 = [F4],
212    % L_m3 = [F5],
213    XC_m3 = [UE2],
214    LC_m3 = [E5],
215    LCallAt_m3 = [{E5,19}],
216    XCallAt_m3 = [{UE2,22}],
217    Info3 = #xref_mod{name = m3, app_name = [a3]},
218    S3 = add_module(S2, Info3, DefAt_m3, X_m3, LCallAt_m3, XCallAt_m3,
219                    XC_m3, LC_m3),
220
221    Info4 = #xref_mod{name = m4, app_name = [a2]},
222    S4 = add_module(S3, Info4, [], [], [], [], [], []),
223
224    AppInfo1 = #xref_app{name = a1, rel_name = [r1]},
225    S9 = add_application(S4, AppInfo1),
226    AppInfo2 = #xref_app{name = a2, rel_name = [r1]},
227    S10 = add_application(S9, AppInfo2),
228    AppInfo3 = #xref_app{name = a3, rel_name = [r2]},
229    S11 = add_application(S10, AppInfo3),
230
231    RelInfo1 = #xref_rel{name = r1},
232    S12 = add_release(S11, RelInfo1),
233    RelInfo2 = #xref_rel{name = r2},
234    S13 = add_release(S12, RelInfo2),
235
236    S = set_up(S13),
237
238    {ok, _} = eval("(Lin)(m1->m1:Mod) * m1->m1", type_error, S),
239    {ok, _} = eval("(XXL)(Lin)(m1->m1:Mod) * m1->m1", type_error, S),
240
241    AllDefAt = eval("(Lin) M", S),
242    AllV = eval("(Fun) M", S),
243    AllCallAt = eval("(XXL)(Lin) E", S),
244    AllE = eval("E", S),
245
246    AM = eval("AM", S),
247    A = eval("A", S),
248    R = eval("R", S),
249
250
251    % vertices
252    % general 1 step
253    {ok, _} = eval("(Fun) (Lin) M", AllV, S),
254    {ok, _} = eval("(Fun) (Lin) (Lin) M", AllV, S),
255    {ok, _} = eval(f("(Fun) (Lin) ~p", [[F1, F3]]), [F1,F3], S),
256    {ok, _} = eval(f("(Mod) ~p", [AllV]), [m1,m17,m2,m3], S),
257    {ok, _} = eval(f("(Mod) ~p", [[F1,F3,F6]]), [m1,m2], S),
258    {ok, _} = eval("(App) M", A, S),
259    {ok, _} = eval(f("(App) ~p", [[m1,m2,m4]]), [a1,a2], S),
260    {ok, _} = eval(f("(Rel) ~p", [A]), R, S),
261    {ok, _} = eval(f("(Rel) ~p", [[a1,a2,a2]]), [r1], S),
262    % general 2 steps
263    {ok, _} = eval("(Mod) (Lin) M", [m1,m17,m2,m3], S),
264    {ok, _} = eval(f("(App) ~p", [AllV]), [a1,a2,a3], S),
265    {ok, _} = eval("(Rel) M", R, S),
266    % general 4 steps
267    {ok, _} = eval("(Rel) (Lin) M", [r1,r2], S),
268
269    % special 1 step
270    {ok, _} = eval(f("(Lin) ~p", [AllV]), AllDefAt, S),
271    {ok, _} = eval(f("(Lin) ~p", [[F1,F3]]), [{F1,12},{F3,9}], S),
272    {ok, _} = eval("(Fun) M", AllV, S),
273    {ok, _} = eval(f("(Fun) ~p", [[m1,m2]]), [F1,F2,F3,F6,F7,UF1], S),
274    {ok, _} = eval(f("(Mod) ~p", [A]), AM, S),
275    {ok, _} = eval(f("(Mod) ~p", [[a1,a2]]), [m1,m2,m4], S),
276    {ok, _} = eval(f("(App) ~p", [R]), A, S),
277    {ok, _} = eval(f("(App) ~p", [[r1]]), [a1,a2], S),
278    % special 2 steps
279    {ok, _} = eval("(Lin) M", AllDefAt, S),
280    AnalyzedV = eval("(Fun) AM", S),
281    {ok, _} = eval(f("(Fun) ~p", [A]), AnalyzedV, S),
282    {ok, _} = eval(f("(Mod) ~p", [R]), AM, S),
283    % special 4 steps
284    AnalyzedAllDefAt = eval("(Lin) AM", S),
285    {ok, _} = eval("(Lin) R", AnalyzedAllDefAt, S),
286
287    % edges
288    Ms = [{m1,m2},{m1,m3},{m2,m1},{m2,m3},{m3,m3}],
289    UMs = [{m2,m17},{m3,m1}],
290    AllMs = append(Ms, UMs),
291    As = [{a1,a2},{a1,a3},{a2,a1},{a2,a3},{a3,a3}],
292    Rs = [{r1,r1},{r1,r2},{r2,r2}],
293
294    % general 1 step
295    {ok, _} = eval("(Fun) (Lin) E", AllE, S),
296    {ok, _}  = eval(f("(Fun)(Lin) ~p", [[E1, E6]]), [E1, E6], S),
297    {ok, _} = eval("(Mod) E", AllMs, S),
298    {ok, _} = eval(f("(Mod) ~p", [[E1, E6]]), [{m1,m2},{m2,m3}], S),
299    {ok, _} = eval(f("(App) ~p", [As]), As, S),
300    {ok, _} = eval("(App) [m1->m2,m2->m3]", [{a1,a2},{a2,a3}], S),
301    {ok, _} = eval(f("(Rel) ~p", [As]), Rs, S),
302    {ok, _} = eval("(Rel) a1->a2", [{r1,r1}], S),
303
304    % special 1 step
305    {ok, _} = eval("(XXL) (Lin) (Fun) E", AllCallAt, S),
306    {ok, _} = eval("(XXL) (XXL) (Lin) (Fun) E", AllCallAt, S),
307
308    {ok, _} = eval(f("(XXL) (Lin) ~p", [[E1, E6]]),
309                   [{{D1,D3},[13]}, {{D7,D4},[12]}], S),
310    {ok, _} = eval(f("(Fun) ~p", [AllMs]), AllE, S),
311    {ok, _} = eval("(Fun) [m1->m2,m2->m3]", [E1,E2,E6], S),
312    {ok, _} = eval(f("(Mod) ~p", [As]), Ms, S),
313    {ok, _} = eval("(Mod) [a1->a2,a2->a3]", [{m1,m2},{m2,m3}], S),
314    {ok, _} = eval(f("(App) ~p", [Rs]), As, S),
315    {ok, _} = eval("(App) r1->r1", [{a1,a2},{a2,a1}], S),
316    ok.
317
318%% Inter Call Graph
319intergraph(Conf) when is_list(Conf) ->
320    S0 = new(),
321
322    F1 = {m1,f1,1}, % X
323    F2 = {m1,f2,2}, % X
324    F3 = {m1,f3,3},
325    F4 = {m1,f4,4},
326    F5 = {m1,f5,5},
327
328    F6 = {m2,f1,6}, % X
329    F7 = {m2,f1,7},
330    F8 = {m2,f1,8},
331    F9 = {m2,f1,9},
332    F10 = {m2,f1,10},
333    F11 = {m2,f1,11},
334
335    % Note: E1 =:= E4!
336    E1 = {F2,F1},
337    E2 = {F2,F3},
338    E3 = {F3,F1},
339    E4 = {F2,F1}, % X
340    E5 = {F4,F2},
341    E6 = {F5,F4},
342    E7 = {F4,F5},
343
344    E8 = {F6,F7},
345    E9 = {F7,F8},
346    E10 = {F8,F1}, % X
347    E11 = {F6,F9},
348    E12 = {F6,F10},
349    E13 = {F9,F11},
350    E14 = {F10,F11},
351    E15 = {F11,F1}, % X
352
353    D1 = {F1,1},
354    D2 = {F2,2},
355    D3 = {F3,3},
356    D4 = {F4,4},
357    D5 = {F5,5},
358    DefAt_m1 = [D1,D2,D3,D4,D5],
359    X_m1 = [F1,F2],
360    % L_m1 = [F3,F4,F5],
361    XC_m1 = [E4],
362    LC_m1 = [E1,E2,E3,E5,E6,E7],
363    % Note: E1 and E4 together!
364    LCallAt_m1 = [{E1,1},{E2,2},{E3,3},{E5,5},{E6,6},{E7,7}],
365    XCallAt_m1 = [{E1,4}],
366    Info1 = #xref_mod{name = m1, app_name = [a1]},
367    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
368                    XC_m1, LC_m1),
369
370    D6 = {F6,6},
371    D7 = {F7,7},
372    D8 = {F8,8},
373    D9 = {F9,9},
374    D10 = {F10,10},
375    D11 = {F11,11},
376    DefAt_m2 = [D6,D7,D8,D9,D10,D11],
377    X_m2 = [F6],
378    % L_m2 = [F7,F8,F9,F10,F11],
379    XC_m2 = [E10,E15],
380    LC_m2 = [E8,E9,E11,E12,E13,E14],
381    LCallAt_m2 = [{E8,8},{E9,9},{E11,11},{E12,12},{E13,13},{E14,14}],
382    XCallAt_m2 = [{E10,10},{E15,15}],
383    Info2 = #xref_mod{name = m2, app_name = [a2]},
384    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
385                    XC_m2, LC_m2),
386
387    AppInfo1 = #xref_app{name = a1, rel_name = [r1]},
388    S5 = add_application(S2, AppInfo1),
389    AppInfo2 = #xref_app{name = a2, rel_name = [r1]},
390    S6 = add_application(S5, AppInfo2),
391
392    RelInfo = #xref_rel{name = r1},
393    S7 = add_release(S6, RelInfo),
394
395    S = set_up(S7),
396
397    {ok, _} = eval("EE | m1", [E1,E5,E6,E7], S),
398    {ok, _} = eval("EE | m2", [{F6,F1}], S),
399    {ok, _} = eval("EE | m2 + EE | m2", [{F6,F1}], S),
400
401    {ok, _} = eval("(Fun)(Lin)(E | m1)",
402                   to_external(union(set(XC_m1), set(LC_m1))), S),
403    {ok, _} = eval("(XXL)(ELin) (EE | m1)",
404                   [{{D2,D1},[1,2,4]},{{D4,D2},[5]},{{D5,D4},[6]},{{D4,D5},[7]}],
405                   S),
406    {ok, _} = eval("(XXL)(ELin)(EE | m2)", [{{D6,D1},[8,11,12]}], S),
407    {ok, _} = eval("(XXL)(ELin)(ELin)(EE | m2)",
408                   [{{D6,D1},[8,11,12]}], S),
409
410    %% Combining graphs (equal or different):
411    {ok, _} = eval("(XXL)(ELin)(EE | m2 + EE | m2)",
412                   [{{D6,D1},[8,11,12]}], S),
413    {ok, _} = eval("(XXL)(ELin)(EE | m2 * EE | m2)",
414                   [{{D6,D1},[8,11,12]}], S),
415    {ok, _} = eval("(XXL)(ELin)(EE | m2 - EE | m1)",
416                   [{{D6,D1},[8,11,12]}], S),
417    {ok, _} = eval("(XXL)(ELin)(EE | m2 - E | m2)",
418                   [{{D6,D1},[8,11,12]}], S),
419    {ok, _} = eval("(XXL)(ELin)(Fun)(ELin)(EE | m2)",
420                   [{{D6,D1},[8,11,12]}], S),
421    {ok, _} = eval("EE | m1 + E | m1", LC_m1, S),
422    {ok, _} = eval(f("EE | ~p + E | ~p", [F2, F2]), [E1,E2], S),
423    %% [1,4] from 'calls' is a subset of [1,2,4] from Inter Call Graph:
424    {ok, _} = eval(f("(XXL)(Lin) (E | ~p)", [F2]),
425                   [{{D2,D1},[1,4]},{{D2,D3},[2]}], S),
426
427    {ok, _} = eval(f("(XXL)(ELin) (EE | ~p)", [F2]),
428                   [{{D2,D1},[1,2,4]}], S),
429    {ok, _} = eval(f("(XXL)((ELin)(EE | ~p) + (Lin)(E | ~p))", [F2, F2]),
430                   [{{D2,D1},[1,2,4]},{{D2,D3},[2]}], S),
431    {ok, _} =
432    eval(f("(XXL)((ELin) ~p + (Lin) ~p)", [{F2, F1}, {F2, F1}]),
433         [{{D2,D1},[1,2,4]}], S),
434    {ok, _} = eval(f("(Fun)(Lin) ~p", [{F2, F1}]), [E1], S),
435    %% The external call E4 is included in the reply:
436    {ok, _} = eval("(XXL)(Lin)(LC | m1)",
437                   [{{D2,D1},[1,4]},{{D2,D3},[2]},{{D3,D1},[3]},
438                    {{D4,D2},[5]},{{D4,D5},[7]},{{D5,D4},[6]}], S),
439    %% The local call E1 is included in the reply:
440    {ok, _} = eval("(XXL)(Lin)(XC | m1)", [{{D2,D1},[1,4]}], S),
441
442    {ok, _} = eval(f("(LLin) (E | ~p || ~p) + (XLin) (E | ~p || ~p)",
443                     [F2, F1, F2, F1]), [{E4,[1,4]}], S),
444
445    {ok, _} = eval("# (ELin) E", 6, S),
446
447    ok.
448
449%% More test of Inter Call Graph, and regular expressions
450lines(Conf) when is_list(Conf) ->
451    S0 = new(),
452
453    F1 = {m1,f1,1}, % X
454    F2 = {m1,f2,2},
455    F3 = {m1,f3,3},
456    F4 = {m2,f4,4}, % X
457    F5 = {m1,f5,5}, % X
458    F6 = {m1,f6,6},
459
460    E1 = {F1,F2},
461    E2 = {F2,F1}, % X
462    E3 = {F3,F2},
463    E4 = {F1,F4}, % X
464    E5 = {F2,F4}, % X
465    E6 = {F5,F6},
466    E7 = {F6,F4}, % X
467
468    D1 = {F1,1},
469    D2 = {F2,2},
470    D3 = {F3,3},
471    D4 = {F4,4},
472    D5 = {F5,5},
473    D6 = {F6,6},
474
475    DefAt_m1 = [D1,D2,D3,D5,D6],
476    X_m1 = [F1,F5],
477    % L_m1 = [F2,F3,F6],
478    XC_m1 = [E4,E5,E7],
479    LC_m1 = [E1,E2,E3,E6],
480    LCallAt_m1 = [{E1,1},{E3,3},{E6,6}],
481    XCallAt_m1 = [{E2,2},{E4,4},{E5,5},{E7,7}],
482    Info1 = #xref_mod{name = m1, app_name = [a1]},
483    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
484                    XC_m1, LC_m1),
485
486    DefAt_m2 = [D4],
487    X_m2 = [F4],
488    % L_m2 = [],
489    XC_m2 = [],
490    LC_m2 = [],
491    LCallAt_m2 = [],
492    XCallAt_m2 = [],
493    Info2 = #xref_mod{name = m2, app_name = [a2]},
494    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
495                    XC_m2, LC_m2),
496
497    AppInfo1 = #xref_app{name = a1, rel_name = [r1]},
498    S5 = add_application(S2, AppInfo1),
499    AppInfo2 = #xref_app{name = a2, rel_name = [r1]},
500    S6 = add_application(S5, AppInfo2),
501
502    RelInfo = #xref_rel{name = r1},
503    S7 = add_release(S6, RelInfo),
504
505    S = set_up(S7),
506
507    {ok, _} = eval("(XXL) (ELin) (EE | m1)",
508                   [{{D1,D1},[1]},{{D1,D4},[1,4]},{{D3,D1},[3]},{{D3,D4},[3]},
509                    {{D5,D4},[6]}], S),
510    {ok, _} = eval("(XXL)(Lin) (E | m1)",
511                   [{{D1,D2},[1]},{{D1,D4},[4]},{{D2,D1},[2]},
512                    {{D2,D4},[5]},{{D3,D2},[3]},{{D5,D6},[6]},{{D6,D4},[7]}],
513                   S),
514    {ok, _} = eval("(E | m1) + (EE | m1)",
515                   [E1,E2,E3,E4,E5,E6,E7,{F1,F1},{F3,F1},{F3,F4},{F5,F4}],
516                   S),
517    {ok, _} = eval("(Lin)(E | m1)",
518                   [{E4,[4]},{E1,[1]},{E2,[2]},{E5,[5]},
519                    {E3,[3]},{E7,[7]},{E6,[6]}], S),
520    {ok, _} = eval("(ELin)(EE | m1)",
521                   [{{F1,F1},[1]},{{F1,F4},[1,4]},{{F3,F1},[3]},{{F3,F4},[3]},
522                    {{F5,F4},[6]}], S),
523    {ok, _} = eval("(Lin)(E | m1) + (ELin)(EE | m1)",
524                   [{E4,[1,4]},{E1,[1]},{E2,[2]},{E5,[5]},
525                    {E3,[3]},{E7,[7]},{E6,[6]},
526                    {{F1,F1},[1]},{{F3,F1},[3]},{{F3,F4},[3]},
527                    {{F5,F4},[6]}], S),
528    {ok, _} = eval("(Lin)(E | m1) - (ELin)(EE | m1)",
529                   [{E1,[1]},{E2,[2]},{E5,[5]},
530                    {E3,[3]},{E7,[7]},{E6,[6]}], S),
531    {ok, _} = eval("(Lin)(E | m1) * (ELin)(EE | m1)",
532                   [{E4,[4]}], S),
533    {ok, _} = eval("(XXL)(Lin) (E | m1)",
534                   [{{D1,D4},[4]},{{D1,D2},[1]},{{D2,D1},[2]},{{D2,D4},[5]},
535                    {{D3,D2},[3]},{{D6,D4},[7]},{{D5,D6},[6]}], S),
536    {ok, _} = eval("(XXL)(ELin) (EE | m1)",
537                   [{{D1,D1},[1]},{{D1,D4},[1,4]},{{D3,D1},[3]},{{D3,D4},[3]},
538                    {{D5,D4},[6]}], S),
539    {ok, _} = eval("(XXL)(Lin)(Fun)(Lin) (E | m1)",
540                   [{{D1,D4},[4]},{{D1,D2},[1]},{{D2,D1},[2]},{{D2,D4},[5]},
541                    {{D3,D2},[3]},{{D6,D4},[7]},{{D5,D6},[6]}], S),
542    {ok, _} = eval("(XXL)(ELin)(Fun)(ELin) (EE | m1)",
543                   [{{D1,D1},[1]},{{D1,D4},[1,4]},{{D3,D1},[3]},{{D3,D4},[3]},
544                    {{D5,D4},[6]}], S),
545
546    %% A few tests on regexp.
547    {ok, _} = eval("\"(foo\":Mod", parse_error, S),
548    {ok, _} = eval("_Foo:_/_", parse_error, S),
549    {ok, _} = eval("\".*foo\"", parse_error, S),
550    {ok, _} = eval("_:_/_:Lin", parse_error, S),
551    {ok, _} = eval("_:_/_:Mod", parse_error, S),
552    {ok, _} = eval("_:_/_:App", parse_error, S),
553    {ok, _} = eval("_:_/_:Rel", parse_error, S),
554    {ok, _} = eval("m2:_/4", [F4], S),
555    {ok, _} = eval("m2:_/4:Fun", [F4], S),
556    {ok, _} = eval("\"m.?\":\"f.*\"/\"6\"", [F6], S),
557    {ok, _} = eval("_:_/6", [F6], S),
558    {ok, _} = eval("m1:\"f1\"/_", [F1], S),
559    {ok, _} = eval("\"m1\":f1/_", [F1], S),
560    {ok, _} = eval("\"m1\":Mod", [m1], S),
561    {ok, _} = eval("\"a1\":App", [a1], S),
562    {ok, _} = eval("\"r1\":Rel", [r1], S),
563    {ok, _} = eval("_:_/-1", [], S),
564
565    ok.
566
567%% More Inter Call Graph, loops and "unusual" cases
568loops(Conf) when is_list(Conf) ->
569    S0 = new(),
570
571    F1 = {m1,f1,1}, % X
572    F2 = {m1,f2,2},
573    F3 = {m1,f3,3}, % X
574    F4 = {m1,f4,4},
575    F5 = {m1,f5,5},
576    F6 = {m1,f1,6}, % X
577    F7 = {m1,f1,7},
578
579    E1 = {F1,F1}, % X
580    E2 = {F2,F2},
581    E3 = {F3,F4},
582    E4 = {F4,F5},
583    E5 = {F5,F3}, % X
584
585    D1 = {F1,1},
586    D2 = {F2,2},
587    D3 = {F3,3},
588    D4 = {F4,4},
589    D5 = {F5,5},
590    D6 = {F6,6},
591    D7 = {F7,7},
592    DefAt_m1 = [D1,D2,D3,D4,D5,D6,D7],
593    X_m1 = [F1,F3,F6],
594    % L_m1 = [F2,F4,F5],
595    XC_m1 = [],
596    LC_m1 = [E1,E2,E3,E4,E5],
597    LCallAt_m1 = [{E2,2},{E3,3},{E4,4}],
598    XCallAt_m1 = [{E1,1},{E5,5}],
599    Info1 = #xref_mod{name = m1, app_name = [a1]},
600    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
601                    XC_m1, LC_m1),
602
603    S = set_up(S1),
604
605    % Neither F6 nor F7 is included. Perhaps one should change that?
606    {ok, _} = eval("EE | m1", [E1,E2,{F3,F3}], S),
607    {ok, _} = eval(f("(XXL)(ELin) (EE | ~p)", [F3]), [{{D3,D3},[3]}], S),
608
609    {ok, _} = eval("m1->m1 | m1->m1", type_error, S),
610    {ok, _} = eval(f("~p | ~p", [F2, F1]), type_error, S),
611
612    {ok, _} = eval(f("range (closure EE | ~p)", [F1]), [F1], S),
613    {ok, _} = eval(f("domain (closure EE || ~p)", [F3]), [F3], S),
614
615    {ok, _} = eval(f("domain (closure E || ~p)", [F3]), [F3,F4,F5], S),
616
617    {ok, _} = eval("components E", [[F1],[F2],[F3,F4,F5]], S),
618    {ok, _} = eval("components EE", [[F1],[F2],[F3]], S),
619
620    ok.
621
622%% Simple tests when there is no data
623no_data(Conf) when is_list(Conf) ->
624    S0 = new(),
625    S1 = set_up(S0),
626    {ok, _} = eval("M", [], S1),
627    {ok, _} = eval("A", [], S1),
628    {ok, _} = eval("R", [], S1),
629
630    ModInfo = #xref_mod{name = m, app_name = []},
631    S2 = add_module(S1, ModInfo, [], [], [], [], [], []),
632    AppInfo = #xref_app{name = a, rel_name = []},
633    S3 = add_application(S2, AppInfo),
634    RelInfo = #xref_rel{name = r, dir = ""},
635    S4 = add_release(S3, RelInfo),
636    S5 = set_up(S4),
637    {ok, _} = eval("M", [m], S5),
638    {ok, _} = eval("A", [a], S5),
639    {ok, _} = eval("R", [r], S5),
640    ok.
641
642%% Modules mode
643modules(Conf) when is_list(Conf) ->
644    CopyDir = ?copydir,
645    Dir = fname(CopyDir, "rel2"),
646    X = fname(Dir, "x.erl"),
647    Y = fname(Dir, "y.erl"),
648    A1_1 = fname([Dir,"lib","app1-1.1"]),
649    A2 = fname([Dir,"lib","app2-1.1"]),
650    EB1_1 = fname(A1_1, "ebin"),
651    EB2 = fname(A2, "ebin"),
652    Xbeam = fname(EB2, "x.beam"),
653    Ybeam = fname(EB1_1, "y.beam"),
654
655    {ok, x} = compile:file(X, [debug_info, {outdir,EB2}]),
656    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
657
658    {ok, S0} = xref_base:new([{xref_mode, modules}]),
659    {ok, release2, S1} =
660    xref_base:add_release(S0, Dir, [{name,release2}]),
661    S = set_up(S1),
662    {{error, _, {unavailable_analysis, undefined_function_calls}}, _} =
663    xref_base:analyze(S, undefined_function_calls),
664    {{error, _, {unavailable_analysis, locals_not_used}}, _} =
665    xref_base:analyze(S, locals_not_used),
666    {{error, _, {unavailable_analysis, {call, foo}}}, _} =
667    xref_base:analyze(S, {call, foo}),
668    {{error, _, {unavailable_analysis, {use, foo}}}, _} =
669    xref_base:analyze(S, {use, foo}),
670    {ok, _} = analyze(undefined_functions, [{x,undef,0}], S),
671    5 = length(xref_base:info(S)),
672
673    %% More: all info, conversions.
674
675    ok = file:delete(Xbeam),
676    ok = file:delete(Ybeam),
677    ok = xref_base:delete(S),
678    ok.
679
680
681%% Add modules, applications, releases, directories
682add(Conf) when is_list(Conf) ->
683    CopyDir = ?copydir,
684    Dir = fname(CopyDir, "rel2"),
685    UDir = fname([CopyDir,"dir","unreadable"]),
686    DDir = fname(CopyDir,"dir"),
687    UFile = fname([DDir, "dir","unreadable.beam"]),
688    X = fname(Dir, "x.erl"),
689    Y = fname(Dir, "y.erl"),
690    A1_1 = fname([Dir,"lib","app1-1.1"]),
691    A2 = fname([Dir,"lib","app2-1.1"]),
692    EB1_1 = fname(A1_1, "ebin"),
693    EB2 = fname(A2, "ebin"),
694    Xbeam = fname(EB2, "x.beam"),
695    Ybeam = fname(EB1_1, "y.beam"),
696
697    {ok, x} = compile:file(X, [debug_info, {outdir,EB2}]),
698    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
699
700    case os:type() of
701        {unix, _} ->
702            make_udir(UDir),
703            make_ufile(UFile);
704        _ ->
705            true
706    end,
707
708    {error, _, {invalid_options,[not_an_option] }} =
709    xref_base:new([not_an_option]),
710    {error, _, {invalid_options,[{verbose,not_a_value}] }} =
711    xref_base:new([{verbose,not_a_value}]),
712    S = new(),
713    {error, _, {invalid_options,[not_an_option]}} =
714    xref_base:set_up(S, [not_an_option]),
715    {error, _, {invalid_options,[{builtins,true},not_an_option]}} =
716    xref_base:add_directory(S, foo, [{builtins,true},not_an_option]),
717    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
718    xref_base:add_directory(S, foo, [{builtins,not_a_value}]),
719    {error, _, {invalid_filename,{foo,bar}}} =
720    xref_base:add_directory(S, {foo,bar}, []),
721    {error, _, {invalid_options,[{builtins,true},not_an_option]}} =
722    xref_base:add_module(S, foo, [{builtins,true},not_an_option]),
723    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
724    xref_base:add_module(S, foo, [{builtins,not_a_value}]),
725    {error, _, {invalid_filename,{foo,bar}}} =
726    xref_base:add_module(S, {foo,bar}, []),
727    {error, _, {invalid_options,[{builtins,true},not_an_option]}} =
728    xref_base:add_application(S, foo, [{builtins,true},not_an_option]),
729    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
730    xref_base:add_application(S, foo, [{builtins,not_a_value}]),
731    {error, _, {invalid_filename,{foo,bar}}} =
732    xref_base:add_application(S, {foo,bar}, []),
733    {error, _, {invalid_options,[not_an_option]}} =
734    xref_base:add_release(S, foo, [not_an_option]),
735    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
736    xref_base:add_release(S, foo, [{builtins,not_a_value}]),
737    {error, _, {invalid_filename,{foo,bar}}} =
738    xref_base:add_release(S, {foo,bar}, []),
739    {ok, S1} =
740    xref_base:set_default(S, [{verbose,false}, {warnings, false}]),
741    case os:type() of
742        {unix, _} ->
743            {error, _, {file_error, _, _}} =
744            xref_base:add_release(S, UDir),
745            true;
746        _ ->
747            true
748    end,
749    {error, _, {file_error, _, _}} =
750    xref_base:add_release(S, fname(["/a/b/c/d/e/f","__foo"])),
751    {ok, release2, S2} =
752    xref_base:add_release(S1, Dir, [{name,release2}]),
753    {error, _, {module_clash, {x, _, _}}} =
754    xref_base:add_module(S2, Xbeam),
755    {ok, S3} = xref_base:remove_release(S2, release2),
756    {ok, rel2, S4} = xref_base:add_release(S3, Dir),
757    {error, _, {release_clash, {rel2, _, _}}} =
758    xref_base:add_release(S4, Dir),
759    {ok, S5} = xref_base:remove_release(S4, rel2),
760    %% One unreadable file and one JAM file found (no verification here):
761    {ok, [], S6} = xref_base:add_directory(S5, fname(CopyDir,"dir"),
762                                           [{recurse,true}, {warnings,true}]),
763    case os:type() of
764        {unix, _} ->
765            {error, _, {file_error, _, _}} =
766            xref_base:add_directory(S6, UDir),
767            true;
768        _ ->
769            true
770    end,
771    {ok, app1, S7} = xref_base:add_application(S6, A1_1),
772    {error, _, {application_clash, {app1, _, _}}} =
773    xref_base:add_application(S7, A1_1),
774    {ok, S8} = xref_base:remove_application(S7, app1),
775    ok = xref_base:delete(S8),
776    ok = file:delete(Xbeam),
777    ok = file:delete(Ybeam),
778    case os:type() of
779        {unix, _} ->
780            ok = file:del_dir(UDir),
781            ok = file:delete(UFile);
782        _ ->
783            true
784    end,
785    ok.
786
787%% Default values of options
788default(Conf) when is_list(Conf) ->
789    S = new(),
790    {error, _, {invalid_options,[not_an_option]}} =
791    xref_base:set_default(S, not_an_option, true),
792    {error, _, {invalid_options,[{builtins, not_a_value}]}} =
793    xref_base:set_default(S, builtins, not_a_value),
794    {error, _, {invalid_options,[not_an_option]}} =
795    xref_base:get_default(S, not_an_option),
796    {error, _, {invalid_options,[not_an_option]}} =
797    xref_base:set_default(S, [not_an_option]),
798
799    D = xref_base:get_default(S),
800    [{builtins,false},{recurse,false},{verbose,false},{warnings,true}] =
801    D,
802
803    ok = xref_base:delete(S),
804    ok.
805
806%% The info functions
807info(Conf) when is_list(Conf) ->
808    CopyDir = ?copydir,
809    Dir = fname(CopyDir,"rel2"),
810    LDir = fname(CopyDir,"lib_test"),
811    X = fname(Dir, "x.erl"),
812    Y = fname(Dir, "y.erl"),
813    A1_1 = fname([Dir,"lib","app1-1.1"]),
814    A2 = fname([Dir,"lib","app2-1.1"]),
815    EB1_1 = fname(A1_1, "ebin"),
816    EB2 = fname(A2, "ebin"),
817    Xbeam = fname(EB2, "x.beam"),
818    Ybeam = fname(EB1_1, "y.beam"),
819
820    {ok, x} = compile:file(X, [debug_info, {outdir,EB2}]),
821    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
822
823    {ok, _} = start(s),
824    {error, _, {no_such_info, release}} = xref:info(s, release),
825    {error, _, {no_such_info, release}} = xref:info(s, release, rel),
826    {error, _, {no_such_module, mod}} = xref:info(s, modules, mod),
827    {error, _, {no_such_application, app}} =
828    xref:info(s, applications, app),
829    {error, _, {no_such_release, rel}} = xref:info(s, releases, rel),
830    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
831    {ok, rel2} = xref:add_release(s, Dir),
832    9 = length(xref:info(s)),
833    [{x,_}, {y, _}] = xref:info(s, modules),
834    [{app1,_}, {app2, _}] = xref:info(s, applications),
835    [{rel2,_}] = xref:info(s, releases),
836    [] = xref:info(s, libraries),
837    [{x,_}] = xref:info(s, modules, x),
838    [{rel2,_}] = xref:info(s, releases, rel2),
839    {error, _, {no_such_library, foo}} = xref:info(s, libraries, [foo]),
840
841    {ok, lib1} =
842    compile:file(fname(LDir,lib1),[debug_info,{outdir,LDir}]),
843    {ok, lib2} =
844    compile:file(fname(LDir,lib2),[debug_info,{outdir,LDir}]),
845    ok = xref:set_library_path(s, [LDir], [{verbose,false}]),
846    [{lib1,_}, {lib2, _}] = xref:info(s, libraries),
847    [{lib1,_}, {lib2, _}] = xref:info(s, libraries, [lib1,lib2]),
848    ok = file:delete(fname(LDir, "lib1.beam")),
849    ok = file:delete(fname(LDir, "lib2.beam")),
850
851    check_state(s),
852
853    xref:stop(s),
854
855    ok = file:delete(Xbeam),
856    ok = file:delete(Ybeam),
857
858    ok.
859
860%% Library modules
861lib(Conf) when is_list(Conf) ->
862    CopyDir = ?copydir,
863    Dir = fname(CopyDir,"lib_test"),
864    UDir = fname([CopyDir,"dir","non_existent"]),
865
866    {ok, lib1} = compile:file(fname(Dir,lib1),[debug_info,{outdir,Dir}]),
867    {ok, lib2} = compile:file(fname(Dir,lib2),[debug_info,{outdir,Dir}]),
868    {ok, lib3} = compile:file(fname(Dir,lib3),[debug_info,{outdir,Dir}]),
869    {ok, t} = compile:file(fname(Dir,t),[debug_info,{outdir,Dir}]),
870
871    {ok, _} = start(s),
872    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
873    {ok, t} = xref:add_module(s, fname(Dir,"t.beam")),
874    {error, _, {invalid_options,[not_an_option]}} =
875    xref:set_library_path(s, ["foo"], [not_an_option]),
876    {error, _, {invalid_path,otp}} = xref:set_library_path(s,otp),
877    {error, _, {invalid_path,[""]}} = xref:set_library_path(s,[""]),
878    {error, _, {invalid_path,[[$a | $b]]}} =
879    xref:set_library_path(s,[[$a | $b]]),
880    {error, _, {invalid_path,[otp]}} = xref:set_library_path(s,[otp]),
881    {ok, []} = xref:get_library_path(s),
882    ok = xref:set_library_path(s, [Dir], [{verbose,false}]),
883    {ok, UnknownFunctions} = xref:q(s, "U"),
884    [{lib1,unknown,0}, {lib2,local,0},
885     {lib2,unknown,0}, {unknown,unknown,0}]
886    = UnknownFunctions,
887    {ok, [{lib2,f,0},{lib3,f,0}]} = xref:q(s, "DF"),
888    {ok, []} = xref:q(s, "DF_1"),
889    {ok, [{lib2,f,0}]} = xref:q(s, "DF_2"),
890    {ok, [{lib2,f,0}]} = xref:q(s, "DF_3"),
891
892    {ok, [unknown]} = xref:q(s, "UM"),
893    {ok, UnknownDefAt} = xref:q(s, "(Lin)U"),
894    [{{lib1,unknown,0},0},{{lib2,local,0},0}, {{lib2,unknown,0},0},
895     {{unknown,unknown,0},0}] = UnknownDefAt,
896    {ok, LibFuns} = xref:q(s, "X * LM"),
897    [{lib2,f,0},{lib3,f,0}] = LibFuns,
898    {ok, LibMods} = xref:q(s, "LM"),
899    [lib1,lib2,lib3] = LibMods,
900    {ok, [{{lib2,f,0},0},{{lib3,f,0},0}]} = xref:q(s, "(Lin) (LM * X)"),
901    {ok, [{{lib1,unknown,0},0}, {{lib2,f,0},0}, {{lib2,local,0},0},
902          {{lib2,unknown,0},0}, {{lib3,f,0},0}]} = xref:q(s,"(Lin)LM"),
903    {ok,[lib1,lib2,lib3,t,unknown]} = xref:q(s,"M"),
904    {ok,[{lib2,f,0},{lib3,f,0},{t,t,0}]} = xref:q(s,"X * M"),
905    check_state(s),
906
907    copy_file(fname(Dir, "lib1.erl"), fname(Dir,"lib1.beam")),
908    ok = xref:set_library_path(s, [Dir]),
909    {error, _, _} = xref:q(s, "U"),
910
911    %% OTP-3921. AM and LM not always disjoint.
912    {ok, lib1} = compile:file(fname(Dir,lib1),[debug_info,{outdir,Dir}]),
913    {ok, lib1} = xref:add_module(s, fname(Dir,"lib1.beam")),
914    check_state(s),
915
916    {error, _, {file_error, _, _}} = xref:set_library_path(s, [UDir]),
917
918    xref:stop(s),
919    ok = file:delete(fname(Dir, "lib1.beam")),
920    ok = file:delete(fname(Dir, "lib2.beam")),
921    ok = file:delete(fname(Dir, "lib3.beam")),
922    ok = file:delete(fname(Dir, "t.beam")),
923
924    {ok, cp} = compile:file(fname(Dir,cp),[debug_info,{outdir,Dir}]),
925    {ok, _} = start(s),
926    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
927    {ok, cp} = xref:add_module(s, fname(Dir,"cp.beam")),
928    {ok, [{lists, sort, 1}]} = xref:q(s, "U"),
929    ok = xref:set_library_path(s, code_path),
930    {ok, []} = xref:q(s, "U"),
931    check_state(s),
932    xref:stop(s),
933    ok = file:delete(fname(Dir, "cp.beam")),
934    ok.
935
936%% Data read from the Abstract Code
937read(Conf) when is_list(Conf) ->
938    CopyDir = ?copydir,
939    Dir = fname(CopyDir,"read"),
940    File = fname(Dir, "read"),
941    Beam = fname(Dir, "read.beam"),
942    {ok, read} = compile:file(File, [debug_info,{outdir,Dir}]),
943    do_read(File, abstract_v2),
944    copy_file(fname(Dir, "read.beam.v1"), Beam),
945    do_read(File, abstract_v1),
946    ok = file:delete(Beam),
947    ok.
948
949do_read(File, Version) ->
950    {ok, _} = start(s),
951    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
952    {ok, read} = xref:add_module(s, File),
953
954    {U, OK, OKB} = read_expected(Version),
955
956    %% {ok, UC} = xref:q(s, "(Lin) UC"),
957    %% RR = to_external(converse(family_to_relation(family(UC)))),
958    %% lists:foreach(fun(X) -> io:format("~w~n", [X]) end, RR),
959    Unres = to_external(relation_to_family(converse(from_term(U)))),
960    {ok, Unres} =	xref:q(s, "(Lin) UC"),
961
962    %% {ok, EE} = xref:q(s, "(Lin) (E - UC)"),
963    %% AA = to_external(converse(family_to_relation(family(EE)))),
964    %% lists:foreach(fun(X) -> io:format("~w~n", [X]) end, AA),
965    Calls = to_external(relation_to_family(converse(from_term(OK)))),
966    {ok, Calls} = xref:q(s, "(Lin) (E - UC) "),
967
968    ok = check_state(s),
969    {ok, UM} = xref:q(s, "UM"),
970    true = member('$M_EXPR', UM),
971
972    {ok, X} = xref:q(s, "X"),
973    true = member({read, module_info, 0}, X),
974    false = member({foo, module_info, 0}, X),
975    false = member({erlang, module_info, 0}, X),
976    {ok, Unknowns} = xref:q(s, "U"),
977    false = member({read, module_info, 0}, Unknowns),
978    true = member({foo, module_info, 0}, Unknowns),
979    true = member({erlang, module_info, 0}, Unknowns),
980    {ok, LC} = xref:q(s, "LC"),
981    true = member({{read,bi,0},{read,bi,0}}, LC),
982
983    ok = xref:set_library_path(s, add_erts_code_path(fname(code:lib_dir(kernel),ebin))),
984    io:format("~p~n",[(catch xref:get_library_path(s))]),
985    {ok, X2} = xref:q(s, "X"),
986    ok = check_state(s),
987    true = member({read, module_info, 0}, X2),
988    false = member({foo, module_info, 0}, X2),
989    true = member({erlang, module_info, 0}, X2),
990    {ok, Unknowns2} = xref:q(s, "U"),
991    false = member({read, module_info, 0}, Unknowns2),
992    true = member({foo, module_info, 0}, Unknowns2),
993    false = member({erlang, module_info, 0}, Unknowns2),
994
995    ok = xref:remove_module(s, read),
996    {ok, read} = xref:add_module(s, File, [{builtins,true}]),
997
998    UnresB = to_external(relation_to_family(converse(from_term(U)))),
999    {ok, UnresB} = xref:q(s, "(Lin) UC"),
1000    CallsB = to_external(relation_to_family(converse(from_term(OKB)))),
1001    {ok, CallsB} = xref:q(s, "(Lin) (E - UC) "),
1002    ok = check_state(s),
1003    {ok, XU} = xref:q(s, "XU"),
1004    Erl = set([{erlang,length,1},{erlang,integer,1},
1005               {erlang,binary_to_term,1}]),
1006    [{erlang,binary_to_term,1},{erlang,length,1}] =
1007    to_external(intersection(set(XU), Erl)),
1008    xref:stop(s).
1009
1010%% What is expected when xref_SUITE_data/read/read.erl is added:
1011read_expected(Version) ->
1012    %% Line positions in xref_SUITE_data/read/read.erl:
1013    POS1 = 28, POS2 = POS1+10, POS3 = POS2+6, POS4 = POS3+6, POS5 = POS4+10,
1014    POS6 = POS5+5, POS7 = POS6+6, POS8 = POS7+6, POS9 = POS8+8,
1015    POS10 = POS9+10, POS11 = POS10+7, POS12 = POS11+8, POS13 = POS12+10,
1016    POS14 = POS13+18, POS15 = POS14+23,
1017
1018    FF = {read,funfuns,0},
1019    U = [{POS1+5,{FF,{dist,'$F_EXPR',0}}},
1020         {POS1+8,{FF,{dist,'$F_EXPR',0}}},
1021         {POS2+8,{{read,funfuns,0},{expr,'$F_EXPR',1}}},
1022         {POS3+4,{FF,{expr,'$F_EXPR',2}}},
1023         {POS4+2,{FF,{modul,'$F_EXPR',1}}},
1024         {POS4+4,{FF,{spm,'$F_EXPR',1}}},
1025         {POS4+6,{FF,{spm,'$F_EXPR',1}}},
1026         {POS4+8,{FF,{spm,'$F_EXPR',1}}},
1027         {POS5+1,{FF,{'$M_EXPR','$F_EXPR',0}}},
1028         {POS5+2,{FF,{'$M_EXPR','$F_EXPR',0}}},
1029         {POS5+3,{FF,{'$M_EXPR','$F_EXPR',0}}},
1030         {POS6+1,{FF,{'$M_EXPR','$F_EXPR',0}}},
1031         {POS6+2,{FF,{'$M_EXPR','$F_EXPR',0}}},
1032         {POS6+4,{FF,{n,'$F_EXPR',-1}}},
1033         {POS7+1,{FF,{'$M_EXPR',f,1}}},
1034         {POS7+2,{FF,{'$M_EXPR',f,1}}},
1035         {POS8+2,{FF,{hej,'$F_EXPR',1}}},
1036         {POS8+3,{FF,{t,'$F_EXPR',1}}},
1037         {POS8+5,{FF,{a,'$F_EXPR',1}}},
1038         {POS8+7,{FF,{m,'$F_EXPR',1}}},
1039         {POS9+1,{FF,{'$M_EXPR',f,1}}},
1040         {POS9+3,{FF,{a,'$F_EXPR',1}}},
1041         {POS10+1,{FF,{'$M_EXPR',foo,1}}},
1042         {POS10+2,{FF,{'$M_EXPR','$F_EXPR',1}}},
1043         {POS10+3,{FF,{'$M_EXPR','$F_EXPR',2}}},
1044         {POS10+4,{FF,{'$M_EXPR','$F_EXPR',1}}},
1045         {POS10+5,{FF,{'$M_EXPR',san,1}}},
1046         {POS10+6,{FF,{'$M_EXPR','$F_EXPR',1}}},
1047         {POS11+1,{FF,{'$M_EXPR','$F_EXPR',1}}},
1048         {POS11+2,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1049         {POS11+3,{FF,{m,f,-1}}},
1050         {POS11+4,{FF,{m,f,-1}}},
1051         {POS11+5,{FF,{'$M_EXPR','$F_EXPR',1}}},
1052         {POS11+6,{FF,{'$M_EXPR','$F_EXPR',1}}},
1053         {POS12+1,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1054         {POS12+4,{FF,{'$M_EXPR','$F_EXPR',2}}},
1055         {POS12+7,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1056         {POS12+8,{FF,{m4,f4,-1}}},
1057         {POS13+2,{FF,{debug,'$F_EXPR',0}}},
1058         {POS13+3,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1059         {POS14+8,{{read,bi,0},{'$M_EXPR','$F_EXPR',1}}}],
1060
1061    O1 = [{20,{{read,lc,0},{ets,new,0}}},
1062          {21,{{read,lc,0},{ets,tab2list,1}}},
1063          {POS1+1,{FF,{erlang,spawn,1}}},
1064          {POS1+1,{FF,{mod17,fun17,0}}},
1065          {POS1+2,{FF,{erlang,spawn,1}}},
1066          {POS1+2,{FF,{read,local,0}}},
1067          {POS1+3,{FF,{erlang,spawn,1}}},
1068          {POS1+4,{FF,{dist,func,0}}},
1069          {POS1+4,{FF,{erlang,spawn,1}}},
1070          {POS1+5,{FF,{erlang,spawn,1}}},
1071          {POS1+6,{FF,{erlang,spawn_link,1}}},
1072          {POS1+6,{FF,{mod17,fun17,0}}},
1073          {POS1+7,{FF,{dist,func,0}}},
1074          {POS1+7,{FF,{erlang,spawn_link,1}}},
1075          {POS1+8,{FF,{erlang,spawn_link,1}}},
1076          {POS2+1,{FF,{d,f,0}}},
1077          {POS2+1,{FF,{dist,func,2}}},
1078          {POS2+1,{FF,{erlang,spawn,2}}},
1079          {POS2+2,{FF,{dist,func,2}}},
1080          {POS2+2,{FF,{erlang,spawn,2}}},
1081          {POS2+2,{FF,{mod42,func,0}}},
1082          {POS2+3,{FF,{d,f,0}}},
1083          {POS2+3,{FF,{dist,func,2}}},
1084          {POS2+3,{FF,{erlang,spawn_link,2}}},
1085          {POS2+4,{FF,{dist,func,2}}},
1086          {POS2+4,{FF,{erlang,spawn_link,2}}},
1087          {POS2+4,{FF,{mod42,func,0}}},
1088          {POS3+1,{FF,{dist,func,2}}},
1089          {POS3+3,{FF,{dist,func,2}}},
1090          {POS4+1,{FF,{erlang,spawn,4}}},
1091          {POS4+1,{FF,{modul,function,0}}},
1092          {POS4+2,{FF,{erlang,spawn,4}}},
1093          {POS4+3,{FF,{dist,func,2}}},
1094          {POS4+3,{FF,{erlang,spawn,4}}},
1095          {POS4+3,{FF,{spm,spf,2}}},
1096          {POS4+4,{FF,{dist,func,2}}},
1097          {POS4+4,{FF,{erlang,spawn,4}}},
1098          {POS4+5,{FF,{dist,func,2}}},
1099          {POS4+5,{FF,{erlang,spawn_link,4}}},
1100          {POS4+5,{FF,{spm,spf,2}}},
1101          {POS4+6,{FF,{dist,func,2}}},
1102          {POS4+6,{FF,{erlang,spawn_link,4}}},
1103          {POS4+7,{FF,{read,bi,0}}},
1104          {POS4+7,{FF,{spm,spf,2}}},
1105          {POS4+8,{FF,{read,bi,0}}},
1106          {POS5+1,{FF,{erlang,spawn,1}}},
1107          {POS5+2,{FF,{erlang,spawn,1}}},
1108          {POS5+3,{FF,{erlang,spawn_link,1}}},
1109          {POS6+1,{FF,{erlang,spawn,2}}},
1110          {POS6+2,{FF,{erlang,spawn_link,2}}},
1111          {POS7+1,{FF,{erlang,spawn,4}}},
1112          {POS8+1,{FF,{hej,san,1}}},
1113          {POS8+4,{FF,{a,b,1}}},
1114          {POS8+6,{FF,{m,f,1}}},
1115          {POS9+1,{FF,{read,bi,0}}},
1116          {POS9+2,{FF,{a,b,1}}},
1117          {POS9+4,{FF,{erlang,not_a_function,1}}},
1118          {POS9+5,{FF,{mod,func,2}}},
1119          {POS9+6,{FF,{erlang,apply,1}}},
1120          {POS9+7,{FF,{math,add3,1}}},
1121          {POS9+8,{FF,{q,f,1}}},
1122          {POS10+5,{FF,{mod1,fun1,1}}},
1123          {POS12+5,{FF,{m3,f3,2}}},
1124          {POS13+1,{FF,{dm,df,1}}},
1125          {POS13+6,{{read,bi,0},{foo,module_info,0}}},
1126          {POS13+7,{{read,bi,0},{read,module_info,0}}},
1127          {POS13+9,{{read,bi,0},{t,foo,1}}},
1128          {POS14+11,{{read,bi,0},{erlang,module_info,0}}},
1129          {POS14+17,{{read,bi,0},{read,bi,0}}}],
1130
1131    OK = case Version of
1132             abstract_v1 ->
1133                 [{0,{FF,{read,'$F_EXPR',178}}},
1134                  {0,{FF,{modul,'$F_EXPR',179}}}]
1135                 ++ O1;
1136             _ ->
1137                 [{16,{FF,{read,'$F_EXPR',178}}},
1138                  {17,{FF,{modul,'$F_EXPR',179}}}]
1139                 ++
1140                 O1
1141         end,
1142
1143    %% When builtins =:= true:
1144    OKB1 = [{POS13+1,{FF,{erts_debug,apply,4}}},
1145            {POS13+2,{FF,{erts_debug,apply,4}}},
1146            {POS13+3,{FF,{erts_debug,apply,4}}},
1147            {POS1+3, {FF,{erlang,binary_to_term,1}}},
1148            {POS3+1, {FF,{erlang,spawn,3}}},
1149            {POS3+2, {FF,{erlang,spawn,3}}},
1150            {POS3+3,  {FF,{erlang,spawn_link,3}}},
1151            {POS3+4, {FF,{erlang,spawn_link,3}}},
1152            {POS4+7, {FF,{erlang,spawn_opt,4}}},
1153            {POS4+8, {FF,{erlang,spawn_opt,4}}},
1154            {POS6+4, {FF,{erlang,spawn,3}}},
1155            {POS7+2, {FF,{erlang,spawn_opt,4}}},
1156            {POS8+4,{FF,{erlang,apply,2}}},
1157            {POS8+5,{FF,{erlang,apply,2}}},
1158            {POS8+6,{FF,{erlang,apply,3}}},
1159            {POS8+7,{FF,{erlang,apply,3}}},
1160            {POS9+1,{FF,{erlang,apply,3}}},
1161            {POS9+2,{FF,{erlang,apply,2}}},
1162            {POS9+3,{FF,{erlang,apply,2}}},
1163            {POS9+4,{FF,{erlang,apply,2}}},
1164            {POS9+5,{FF,{erlang,apply,3}}},
1165            {POS9+7,{FF,{erlang,apply,2}}},
1166            {POS10+4,{FF,{erlang,apply,2}}},
1167            {POS11+1,{FF,{erlang,apply,3}}},
1168            {POS11+2,{FF,{erlang,apply,3}}},
1169            {POS11+3,{FF,{erlang,apply,3}}},
1170            {POS11+4,{FF,{erlang,apply,3}}},
1171            {POS11+6,{FF,{erlang,apply,2}}},
1172            {POS12+1,{FF,{erlang,apply,2}}},
1173            {POS12+4,{FF,{erlang,apply,2}}},
1174            {POS12+5,{FF,{erlang,apply,3}}},
1175            {POS12+7,{FF,{erlang,apply,2}}},
1176            {POS12+8,{FF,{erlang,apply,3}}},
1177            {POS13+5, {{read,bi,0},{erlang,length,1}}},
1178            {POS14+3, {{read,bi,0},{erlang,length,1}}}],
1179
1180    %% Operators (OTP-8647):
1181    OKB = case Version of
1182              abstract_v1 ->
1183                  [{POS8+3, {FF,{erlang,apply,3}}},
1184                   {POS10+1, {FF,{erlang,apply,3}}},
1185                   {POS10+6, {FF,{erlang,apply,3}}}];
1186              _ ->
1187                  [{POS13+16, {{read,bi,0},{erlang,'!',2}}},
1188                   {POS13+16, {{read,bi,0},{erlang,'-',1}}},
1189                   {POS13+16, {{read,bi,0},{erlang,self,0}}},
1190                   {POS15+1,  {{read,bi,0},{erlang,'>',2}}},
1191                   {POS15+2,  {{read,bi,0},{erlang,'-',2}}},
1192                   {POS15+2,  {{read,bi,0},{erlang,'*',2}}},
1193                   {POS15+8,  {{read,bi,0},{erlang,'/',2}}}]
1194          end
1195    ++ [{POS14+19, {{read,bi,0},{erlang,'+',2}}},
1196        {POS14+21, {{read,bi,0},{erlang,'+',2}}},
1197        {POS13+16, {{read,bi,0},{erlang,'==',2}}},
1198        {POS14+15, {{read,bi,0},{erlang,'==',2}}},
1199        {POS13+5,  {{read,bi,0},{erlang,'>',2}}},
1200        {POS14+3,  {{read,bi,0},{erlang,'>',2}}}]
1201    ++ OKB1 ++ OK,
1202
1203    {U, OK, OKB}.
1204
1205%% Data read from the Abstract Code (cont)
1206read2(Conf) when is_list(Conf) ->
1207    %% Handles the spawn_opt versions added in R9 (OTP-4180).
1208    %% Expected augmentations: try/catch, cond.
1209    CopyDir = ?copydir,
1210    Dir = fname(CopyDir,"read"),
1211    File = fname(Dir, "read2.erl"),
1212    MFile = fname(Dir, "read2"),
1213    Beam = fname(Dir, "read2.beam"),
1214    Test = <<"-module(read2).
1215              -compile(export_all).
1216
1217              f() ->
1218                  spawn_opt({read2,f}, % POS2
1219                            [f()]),
1220                  spawn_opt(fun() -> foo end, [link]),
1221                  spawn_opt(f(),
1222                            {read2,f}, [{min_heap_size,1000}]),
1223                  spawn_opt(f(),
1224                            fun() -> f() end, [flopp]),
1225                  spawn_opt(f(),
1226                            read2, f, [], []);
1227              f() ->
1228                  %% Duplicated unresolved calls are ignored:
1229                  (f())(foo,bar),(f())(foo,bar). % POS1
1230
1231              %% Warning forms must be ignored.
1232              -warning(must_not_crash).
1233             ">>,
1234    ok = file:write_file(File, Test),
1235    {ok, read2} = compile:file(File, [debug_info,{outdir,Dir}]),
1236
1237    {ok, _} = xref:start(s),
1238    {ok, read2} = xref:add_module(s, MFile),
1239    {U0, OK0} = read2_expected(),
1240
1241    U = to_external(relation_to_family(converse(from_term(U0)))),
1242    OK = to_external(relation_to_family(converse(from_term(OK0)))),
1243    {ok, U2} = xref:q(s, "(Lin) UC"),
1244    {ok, OK2} = xref:q(s, "(Lin) (E - UC)"),
1245    true = U =:= U2,
1246    true = OK =:= OK2,
1247    ok = check_state(s),
1248    xref:stop(s),
1249
1250    ok = file:delete(File),
1251    ok = file:delete(Beam),
1252    ok.
1253
1254
1255read2_expected() ->
1256    POS1 = 16,
1257    POS2 = 5,
1258    FF = {read2,f,0},
1259    U =  [{POS1,{FF,{'$M_EXPR','$F_EXPR',2}}}],
1260    OK = [{POS2,{FF,{erlang,spawn_opt,2}}},
1261          {POS2,{FF,FF}},
1262          {POS2+1,{FF,FF}},
1263          {POS2+2,{FF,{erlang,spawn_opt,2}}},
1264          {POS2+3,{FF,{erlang,spawn_opt,3}}},
1265          {POS2+3,{FF,FF}},
1266          {POS2+3,{FF,FF}},
1267          {POS2+5,{FF,{erlang,spawn_opt,3}}},
1268          {POS2+5,{FF,FF}},
1269          {POS2+6,{FF,FF}},
1270          {POS2+7,{FF,{erlang,spawn_opt,5}}},
1271          {POS2+7,{FF,FF}},
1272          {POS2+7,{FF,FF}},
1273          {POS1,{FF,FF}}],
1274    {U, OK}.
1275
1276%% Remove modules, applications, releases
1277remove(Conf) when is_list(Conf) ->
1278    S = new(),
1279    {error, _, {no_such_module, mod}} =
1280    xref_base:remove_module(S, mod),
1281    {error, _, {no_such_application, app}} =
1282    xref_base:remove_application(S, app),
1283    {error, _, {no_such_release, rel}} =
1284    xref_base:remove_release(S, rel),
1285    ok = xref_base:delete(S),
1286    ok.
1287
1288%% Replace modules, applications, releases
1289replace(Conf) when is_list(Conf) ->
1290    CopyDir = ?copydir,
1291    Dir = fname(CopyDir,"rel2"),
1292    X = fname(Dir, "x.erl"),
1293    Y = fname(Dir, "y.erl"),
1294    A1_0 = fname(Dir, fname("lib","app1-1.0")),
1295    A1_1 = fname(Dir, fname("lib","app1-1.1")),
1296    A2 = fname(Dir, fname("lib","app2-1.1")),
1297    EB1_0 = fname(A1_0, "ebin"),
1298    EB1_1 = fname(A1_1, "ebin"),
1299    Xbeam = fname(EB1_1, "x.beam"),
1300    Ybeam = fname(EB1_1, "y.beam"),
1301
1302    {ok, x} = compile:file(X, [debug_info, {outdir,EB1_0}]),
1303    {ok, x} = compile:file(X, [debug_info, {outdir,EB1_1}]),
1304    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
1305
1306    {ok, _} = start(s),
1307    {ok, false} = xref:set_default(s, verbose, false),
1308    {ok, true} = xref:set_default(s, warnings, false),
1309    {ok, rel2} = xref:add_release(s, Dir, []),
1310    {error, _, _} = xref:replace_application(s, app1, "no_data"),
1311    {error, _, {no_such_application, app12}} =
1312    xref:replace_application(s, app12, A1_0, []),
1313    {error, _, {invalid_filename,{foo,bar}}} =
1314    xref:replace_application(s, app1, {foo,bar}, []),
1315    {error, _, {invalid_options,[not_an_option]}} =
1316    xref:replace_application(s, foo, bar, [not_an_option]),
1317    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
1318    xref:replace_application(s, foo, bar, [{builtins,not_a_value}]),
1319    {ok, app1} =
1320    xref:replace_application(s, app1, A1_0),
1321    [{_, AppInfo}] = xref:info(s, applications, app1),
1322    {value, {release, [rel2]}} = keysearch(release, 1, AppInfo),
1323
1324    {error, _, {no_such_module, xx}} =
1325    xref:replace_module(s, xx, Xbeam, []),
1326    {error, _, {invalid_options,[{builtins,true},not_an_option]}} =
1327    xref:replace_module(s, foo, bar,[{builtins,true},not_an_option]),
1328    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
1329    xref:replace_module(s, foo, bar, [{builtins,not_a_value}]),
1330    {error, _, {invalid_filename,{foo,bar}}} =
1331    xref:replace_module(s, x, {foo,bar}),
1332    {ok, x} = xref:replace_module(s, x, Xbeam),
1333    [{x, ModInfo}] = xref:info(s, modules, x),
1334    {value, {application, [app1]}} =
1335    keysearch(application, 1, ModInfo),
1336
1337    {ok, x} = compile:file(X, [no_debug_info, {outdir,EB1_1}]),
1338    {error, _, {no_debug_info, _}} = xref:replace_module(s, x, Xbeam),
1339    {error, _, {module_mismatch, x,y}} =
1340    xref:replace_module(s, x, Ybeam),
1341    case os:type() of
1342        {unix, _} ->
1343            hide_file(Ybeam),
1344            {error, _, {file_error, _, _}} =
1345            xref:replace_module(s, x, Ybeam),
1346            true;
1347        _ ->
1348            true
1349    end,
1350    ok = xref:remove_module(s, x),
1351    {error, _, {no_debug_info, _}} = xref:add_module(s, Xbeam),
1352
1353    %% "app2" is ignored, the old application name is kept
1354    {ok, app1} = xref:replace_application(s, app1, A2),
1355
1356    xref:stop(s),
1357    ok = file:delete(fname(EB1_0, "x.beam")),
1358    ok = file:delete(Xbeam),
1359    ok = file:delete(Ybeam),
1360    ok.
1361
1362%% The update() function
1363update(Conf) when is_list(Conf) ->
1364    CopyDir = ?copydir,
1365    Dir = fname(CopyDir,"update"),
1366    Source = fname(Dir, "x.erl"),
1367    Beam = fname(Dir, "x.beam"),
1368    copy_file(fname(Dir, "x.erl.1"), Source),
1369    {ok, x} = compile:file(Source, [debug_info, {outdir,Dir}]),
1370
1371    {ok, _} = start(s),
1372    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
1373    {ok, [x]} = xref:add_directory(s, Dir, [{builtins,true}]),
1374    {error, _, {invalid_options,[not_an_option]}} =
1375    xref:update(s, [not_an_option]),
1376    {ok, []} = xref:update(s),
1377    {ok, [{erlang,atom_to_list,1}]} = xref:q(s, "XU"),
1378
1379    [{x, ModInfo}] = xref:info(s, modules, x),
1380    case keysearch(directory, 1, ModInfo) of
1381        {value, {directory, Dir}} -> ok
1382    end,
1383
1384    timer:sleep(2000), % make sure modification time has changed
1385    copy_file(fname(Dir, "x.erl.2"), Source),
1386    {ok, x} = compile:file(Source, [debug_info, {outdir,Dir}]),
1387    {ok, [x]} = xref:update(s, []),
1388    {ok, [{erlang,list_to_atom,1}]} = xref:q(s, "XU"),
1389
1390    timer:sleep(2000),
1391    {ok, x} = compile:file(Source, [no_debug_info,{outdir,Dir}]),
1392    {error, _, {no_debug_info, _}} = xref:update(s),
1393
1394    xref:stop(s),
1395    ok = file:delete(Beam),
1396    ok = file:delete(Source),
1397    ok.
1398
1399%% OTP-4695: Deprecated functions.
1400deprecated(Conf) when is_list(Conf) ->
1401    Dir = ?copydir,
1402    File = fname(Dir, "depr.erl"),
1403    MFile_r9c = fname(Dir, "depr_r9c"),
1404    MFile = fname(Dir, "depr"),
1405    Beam = fname(Dir, "depr.beam"),
1406    %% This file has been compiled to ?datadir/depr_r9c.beam
1407    %% using the R9C compiler. From R10B and onwards the linter
1408    %% checks the 'deprecated' attribute as well.
1409    %     Test = <<"-module(depr).
1410
1411    %               -export([t/0,f/1,bar/2,f/2,g/3]).
1412
1413    %               -deprecated([{f,1},                             % DF
1414    %                            {bar,2,eventually}]).              % DF_3
1415    %               -deprecated([{f,1,next_major_release}]).        % DF_2 (again)
1416    %               -deprecated([{frutt,0,next_version}]).          % message...
1417    %               -deprecated([{f,2,next_major_release},          % DF_2
1418    %                            {g,3,next_version},                % DF_1
1419    %                            {ignored,10,100}]).                % message...
1420    %               -deprecated([{does_not_exist,1}]).              % message...
1421
1422    %               -deprecated(foo).                               % message...
1423
1424    %               t() ->
1425    %                   frutt(1),
1426    %                   g(1,2, 3),
1427    %                   ?MODULE:f(10).
1428
1429    %               f(A) ->
1430    %                   ?MODULE:f(A,A).
1431
1432    %               f(X, Y) ->
1433    %                   ?MODULE:g(X, Y, X).
1434
1435    %               g(F, G, H) ->
1436    %                   ?MODULE:bar(F, {G,H}).
1437
1438    %               bar(_, _) ->
1439    %                   true.
1440
1441    %               frutt(_) ->
1442    %                   frutt().
1443
1444    %               frutt() ->
1445    %                   true.
1446    %              ">>,
1447
1448    %    ok = file:write_file(File, Test),
1449    %    {ok, depr_r9c} = compile:file(File, [debug_info,{outdir,Dir}]),
1450
1451    {ok, _} = xref:start(s),
1452    {ok, depr_r9c} = xref:add_module(s, MFile_r9c),
1453    M9 = depr_r9c,
1454    DF_1 = usort([{{M9,f,2},{M9,g,3}}]),
1455    DF_2 = usort(DF_1++[{{M9,f,1},{M9,f,2}},{{M9,t,0},{M9,f,1}}]),
1456    DF_3 = usort(DF_2++[{{M9,g,3},{M9,bar,2}}]),
1457    DF = usort(DF_3++[{{M9,t,0},{M9,f,1}}]),
1458
1459    {ok,DF} = xref:analyze(s, deprecated_function_calls),
1460    {ok,DF_1} =
1461    xref:analyze(s, {deprecated_function_calls,next_version}),
1462    {ok,DF_2} =
1463    xref:analyze(s, {deprecated_function_calls,next_major_release}),
1464    {ok,DF_3} =
1465    xref:analyze(s, {deprecated_function_calls,eventually}),
1466
1467    D = to_external(range(from_term(DF))),
1468    D_1 = to_external(range(from_term(DF_1))),
1469    D_2 = to_external(range(from_term(DF_2))),
1470    D_3 = to_external(range(from_term(DF_3))),
1471
1472    {ok,D} = xref:analyze(s, deprecated_functions),
1473    {ok,D_1} =
1474    xref:analyze(s, {deprecated_functions,next_version}),
1475    {ok,D_2} =
1476    xref:analyze(s, {deprecated_functions,next_major_release}),
1477    {ok,D_3} =
1478    xref:analyze(s, {deprecated_functions,eventually}),
1479
1480    ok = check_state(s),
1481    xref:stop(s),
1482
1483    Test2= <<"-module(depr).
1484
1485              -export([t/0,f/1,bar/2,f/2,g/3,string/0]).
1486
1487              -deprecated([{'_','_',eventually}]).            % DF_3
1488              -deprecated([{f,'_',next_major_release}]).      % DF_2
1489              -deprecated([{g,'_',next_version}]).            % DF_1
1490              -deprecated([{bar,2}]).                         % DF
1491              -deprecated([{string,0,\"hello\"}]).            % DF
1492
1493              t() ->
1494                  g(1,2, 3),
1495                  ?MODULE:f(10).
1496
1497              f(A) ->
1498                  ?MODULE:f(A,A).
1499
1500              f(X, Y) ->
1501                  ?MODULE:g(X, Y, X).
1502
1503              g(F, G, H) ->
1504                  ?MODULE:bar(F, {G,H}).
1505
1506              string() ->
1507                  ?MODULE:string().
1508
1509              bar(_, _) ->
1510                  ?MODULE:t().
1511             ">>,
1512
1513    ok = file:write_file(File, Test2),
1514    {ok, depr} = compile:file(File, [debug_info,{outdir,Dir}]),
1515
1516    {ok, _} = xref:start(s),
1517    {ok, depr} = xref:add_module(s, MFile),
1518
1519    M = depr,
1520    DFa_1 = usort([{{M,f,2},{M,g,3}}]),
1521    DFa_2 = usort(DFa_1++[{{M,f,1},{M,f,2}},{{M,t,0},{M,f,1}}]),
1522    DFa_3 = usort(DFa_2++[{{M,bar,2},{M,t,0}},{{M,g,3},{M,bar,2}},
1523                          {{M,string,0},{M,string,0}}]),
1524    DFa = DFa_3,
1525
1526    {ok,DFa} = xref:analyze(s, deprecated_function_calls),
1527    {ok,DFa_1} =
1528    xref:analyze(s, {deprecated_function_calls,next_version}),
1529    {ok,DFa_2} =
1530    xref:analyze(s, {deprecated_function_calls,next_major_release}),
1531    {ok,DFa_3} =
1532    xref:analyze(s, {deprecated_function_calls,eventually}),
1533
1534    ok = check_state(s),
1535    xref:stop(s),
1536
1537    %% All of the module is deprecated.
1538    Test3= <<"-module(depr).
1539
1540              -export([t/0,f/1,bar/2,f/2,g/3]).
1541
1542              -deprecated([{f,'_',next_major_release}]).      % DF_2
1543              -deprecated([{g,'_',next_version}]).            % DF_1
1544              -deprecated(module).                            % DF
1545
1546              t() ->
1547                  g(1,2, 3),
1548                  ?MODULE:f(10).
1549
1550              f(A) ->
1551                  ?MODULE:f(A,A).
1552
1553              f(X, Y) ->
1554                  ?MODULE:g(X, Y, X).
1555
1556              g(F, G, H) ->
1557                  ?MODULE:bar(F, {G,H}).
1558
1559              bar(_, _) ->
1560                  ?MODULE:t().
1561             ">>,
1562
1563    ok = file:write_file(File, Test3),
1564    {ok, depr} = compile:file(File, [debug_info,{outdir,Dir}]),
1565
1566    {ok, _} = xref:start(s),
1567    {ok, depr} = xref:add_module(s, MFile),
1568
1569    DFb_1 = usort([{{M,f,2},{M,g,3}}]),
1570    DFb_2 = usort(DFb_1++[{{M,f,1},{M,f,2}},{{M,t,0},{M,f,1}}]),
1571    DFb_3 = DFb_2,
1572    DFb = usort(DFb_2++[{{M,bar,2},{M,t,0}},{{M,g,3},{M,bar,2}}]),
1573
1574    {ok,DFb} = xref:analyze(s, deprecated_function_calls),
1575    {ok,DFb_1} =
1576    xref:analyze(s, {deprecated_function_calls,next_version}),
1577    {ok,DFb_2} =
1578    xref:analyze(s, {deprecated_function_calls,next_major_release}),
1579    {ok,DFb_3} =
1580    xref:analyze(s, {deprecated_function_calls,eventually}),
1581
1582    ok = check_state(s),
1583    xref:stop(s),
1584
1585    ok = file:delete(File),
1586    ok = file:delete(Beam),
1587    ok.
1588
1589
1590%% OTP-5152: try/catch, final (?) version.
1591trycatch(Conf) when is_list(Conf) ->
1592    Dir = ?copydir,
1593    File = fname(Dir, "trycatch.erl"),
1594    MFile = fname(Dir, "trycatch"),
1595    Beam = fname(Dir, "trycatch.beam"),
1596    Test = <<"-module(trycatch).
1597
1598              -export([trycatch/0]).
1599
1600              trycatch() ->
1601                  try
1602                     foo:bar(),
1603                     bar:foo() of
1604                        1 -> foo:foo();
1605                        2 -> bar:bar()
1606                  catch
1607                     error:a -> err:e1();
1608                     error:b -> err:e2()
1609                  after
1610                     fini:shed()
1611                  end.
1612             ">>,
1613
1614    ok = file:write_file(File, Test),
1615    {ok, trycatch} = compile:file(File, [debug_info,{outdir,Dir}]),
1616
1617    {ok, _} = xref:start(s),
1618    {ok, trycatch} = xref:add_module(s, MFile),
1619    A = trycatch,
1620    {ok,[{{{A,A,0},{bar,bar,0}},[10]},
1621         {{{A,A,0},{bar,foo,0}},[8]},
1622         {{{A,A,0},{err,e1,0}},[12]},
1623         {{{A,A,0},{err,e2,0}},[13]},
1624         {{{A,A,0},{fini,shed,0}},[15]},
1625         {{{A,A,0},{foo,bar,0}},[7]},
1626         {{{A,A,0},{foo,foo,0}},[9]}]} =
1627    xref:q(s, "(Lin) (E | trycatch:trycatch/0)"),
1628
1629    ok = check_state(s),
1630    xref:stop(s),
1631
1632    ok = file:delete(File),
1633    ok = file:delete(Beam),
1634    ok.
1635
1636
1637%% OTP-5653: fun M:F/A.
1638fun_mfa(Conf) when is_list(Conf) ->
1639    Dir = ?copydir,
1640    File = fname(Dir, "fun_mfa.erl"),
1641    MFile = fname(Dir, "fun_mfa"),
1642    Beam = fname(Dir, "fun_mfa.beam"),
1643    Test = <<"-module(fun_mfa).
1644
1645              -export([t/0, t1/0, t2/0, t3/0]).
1646
1647              t() ->
1648                  F = fun ?MODULE:t/0,
1649                  (F)().
1650
1651              t1() ->
1652                  F = fun t/0,
1653                  (F)().
1654
1655              t2() ->
1656                  fun ?MODULE:t/0().
1657
1658              t3() ->
1659                  fun t3/0().
1660             ">>,
1661
1662    ok = file:write_file(File, Test),
1663    A = fun_mfa,
1664    {ok, A} = compile:file(File, [debug_info,{outdir,Dir}]),
1665    {ok, _} = xref:start(s),
1666    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1667    {ok, [{{{A,t,0},{'$M_EXPR','$F_EXPR',0}},[7]},
1668          {{{A,t,0},{A,t,0}},[6]},
1669          {{{A,t1,0},{'$M_EXPR','$F_EXPR',0}},[11]},
1670          {{{A,t1,0},{A,t,0}},[10]},
1671          {{{A,t2,0},{A,t,0}},[14]},
1672          {{{A,t3,0},{fun_mfa,t3,0}},[17]}]} =
1673    xref:q(s, "(Lin) E"),
1674
1675    ok = check_state(s),
1676    xref:stop(s),
1677
1678    ok = file:delete(File),
1679    ok = file:delete(Beam),
1680    ok.
1681
1682%% fun M:F/A with varibles.
1683fun_mfa_vars(Conf) when is_list(Conf) ->
1684    Dir = ?copydir,
1685    File = fname(Dir, "fun_mfa_vars.erl"),
1686    MFile = fname(Dir, "fun_mfa_vars"),
1687    Beam = fname(Dir, "fun_mfa_vars.beam"),
1688    Test = <<"-module(fun_mfa_vars).
1689
1690              -export([t/1, t1/1, t2/3]).
1691
1692              t(Mod) ->
1693                  F = fun Mod:bar/2,
1694                  (F)(a, b).
1695
1696              t1(Name) ->
1697                  F = fun ?MODULE:Name/1,
1698                  (F)(a).
1699
1700              t2(Mod, Name, Arity) ->
1701                  F = fun Mod:Name/Arity,
1702                  (F)(a).
1703
1704              t3(Arity) ->
1705                  F = fun ?MODULE:t/Arity,
1706                  (F)(1, 2, 3).
1707
1708              t4(Mod, Name) ->
1709                  F = fun Mod:Name/3,
1710                  (F)(a, b, c).
1711
1712              t5(Mod, Arity) ->
1713                  F = fun Mod:t/Arity,
1714                  (F)().
1715             ">>,
1716
1717    ok = file:write_file(File, Test),
1718    A = fun_mfa_vars,
1719    {ok, A} = compile:file(File, [report,debug_info,{outdir,Dir}]),
1720    {ok, _} = xref:start(s),
1721    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1722    {ok, [{{{A,t,1},{'$M_EXPR','$F_EXPR',2}},[7]},
1723          {{{A,t,1},{'$M_EXPR',bar,2}},[6]},
1724          {{{A,t1,1},{'$M_EXPR','$F_EXPR',1}},[11]},
1725          {{{A,t1,1},{A,'$F_EXPR',1}},[10]},
1726          {{{A,t2,3},{'$M_EXPR','$F_EXPR',-1}},[14]},
1727          {{{A,t2,3},{'$M_EXPR','$F_EXPR',1}},[15]},
1728          {{{A,t3,1},{'$M_EXPR','$F_EXPR',3}},[19]},
1729          {{{A,t3,1},{fun_mfa_vars,t,-1}},[18]},
1730          {{{A,t4,2},{'$M_EXPR','$F_EXPR',3}},[22,23]},
1731          {{{A,t5,2},{'$M_EXPR','$F_EXPR',0}},[27]},
1732          {{{A,t5,2},{'$M_EXPR',t,-1}},[26]}]} =
1733    xref:q(s, "(Lin) E"),
1734
1735    ok = check_state(s),
1736    xref:stop(s),
1737
1738    ok = file:delete(File),
1739    ok = file:delete(Beam),
1740    ok.
1741
1742%% OTP-5195: A bug fix when using qlc:q/1,2.
1743qlc(Conf) when is_list(Conf) ->
1744    Dir = ?copydir,
1745    File = fname(Dir, "qlc.erl"),
1746    MFile = fname(Dir, "qlc"),
1747    Beam = fname(Dir, "qlc.beam"),
1748    Test = <<"-module(qlc).
1749
1750              -include_lib(\"stdlib/include/qlc.hrl\").
1751
1752              -export([t/0]).
1753
1754              t() ->
1755                  dets:open_file(t, []),
1756                  dets:insert(t, [{1,a},{2,b},{3,c},{4,d}]),
1757                  MS = ets:fun2ms(fun({X,Y}) when (X > 1) or (X < 5) -> {Y}
1758                                  end),
1759                  QH1 = dets:table(t, [{traverse, {select, MS}}]),
1760                  QH2 = qlc:q([{Y} || {X,Y} <- dets:table(t),
1761                                      (X > 1) or (X < 5)]),
1762                  true = qlc:info(QH1) =:= qlc:info(QH2),
1763                  dets:close(t),
1764                  ok.
1765             ">>,
1766
1767    ok = file:write_file(File, Test),
1768    A = qlc,
1769    {ok, A} = compile:file(File, [debug_info,{outdir,Dir}]),
1770    {ok, _} = xref:start(s),
1771    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1772    {ok, _} = xref:q(s, "(Lin) E"), % is can be loaded
1773
1774    ok = check_state(s),
1775    xref:stop(s),
1776
1777    ok = file:delete(File),
1778    ok = file:delete(Beam),
1779    ok.
1780
1781
1782
1783%% Simple analyses
1784analyze(Conf) when is_list(Conf) ->
1785    S0 = new(),
1786    {{error, _, {invalid_options,[not_an_option]}}, _} =
1787      xref_base:analyze(S0, undefined_function_calls, [not_an_option]),
1788    {{error, _, {invalid_query,{q}}}, _} = xref_base:q(S0,{q}),
1789    {{error, _, {unknown_analysis,foo}}, _} = xref_base:analyze(S0, foo),
1790    {{error, _, {unknown_constant,"foo:bar/-1"}}, _} =
1791      xref_base:analyze(S0, {use,{foo,bar,-1}}),
1792
1793    CopyDir = ?copydir,
1794    Dir = fname(CopyDir,"rel2"),
1795    X = fname(Dir, "x.erl"),
1796    Y = fname(Dir, "y.erl"),
1797    A1_1 = fname([Dir,"lib","app1-1.1"]),
1798    A2 = fname([Dir,"lib","app2-1.1"]),
1799    EB1_1 = fname(A1_1, "ebin"),
1800    EB2 = fname(A2, "ebin"),
1801    Xbeam = fname(EB2, "x.beam"),
1802    Ybeam = fname(EB1_1, "y.beam"),
1803
1804    {ok, x} = compile:file(X, [debug_info, {outdir,EB2}]),
1805    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
1806
1807    {ok, rel2, S1} = xref_base:add_release(S0, Dir, [{verbose,false}]),
1808    S = set_up(S1),
1809
1810    {ok, _} = analyze(undefined_function_calls, [{{x,xx,0},{x,undef,0}}], S),
1811    {ok, _} = analyze(undefined_functions, [{x,undef,0}], S),
1812    {ok, _} = analyze(locals_not_used, [{x,l,0},{x,l1,0}], S),
1813    {ok, _} = analyze(exports_not_used, [{x,xx,0},{y,t,0}], S),
1814
1815    {ok, _} = analyze(deprecated_function_calls, [{{y,t,0},{x,t,0}}], S),
1816    {ok, _} = analyze({deprecated_function_calls,next_version}, [], S),
1817    {ok, _} = analyze({deprecated_function_calls,next_major_release}, [], S),
1818    {ok, _} = analyze({deprecated_function_calls,eventually},
1819                      [{{y,t,0},{x,t,0}}], S),
1820    {ok, _} = analyze(deprecated_functions, [{x,t,0}], S),
1821    {ok, _} = analyze({deprecated_functions,next_version}, [], S),
1822    {ok, _} = analyze({deprecated_functions,next_major_release}, [], S),
1823    {ok, _} = analyze({deprecated_functions,eventually}, [{x,t,0}], S),
1824
1825    {ok, _} = analyze({call, {x,xx,0}}, [{x,undef,0}], S),
1826    {ok, _} = analyze({call, [{x,xx,0},{x,l,0}]}, [{x,l1,0},{x,undef,0}], S),
1827    {ok, _} = analyze({use, {x,l,0}}, [{x,l1,0}], S),
1828    {ok, _} = analyze({use, [{x,l,0},{x,l1,0}]}, [{x,l,0},{x,l1,0}], S),
1829
1830    {ok, _} = analyze({module_call, x}, [x], S),
1831    {ok, _} = analyze({module_call, [x,y]}, [x], S),
1832    {ok, _} = analyze({module_use, x}, [x,y], S),
1833    {ok, _} = analyze({module_use, [x,y]}, [x,y], S),
1834
1835    {ok, _} = analyze({application_call, app1}, [app2], S),
1836    {ok, _} = analyze({application_call, [app1,app2]}, [app2], S),
1837    {ok, _} = analyze({application_use, app2}, [app1,app2], S),
1838    {ok, _} = analyze({application_use, [app1,app2]}, [app1,app2], S),
1839
1840    ok = xref_base:delete(S),
1841    ok = file:delete(Xbeam),
1842    ok = file:delete(Ybeam),
1843    ok.
1844
1845%% Use of operators
1846basic(Conf) when is_list(Conf) ->
1847    S0 = new(),
1848
1849    F1 = {m1,f1,1},
1850    F6 = {m1,f2,6}, % X
1851    F2 = {m2,f1,2},
1852    F3 = {m2,f2,3}, % X
1853    F7 = {m2,f3,7}, % X
1854    F4 = {m3,f1,4}, % X
1855    F5 = {m3,f2,5},
1856
1857    UF1 = {m1,f12,17},
1858    UF2 = {m17,f17,177},
1859
1860    E1 = {F1,F3}, % X
1861    E2 = {F6,F7}, % X
1862    E3 = {F2,F6}, % X
1863    E4 = {F1,F4}, % X
1864    E5 = {F4,F5},
1865    E6 = {F7,F4}, % X
1866    E7 = {F1,F6},
1867
1868    UE1 = {F2,UF2}, % X
1869    UE2 = {F5,UF1}, % X
1870
1871    D1 = {F1,12},
1872    D6 = {F6,3},
1873    DefAt_m1 = [D1,D6],
1874    X_m1 = [F6],
1875    % L_m1 = [F1],
1876    XC_m1 = [E1,E2,E4],
1877    LC_m1 = [E7],
1878    LCallAt_m1 = [{E7,12}],
1879    XCallAt_m1 = [{E1,13},{E2,17},{E4,7}],
1880    Info1 = #xref_mod{name = m1, app_name = [a1]},
1881    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
1882                    XC_m1, LC_m1),
1883
1884    D2 = {F2,7},
1885    D3 = {F3,9},
1886    D7 = {F7,19},
1887    DefAt_m2 = [D2,D3,D7],
1888    X_m2 = [F3,F7],
1889    % L_m2 = [F2],
1890    XC_m2 = [E3,E6,UE1],
1891    LC_m2 = [],
1892    LCallAt_m2 = [],
1893    XCallAt_m2 = [{E3,96},{E6,12},{UE1,77}],
1894    Info2 = #xref_mod{name = m2, app_name = [a2]},
1895    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
1896                    XC_m2, LC_m2),
1897
1898    D4 = {F4,6},
1899    D5 = {F5,97},
1900    DefAt_m3 = [D4,D5],
1901    X_m3 = [F4],
1902    % L_m3 = [F5],
1903    XC_m3 = [UE2],
1904    LC_m3 = [E5],
1905    LCallAt_m3 = [{E5,19}],
1906    XCallAt_m3 = [{UE2,22}],
1907    Info3 = #xref_mod{name = m3, app_name = [a3]},
1908    S3 = add_module(S2, Info3, DefAt_m3, X_m3, LCallAt_m3, XCallAt_m3,
1909                    XC_m3, LC_m3),
1910
1911    Info4 = #xref_mod{name = m4, app_name = [a2]},
1912    S4 = add_module(S3, Info4, [], [], [], [], [], []),
1913
1914    AppInfo1 = #xref_app{name = a1, rel_name = [r1]},
1915    S9 = add_application(S4, AppInfo1),
1916    AppInfo2 = #xref_app{name = a2, rel_name = [r1]},
1917    S10 = add_application(S9, AppInfo2),
1918    AppInfo3 = #xref_app{name = a3, rel_name = [r2]},
1919    S11 = add_application(S10, AppInfo3),
1920
1921    RelInfo1 = #xref_rel{name = r1},
1922    S12 = add_release(S11, RelInfo1),
1923    RelInfo2 = #xref_rel{name = r2},
1924    S13 = add_release(S12, RelInfo2),
1925
1926    S = set_up(S13),
1927
1928    {ok, _} = eval("[m1,m2] + m:f/1", unknown_constant, S),
1929    {ok, _} = eval("[m1, m2, m:f/1]", type_mismatch, S),
1930
1931    {ok, _} = eval("[m1, m1->m2]", type_mismatch, S),
1932    {ok, _} = eval("components:f/1", unknown_constant, S),
1933    {ok, _} = eval("'of':f/1", unknown_constant, S),
1934    {ok, _} = eval("of:f/1", parse_error, S),
1935    {ok, _} = eval("components", unknown_constant, S),
1936    {ok, _} = eval("[components, of, closure]", parse_error, S),
1937    {ok, _} = eval("[components, 'of', closure]", unknown_constant, S),
1938
1939    {ok, _} = eval("[a1->a2,m1->m2]", type_mismatch, S),
1940    {ok, _} = eval("a1->a2,m1->m2", parse_error, S),
1941
1942    {ok, _} = eval("m1->a1", type_mismatch, S),
1943    {ok, _} = eval("[{m1,f1,1}] : App", parse_error, S),
1944    {ok, _} = eval("[{m1,f1,1}] : Fun", [F1], S),
1945    {ok, _} = eval("range X", type_error, S),
1946    {ok, _} = eval("domain X", type_error, S),
1947    {ok, _} = eval("range M", type_error, S),
1948    {ok, _} = eval("domain M", type_error, S),
1949
1950    % Misc.
1951    {ok, _} = eval("not_a_prefix_operator m1", parse_error, S),
1952    {ok, _} = eval(f("(Mod) ~p", [[F1,F6,F5]]), [m1,m3], S),
1953    {ok, _} = eval("(Lin) M - (Lin) m1",
1954                   [{F2,7},{F3,9},{F7,19},{F4,6},{F5,97},{UF2,0}], S),
1955    {ok, _} = eval(f("(Lin) M * (Lin) ~p", [[F1,F6]]),
1956                   [{F1,12},{F6,3}], S),
1957
1958    {ok, _} = eval(f("X * ~p", [[F1, F2, F3, F4, F5]]), [F3, F4], S),
1959    {ok, _} = eval("X", [F6,F3,F7,F4], S),
1960    {ok, _} = eval("X * AM", [F6,F3,F7,F4], S),
1961    {ok, _} = eval("X * a2", [F3,F7], S),
1962
1963    {ok, _} = eval("L * r1", [F1,F2], S),
1964    {ok, _} = eval("U", [UF1, UF2], S),
1965    {ok, _} = eval("U * AM", [UF1], S),
1966    {ok, _} = eval("U * UM", [UF2], S),
1967    {ok, _} = eval("XU * [m1, m2]", [F6,F3,F7,UF1], S),
1968    {ok, _} = eval("LU * [m3, m4]", [F5], S),
1969    {ok, _} = eval("UU", [F1,F2], S),
1970
1971    {ok, _} = eval("XC | m1", [E1,E2,E4], S),
1972    {ok, _} = eval(f("XC | ~p", [F1]), [E1,E4], S),
1973    {ok, _} = eval(f("(XXL) (Lin) (XC | ~p)", [F1]),
1974                   [{{D1,D3},[13]},{{D1,D4},[7]}],S),
1975    {ok, _} = eval(f("XC | (~p + ~p)", [F1, F2]), [E1,E4,E3,UE1], S),
1976    {ok, _} = eval(f("(XXL) (Lin) (XC | ~p)", [F1]),
1977                   [{{D1,D3},[13]},{{D1,D4},[7]}], S),
1978    {ok, _} = eval("LC | m3", [E5], S),
1979    {ok, _} = eval(f("LC | ~p", [F1]), [E7], S),
1980    {ok, _} = eval(f("LC | (~p + ~p)", [F1, F4]), [E7, E5], S),
1981    {ok, _} = eval("E | m1", [E1,E2,E4,E7], S),
1982    {ok, _} = eval(f("E | ~p", [F1]), [E1,E7,E4], S),
1983    {ok, _} = eval(f("E | (~p + ~p)", [F1, F2]), [E1,E7,E4,E3,UE1], S),
1984
1985    {ok, _} = eval("XC || m1", [E3,UE2], S),
1986    {ok, _} = eval(f("XC || ~p", [F6]), [E3], S),
1987    {ok, _} = eval(f("XC || (~p + ~p)", [F4, UF2]), [UE1,E4,E6], S),
1988    {ok, _} = eval("LC || m3", [E5], S),
1989    {ok, _} = eval(f("LC || ~p", [F1]), [], S),
1990    {ok, _} = eval(f("LC || ~p", [F6]), [E7], S),
1991    {ok, _} = eval(f("LC || (~p + ~p)", [F5, F6]), [E7,E5], S),
1992    {ok, _} = eval("E || m1", [E3,UE2,E7], S),
1993    {ok, _} = eval(f("E || ~p", [F6]), [E3,E7], S),
1994    {ok, _} = eval(f("E || (~p + ~p)", [F3,F4]), [E1,E4,E6], S),
1995
1996    {ok, _} = eval(f("~p + ~p", [F1,F2]), [F1,F2], S),
1997    {ok, _} = eval(f("~p * ~p", [m1,[F1,F6,F2]]), [F1,F6], S),
1998    {ok, _} = eval(f("~p * ~p", [F1,F2]), [], S),
1999
2000    %% range, domain
2001    {ok, _} = eval("range (E || m1)", [F6,UF1], S),
2002    {ok, _} = eval("domain (E || m1)", [F1,F2,F5], S),
2003    {ok, _} = eval(f("E | domain ~p", [[E1, {F2,F4}]]),
2004                   [E1,E7,E4,E3,UE1], S),
2005
2006    %% components, condensation, use, call
2007    {ok, _} = eval("(Lin) components E", type_error, S),
2008    {ok, _} = eval("components (Lin) E", type_error, S),
2009    {ok, _} = eval("components V", type_error, S),
2010    {ok, _} = eval("components E + components E", type_error, S),
2011
2012    {ok, _} = eval(f("range (closure E | ~p)", [[F1,F2]]),
2013                   [F6,F3,F7,F4,F5,UF1,UF2], S),
2014    {ok, _} =
2015    eval(f("domain (closure E || ~p)", [[UF2,F7]]), [F1,F2,F6], S),
2016    {ok, _} = eval("components E", [], S),
2017    {ok, _} = eval("components (Mod) E", [[m1,m2,m3]], S),
2018    {ok, _} = eval("components closure (Mod) E", [[m1,m2,m3]], S),
2019    {ok, _} = eval("condensation (Mod) E",
2020                   [{[m1,m2,m3],[m17]}], S),
2021    {ok, _} = eval("condensation closure (Mod) E",
2022                   [{[m1,m2,m3],[m17]}], S),
2023    {ok, _} = eval("condensation closure closure closure (Mod) E",
2024                   [{[m1,m2,m3],[m17]}], S),
2025    {ok, _} = eval("weak condensation (Mod) E",
2026                   [{[m1,m2,m3],[m1,m2,m3]},{[m1,m2,m3],[m17]},{[m17],[m17]}], S),
2027    {ok, _} = eval("strict condensation (Mod) E",
2028                   [{[m1,m2,m3],[m17]}], S),
2029    {ok, _} = eval("range condensation (Mod) E",
2030                   [[m17]], S),
2031    {ok, _} = eval("domain condensation (Mod) E",
2032                   [[m1,m2,m3]], S),
2033
2034    %% |, ||, |||
2035    {ok, _} = eval("(Lin) E || V", type_error, S),
2036    {ok, _} = eval("E ||| (Lin) V", type_error, S),
2037    {ok, _} = eval("E ||| m1", [E7], S),
2038    {ok, _} = eval("closure E ||| m1", [E7,{F1,UF1},{F6,UF1}], S),
2039    {ok, _} = eval("closure E ||| [m1,m2]",
2040                   [{F1,UF1},{F2,F7},{F1,F7},{F6,UF1},{F2,UF1},{F7,UF1},E7,E1,E2,E3], S),
2041    {ok, _} = eval("AE | a1", [{a1,a1},{a1,a2},{a1,a3}], S),
2042
2043    %% path ('of')
2044    {ok, _} = eval("(Lin) {m1,m2} of E", type_error, S),
2045    {ok, _} = eval("{m1,m2} of (Lin) E", type_error, S),
2046    [m1,m2] = eval("{m1,m2} of {m1,m2}", S),
2047    {ok, _} = eval("{m1,m2} of m1", type_error, S),
2048    {ok, _} = eval("{a3,m1} of ME", type_mismatch, S),
2049    [m1,m1] = eval("{m1} of ME", S),
2050    [m1,m1] = eval("{m1} of closure closure ME", S),
2051    false = eval("{m17} of ME", S),
2052    [m2,m1,m2] = eval("{m2} : Mod of ME", S),
2053    [m1,m2,m17] = eval("{m1, m17} of ME", S),
2054    [m1,m2,m17] = eval("m1 -> m17 of ME", S),
2055    {ok, _} = eval("[m1->m17,m17->m1] of ME", type_error, S),
2056    case eval(f("~p of E", [{F1,F7,UF1}]), S) of
2057        [F1,F6,F7,F4,F5,UF1] -> ok
2058    end,
2059    [a2,a1,a2] = eval("{a2} of AE", S),
2060
2061    %% weak/strict
2062    {ok, _} = eval("weak {m1,m2}", [{m1,m1},{m1,m2},{m2,m2}], S),
2063    {ok, _} = eval("strict [{m1,m1},{m1,m2},{m2,m2}]", [{m1,m2}], S),
2064    {ok, _} = eval("range weak [{m1,m2}] : Mod", [m1,m2], S),
2065    {ok, _} = eval("domain strict [{m1,m1},{m1,m2},{m2,m2}]", [m1], S),
2066
2067    %% #, number of
2068    {ok, _} = eval("# [{r1,r2}] : Rel", 1, S),
2069    {ok, _} = eval("# [{a3,a1}] : App", 1, S),
2070    {ok, _} = eval("# AE", 7, S),
2071    {ok, _} = eval("# ME", 8, S),
2072    {ok, _} = eval("# AE + # ME", 15, S),
2073    {ok, _} = eval("# AE * # ME", 56, S),
2074    {ok, _} = eval("# AE - # ME", -1, S),
2075    {ok, _} = eval("# E", 9, S),
2076    {ok, _} = eval("# V", 9, S),
2077    {ok, _} = eval("# (Lin) E", 9, S),
2078    {ok, _} = eval("# (ELin) E", 7, S),
2079    {ok, _} = eval("# closure E", type_error, S),
2080    {ok, _} = eval("# weak {m1,m2}", 3, S),
2081    {ok, _} = eval("#strict condensation (Mod) E", 1, S),
2082    {ok, _} = eval("#components closure (Mod) E", 1, S),
2083    {ok, _} = eval("# range strict condensation (Mod) E", 1, S),
2084    ok.
2085
2086%% The xref:m() and xref:d() functions
2087md(Conf) when is_list(Conf) ->
2088    CopyDir = ?copydir,
2089    Dir = fname(CopyDir,"md"),
2090    X = fname(Dir, "x__x.erl"),
2091    Y = fname(Dir, "y__y.erl"),
2092    Xbeam = fname(Dir, "x__x.beam"),
2093    Ybeam = fname(Dir, "y__y.beam"),
2094
2095    {error, _, {invalid_filename,{foo,bar}}} = xref:m({foo,bar}),
2096    {error, _, {invalid_filename,{foo,bar}}} = xref:d({foo,bar}),
2097
2098    {ok, x__x} = compile:file(X, [debug_info, {outdir,Dir}]),
2099    {ok, y__y} = compile:file(Y, [debug_info, {outdir,Dir}]),
2100
2101    {error, _, {no_such_module, foo_bar}} = xref:m(foo_bar),
2102    OldPath = code:get_path(),
2103    true = code:set_path([Dir | OldPath]),
2104    MInfo = xref:m(x__x),
2105    [{{x__x,t,1},{y__y,t,2}}] = info_tag(MInfo, undefined),
2106    [] = info_tag(MInfo, unused),
2107    [] = info_tag(MInfo, deprecated),
2108    DInfo = xref:d(Dir),
2109    [{{x__x,t,1},{y__y,t,2}}] = info_tag(DInfo, undefined),
2110    [{y__y,l,0},{y__y,l1,0}] = info_tag(DInfo, unused),
2111    [] = info_tag(MInfo, deprecated),
2112
2113    %% Switch from 'functions' mode to 'modules' mode.
2114    {ok, x__x} = compile:file(X, [no_debug_info, {outdir,Dir}]),
2115    {ok, y__y} = compile:file(Y, [no_debug_info, {outdir,Dir}]),
2116    MInfoMod = xref:m(x__x),
2117    [{y__y,t,2}] = info_tag(MInfoMod, undefined),
2118    [] = info_tag(MInfo, deprecated),
2119    DInfoMod = xref:d(Dir),
2120    [{y__y,t,2}] = info_tag(DInfoMod, undefined),
2121    [] = info_tag(MInfo, deprecated),
2122
2123    true = code:set_path(OldPath),
2124    ok = file:delete(Xbeam),
2125    ok = file:delete(Ybeam),
2126    ok.
2127
2128%% User queries
2129q(Conf) when is_list(Conf) ->
2130    S0 = new(),
2131    {ok, _} = eval("'foo", parse_error, S0),
2132    {ok, _} = eval("TT = E, TT = V", variable_reassigned, S0),
2133    {ok, _} = eval("TT = E, TTT", unknown_variable, S0),
2134    {ok, S} = eval("TT := E", [], S0),
2135    {ok, S1} = eval("TT * TT * TT", [], S),
2136    {ok, _S2} = xref_base:forget(S1, 'TT'),
2137    ok.
2138
2139%% Setting and getting values of query variables
2140variables(Conf) when is_list(Conf) ->
2141    Sa = new(),
2142    {{error, _, {invalid_options,[not_an_option]}}, _} =
2143    xref_base:variables(Sa, [not_an_option]),
2144    {error, _, {not_user_variable,foo}} = xref_base:forget(Sa, foo),
2145    Sa1 = set_up(Sa),
2146    {error, _, {not_user_variable,foo}} = xref_base:forget(Sa1, foo),
2147    ok = xref_base:delete(Sa1),
2148
2149    S0 = new(),
2150
2151    F1 = {m1,f1,1},
2152    F2 = {m2,f1,2},
2153    Lib = {lib1,f1,1}, % undefined
2154
2155    E1 = {F1,F2},
2156    E2 = {F2,F1},
2157    E3 = {F1,Lib},
2158
2159    D1 = {F1,12},
2160    DefAt_m1 = [D1],
2161    X_m1 = [F1],
2162    % L_m1 = [],
2163    XC_m1 = [E1,E3],
2164    LC_m1 = [],
2165    LCallAt_m1 = [],
2166    XCallAt_m1 = [{E1,13},{E3,17}],
2167    Info1 = #xref_mod{name = m1, app_name = [a1]},
2168    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
2169                    XC_m1, LC_m1),
2170
2171    D2 = {F2,7},
2172    DefAt_m2 = [D2],
2173    X_m2 = [F2],
2174    % L_m2 = [],
2175    XC_m2 = [E2],
2176    LC_m2 = [],
2177    LCallAt_m2 = [],
2178    XCallAt_m2 = [{E2,96}],
2179    Info2 = #xref_mod{name = m2, app_name = [a2]},
2180    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
2181                    XC_m2, LC_m2),
2182
2183    S = set_up(S2),
2184
2185    {ok, _} = eval("T1=E, T2=E*T1, T3 = T2*T2, T4=range T3, T5=T3|T4, T5",
2186                   [E1,E2,E3], S),
2187    {ok, _} = eval("((E*E)*(E*E)) | (range ((E*E)*(E*E)))",
2188                   [E1,E2,E3], S),
2189    {ok, _} = eval("T1=V*V,T2=T1*V,T3=V*V*V,T3",
2190                   [F1,F2,Lib], S),
2191    {ok, _} = eval("T1=V*V, T2=V*V, T1*T2",
2192                   [F1,F2,Lib], S),
2193
2194    {ok, S100} = eval("T0 := E", [E1, E2, E3], S),
2195    {ok, S101} = eval("T1 := E  | m1", [E1, E3], S100),
2196    {ok, S102} = eval("T2 := E  | m2", [E2], S101),
2197    {{ok, [{user, ['T0', 'T1', 'T2']}]}, _} = xref_base:variables(S102),
2198    {ok, S103} = xref_base:forget(S102, 'T0'),
2199    {{ok, [{user, ['T1', 'T2']}]}, S104} =
2200    xref_base:variables(S103, [user]),
2201    {ok, S105} = xref_base:forget(S104),
2202    {{ok, [{user, []}]}, S106} = xref_base:variables(S105),
2203    {{ok, [{predefined,_}]}, S107_0} =
2204    xref_base:variables(S106, [predefined]),
2205
2206    {ok, S107_1} =
2207    eval("TT := E, TT2 := V, TT1 := TT * TT", [E1,E2,E3], S107_0),
2208    {{ok, [{user, ['TT', 'TT1', 'TT2']}]}, _} =
2209    xref_base:variables(S107_1),
2210    {ok, S107} = xref_base:forget(S107_1),
2211
2212    CopyDir = ?copydir,
2213    Dir = fname(CopyDir,"lib_test"),
2214    Beam = fname(Dir, "lib1.beam"),
2215
2216    copy_file(fname(Dir, "lib1.erl"), Beam),
2217    {ok, S108} =
2218    xref_base:set_library_path(S107, [Dir], [{verbose,false}]),
2219    {{error, _, _}, _} = xref_base:variables(S108, [{verbose,false}]),
2220    {ok, S109} = xref_base:set_library_path(S108, [], [{verbose,false}]),
2221
2222    NoOfTables = erlang:system_info(ets_count),
2223
2224    {ok, S110} = eval("Eplus := closure E, TT := Eplus",
2225                      'closure()', S109),
2226    {{ok, [{user, ['Eplus','TT']}]}, S111} = xref_base:variables(S110),
2227    {ok, S112} = xref_base:forget(S111, ['TT','Eplus']),
2228    true = NoOfTables =:= erlang:system_info(ets_count),
2229
2230    {ok, NS0} = eval("Eplus := closure E", 'closure()', S112),
2231    {{ok, [{user, ['Eplus']}]}, NS} = xref_base:variables(NS0),
2232    ok = xref_base:delete(NS),
2233    true = NoOfTables =:= erlang:system_info(ets_count),
2234
2235    ok = file:delete(Beam),
2236    ok.
2237
2238%% OTP-5071. Too many unused functions.
2239unused_locals(Conf) when is_list(Conf) ->
2240    Dir = ?copydir,
2241
2242    File1 = fname(Dir, "a.erl"),
2243    MFile1 = fname(Dir, "a"),
2244    Beam1 = fname(Dir, "a.beam"),
2245    Test1 = <<"-module(a).
2246               -export([f/1, g/2]).
2247
2248               f(X) ->
2249                   Y = b:f(X),
2250                   Z = b:g(Y),
2251                   start(b, h, [Z]).
2252
2253               g(X, Y) ->
2254                   ok.
2255
2256               start(M, F, A) ->
2257                   spawn(M, F, A).
2258             ">>,
2259    ok = file:write_file(File1, Test1),
2260    {ok, a} = compile:file(File1, [debug_info,{outdir,Dir}]),
2261
2262    File2 = fname(Dir, "b.erl"),
2263    MFile2 = fname(Dir, "b"),
2264    Beam2 = fname(Dir, "b.beam"),
2265    Test2 = <<"-module(b).
2266               -export([f/1, g/2]).
2267
2268               f(X) ->
2269                   io:write(\"~w\", [X]),
2270                   a:start(timer, sleep, [1000]).
2271
2272               g(X, Y) ->
2273                   apply(a, g, [X, Y]).
2274             ">>,
2275
2276    ok = file:write_file(File2, Test2),
2277    {ok, b} = compile:file(File2, [debug_info,{outdir,Dir}]),
2278
2279    {ok, _} = xref:start(s),
2280    {ok, a} = xref:add_module(s, MFile1),
2281    {ok, b} = xref:add_module(s, MFile2),
2282    {ok, []} = xref:analyse(s, locals_not_used),
2283    ok = check_state(s),
2284    xref:stop(s),
2285
2286    ok = file:delete(File1),
2287    ok = file:delete(Beam1),
2288    ok = file:delete(File2),
2289    ok = file:delete(Beam2),
2290    ok.
2291
2292
2293%% Format error messages
2294format_error(Conf) when is_list(Conf) ->
2295    {ok, _Pid} = start(s),
2296    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
2297
2298    %% Parse error messages.
2299    "Invalid regular expression \"add(\"" ++ _ = fstring(xref:q(s,'"add("')),
2300    'Invalid operator foo\n' = fatom(xref:q(s,'foo E')),
2301    'Invalid wildcard variable \'_Var\' (only \'_\' is allowed)\n'
2302      = fatom(xref:q(s,"module:function/_Var")),
2303    'Missing type of regular expression ".*"\n'
2304      = fatom(xref:q(s,'".*"')),
2305    'Type does not match structure of constant: \'M\' : Fun\n'
2306      = fatom(xref:q(s,"'M' : Fun")),
2307    'Type does not match structure of constant: ".*" : Fun\n'
2308      = fatom(xref:q(s,'".*" : Fun')),
2309    'Type does not match structure of constant: [m:f/1, m1:f2/3] : App\n'
2310      = fatom(xref:q(s,"[m:f/1,m1:f2/3] : App")),
2311    'Parse error on line 1: syntax error before: \'-\'\n'
2312      = fatom(xref:q(s,"E + -")),
2313    "Parse error on line 1: unterminated atom starting with 'foo'\n"
2314      = flatten(xref:format_error(xref:q(s,"'foo"))),
2315    'Parse error at end of string: syntax error before: \n'
2316      = fatom(xref:q(s,"E +")),
2317    'Parse error on line 1: syntax error before: \'Lin\'\n'
2318      = fatom(xref:q(s,"Lin")),
2319
2320    %% Other messages
2321    'Variable \'QQ\' used before set\n' = fatom(xref:q(s,"QQ")),
2322    'Unknown constant a\n' = fatom(xref:q(s,"{a} of E")),
2323
2324    %% Testing xref_parser:t2s/1.
2325    'Variable assigned more than once: E := E + E\n' = fatom(xref:q(s,"E:=E + E")),
2326    'Variable assigned more than once: E = E + E\n' = fatom(xref:q(s,"E=E + E")),
2327    "Operator applied to argument(s) of different or invalid type(s): "
2328    "E + V * V\n" = flatten(xref:format_error(xref:q(s,"E + (V * V)"))),
2329    {error,xref_compiler,{type_error,"(V + V) * E"}} = xref:q(s,"(V + V) * E"),
2330    "Type does not match structure of constant: [m:f/3 -> g:h/17] : "
2331    "App\n" = flatten(xref:format_error(xref:q(s,"[{{m,f,3},{g,h,17}}] : App"))),
2332    'Type does not match structure of constant: [m -> f, g -> h] : Fun\n'
2333      = fatom(xref:q(s,"[{m,f},g->h] : Fun")),
2334    'Type does not match structure of constant: {m, n, o} : Fun\n' =
2335    fatom(xref:q(s,"{m,n,o} : Fun")),
2336    {error,xref_compiler,{type_error,"range (Lin) V"}} =
2337      xref:q(s,"range ((Lin) V)"),
2338    {error,xref_compiler,{type_error,"condensation range E"}} =
2339      xref:q(s,"condensation (range E)"),
2340    {error,xref_compiler,{type_error,"condensation (# E + # V)"}} =
2341      xref:q(s,"condensation (# E + # V)"),
2342    {error,xref_compiler,{type_error,"range (# E + # E)"}} =
2343      xref:q(s,"range (#E + #E)"),
2344    {error,xref_compiler,{type_error,"range (# E)"}} =
2345      xref:q(s,"range #E"), % Hm...
2346    {error,xref_compiler,{type_error,"E + # E"}} =
2347      xref:q(s,"E + #E + #E"), % Hm...
2348    {error,xref_compiler,{type_error,"V * E || V | V"}} =
2349      xref:q(s,"V * (E || V) | V"),
2350    {error,xref_compiler,{type_error,"E || (E | V)"}} =
2351      xref:q(s,"V * E || (E | V)"),
2352    {error,xref_compiler,{type_error,"E * \"m\" : Mod"}} =
2353      xref:q(s,'E * "m" : Mod'),
2354    {error,xref_compiler,{type_error,"E * (\"m\":f/_ + m:\"f\"/3)"}} =
2355      xref:q(s,'E * ("m":f/_ + m:"f"/3)'),
2356
2357    xref:stop(s),
2358    ok.
2359
2360%% OTP-7423. Xref scanner bug.
2361otp_7423(Conf) when is_list(Conf) ->
2362    {ok, _Pid} = start(s),
2363    S = "E | [compiler] : App || [{erlang,
2364                                   size,
2365                                   1}] : Fun",
2366    {error,xref_compiler,{unknown_constant,"compiler"}} = xref:q(s,S),
2367    xref:stop(s),
2368    ok.
2369
2370%% OTP-7831. Allow anonymous Xref processes.
2371otp_7831(Conf) when is_list(Conf) ->
2372    {ok, Pid1} = xref:start([]),
2373    xref:stop(Pid1),
2374    {ok, Pid2} = xref:start([{xref_mode, modules}]),
2375    xref:stop(Pid2),
2376    ok.
2377
2378%% OTP-10192. Allow filenames with character codes greater than 126.
2379otp_10192(Conf) when is_list(Conf) ->
2380    PrivDir = ?privdir,
2381    {ok, _Pid} = xref:start(s),
2382    Dir = filename:join(PrivDir, "ä"),
2383    ok = file:make_dir(Dir),
2384    {ok, []} = xref:add_directory(s, Dir),
2385    xref:stop(s),
2386    ok.
2387
2388otp_13708(Conf) when is_list(Conf) ->
2389    {ok, _} = start(s),
2390    ok = xref:set_default(s, [{verbose, true}]),
2391    {ok, []} = xref:q(s,"E"),
2392    xref:stop(s),
2393
2394    CopyDir = ?copydir,
2395    Dir = fname(CopyDir,"lib_test"),
2396    {ok, _} = start(s),
2397    ok = xref:set_library_path(s, [Dir], [{verbose, true}]),
2398    xref:stop(s).
2399
2400%% OTP-14464. Unicode atoms.
2401otp_14464(Conf) when is_list(Conf) ->
2402    Dir = ?copydir,
2403
2404    File1 = fname(Dir, "a.erl"),
2405    MFile1 = fname(Dir, "a"),
2406    Beam1 = fname(Dir, "a.beam"),
2407    Test1 = "-module(a).
2408             -export([ärlig/0, 'кlирилли́ческий атомB'/0]).
2409
2410              ärlig() ->
2411                  'кlирилли́ческий атомB'.
2412
2413              'кlирилли́ческий атомB'() ->
2414                  foo.
2415             ",
2416    ok = file:write_file(File1, unicode:characters_to_binary(Test1)),
2417    {ok, a} = compile:file(File1, [debug_info,{outdir,Dir}]),
2418
2419    {ok, _} = xref:start(s),
2420    {ok, a} = xref:add_module(s, MFile1),
2421
2422    {ok, [{arlig,0}]} = xref:q(s, 'a:"ärlig"/0'),
2423    {ok, [{a,'кlирилли́ческий атомB',0}]} =
2424        xref:q(s, 'a:"кlирилли́ческий атомB"/0'),
2425
2426    xref:stop(s),
2427    ok = file:delete(File1),
2428    ok = file:delete(Beam1).
2429
2430%% OTP-14344. -on_load() attribute.
2431otp_14344(Conf) when is_list(Conf) ->
2432    Dir = ?copydir,
2433
2434    File1 = fname(Dir, "a.erl"),
2435    MFile1 = fname(Dir, "a"),
2436    Beam1 = fname(Dir, "a.beam"),
2437    Test1 = <<"-module(a).
2438               -on_load(init/0).
2439               init() -> do_init().
2440               do_init() -> ok.
2441              ">>,
2442    ok = file:write_file(File1, Test1),
2443    {ok, a} = compile:file(File1, [debug_info,{outdir,Dir}]),
2444
2445    {ok, _} = xref:start(s),
2446    {ok, a} = xref:add_module(s, MFile1),
2447
2448    {ok, [{a,init,0}]} = xref:q(s, "OL"),
2449    {ok, []} = xref:analyze(s, locals_not_used),
2450
2451    xref:stop(s),
2452    ok = file:delete(File1),
2453    ok = file:delete(Beam1).
2454
2455%% PR-2752, ERL-1353, ERL-1476, GH-4192.
2456behaviour(Config) ->
2457    ModMode = [{xref_mode, modules}],
2458    FunMode = [],
2459
2460    Test1 = [{a, <<"-module(a).
2461                    -callback a() -> ok.
2462                    ">>}],
2463    {Undef1, UnusedExports1} = behaviour_test(Test1, Config, FunMode),
2464    [] = Undef1,
2465    [] = UnusedExports1,
2466    {Undef1m, UnusedExports1m} = behaviour_test(Test1, Config, ModMode),
2467    [] = Undef1m,
2468    [] = UnusedExports1m,
2469
2470    Test2 = [{a, <<"-module(a).
2471                    -export([behaviour_info/1]).
2472                    behaviour_info(_) ->
2473                        ok.
2474                   ">>}],
2475    {Undef2, UnusedExports2} = behaviour_test(Test2, Config, FunMode),
2476    [] = Undef2,
2477    [{a,behaviour_info,1}] = UnusedExports2,
2478    {Undef2m, UnusedExports2m} = behaviour_test(Test2, Config, ModMode),
2479    [] = Undef2m,
2480    %% Without abstract code it is not possible to determine if
2481    %% M:behaviour_info/1 is generated or not. The best we can do is
2482    %% to assume it is generated since it would otherwise always be
2483    %% returned as unused.
2484    [] = UnusedExports2m,
2485
2486    Test3 = [{a, <<"-module(a).
2487                    -export([behaviour_info/1]).
2488                    behaviour_info(_) ->
2489                        ok.
2490               ">>},
2491             {b, <<"-module(b).
2492                    -export([bar/0]).
2493                    bar() -> a:behaviour_info(callbacks).
2494                   ">>}],
2495    {Undef3, UnusedExports3} = behaviour_test(Test3, Config, FunMode),
2496    [] = Undef3,
2497    [{b,bar,0}] = UnusedExports3,
2498    {Undef3m, UnusedExports3m} = behaviour_test(Test3, Config, ModMode),
2499    [] = Undef3m,
2500    [{b,bar,0}] = UnusedExports3m,
2501    ok.
2502
2503behaviour_test(Tests, Conf, Opts) ->
2504    {ok, _} = xref:start(s, Opts),
2505    add_modules(Tests, Conf),
2506    case lists:keyfind(xref_mode, 1, Opts) of
2507        {xref_mode, modules} ->
2508            UndefinedFunctionCalls = [];
2509        _ ->
2510            {ok, UndefinedFunctionCalls} =
2511                xref:analyze(s, undefined_function_calls)
2512    end,
2513    {ok, ExportsNotUsed} = xref:analyze(s, exports_not_used),
2514    xref:stop(s),
2515    {UndefinedFunctionCalls, ExportsNotUsed}.
2516
2517add_modules([], _Conf) ->
2518    ok;
2519add_modules([{Mod, Test} |Tests], Conf) ->
2520    Dir = ?copydir,
2521    Name = atom_to_list(Mod),
2522    File = fname(Dir, Name ++ ".erl"),
2523    MFile = fname(Dir, Name),
2524    Beam = fname(Dir, Name ++ ".beam"),
2525    ok = file:write_file(File, Test),
2526    {ok, Mod} = compile:file(File, [debug_info,{outdir,Dir}]),
2527    {ok, Mod} = xref:add_module(s, MFile),
2528    check_state(s),
2529    ok = file:delete(File),
2530    ok = file:delete(Beam),
2531    add_modules(Tests, Conf).
2532
2533%%%
2534%%% Utilities
2535%%%
2536
2537copy_file(Src, Dest) ->
2538    {ok, _} = file:copy(Src, Dest),
2539    ok.
2540
2541fname(N) ->
2542    filename:join(N).
2543
2544fname(Dir, Basename) ->
2545    filename:join(Dir, Basename).
2546
2547new() ->
2548    {ok, S} = xref_base:new(),
2549    S.
2550
2551set_up(S) ->
2552    {ok, S1} = xref_base:set_up(S, [{verbose, false}]),
2553    S1.
2554
2555eval(Query, E, S) ->
2556    ?format("------------------------------~n", []),
2557    ?format("Evaluating ~p~n", [Query]),
2558    {Answer, NewState} = xref_base:q(S, Query, [{verbose, false}]),
2559    {Reply, Expected} =
2560    case Answer of
2561        {ok, R} when is_list(E) ->
2562            {unsetify(R), sort(E)};
2563        {ok, R} ->
2564            {unsetify(R), E};
2565        {error, _Module, Reason} ->
2566            {element(1, Reason), E}
2567    end,
2568    if
2569        Reply =:= Expected ->
2570            ?format("As expected, got ~n~p~n", [Expected]),
2571            {ok, NewState};
2572        true ->
2573            ?format("Expected ~n~p~nbut got ~n~p~n", [Expected, Reply]),
2574            not_ok
2575    end.
2576
2577analyze(Query, E, S) ->
2578    ?format("------------------------------~n", []),
2579    ?format("Evaluating ~p~n", [Query]),
2580    {{ok, L}, NewState} =
2581    xref_base:analyze(S, Query, [{verbose, false}]),
2582    case {unsetify(L), sort(E)} of
2583        {X,X} ->
2584            ?format("As was expected, got ~n~p~n", [X]),
2585            {ok, NewState};
2586        {_R,_X} ->
2587            ?format("Expected ~n~p~nbut got ~n~p~n", [_X, _R]),
2588            not_ok
2589    end.
2590
2591unsetify(S) ->
2592    case is_sofs_set(S) of
2593        true -> to_external(S);
2594        false -> S
2595    end.
2596
2597%% Note: assumes S has been set up; the new state is not returned
2598eval(Query, S) ->
2599    {{ok, Answer}, _NewState} =
2600    xref_base:q(S, Query, [{verbose, false}]),
2601    unsetify(Answer).
2602
2603add_module(S, XMod, DefAt, X, LCallAt, XCallAt, XC, LC) ->
2604    Attr = {[], [], []},
2605    Depr0 = {[], [], [], []},
2606    DBad = [],
2607    Depr = {Depr0,DBad},
2608    OL = [],
2609    Data = {DefAt, LCallAt, XCallAt, LC, XC, X, Attr, Depr, OL},
2610    Unres = [],
2611    {ok, _Module, _Bad, State} =
2612    xref_base:do_add_module(S, XMod, Unres, Data),
2613    State.
2614
2615add_application(S, XApp) ->
2616    xref_base:do_add_application(S, XApp).
2617
2618add_release(S, XRel) ->
2619    xref_base:do_add_release(S, XRel).
2620
2621remove_module(S, M) ->
2622    xref_base:do_remove_module(S, M).
2623
2624info_tag(Info, Tag) ->
2625    {value, {_Tag, Value}} = lists:keysearch(Tag, 1, Info),
2626    Value.
2627
2628make_ufile(FileName) ->
2629    ok = file:write_file(FileName, term_to_binary(foo)),
2630    hide_file(FileName).
2631
2632make_udir(Dir) ->
2633    ok = file:make_dir(Dir),
2634    hide_file(Dir).
2635
2636hide_file(FileName) ->
2637    {ok, FileInfo} = file:read_file_info(FileName),
2638    NewFileInfo = FileInfo#file_info{mode = 0},
2639    ok = file:write_file_info(FileName, NewFileInfo).
2640
2641%% Note that S has to be set up before calling this checking function.
2642check_state(S) ->
2643    Info = xref:info(S),
2644
2645    modules_mode_check(S, Info),
2646    case info(Info, mode) of
2647        modules ->
2648            ok;
2649        functions ->
2650            functions_mode_check(S, Info)
2651    end.
2652
2653%% The manual mentions some facts that should always hold.
2654%% Here they are again.
2655functions_mode_check(S, Info) ->
2656    %% F = L + X,
2657    {ok, F} = xref:q(S, "F"),
2658    {ok, F} = xref:q(S, "L + X"),
2659
2660    %% V = X + L + B + U,
2661    {ok, V} = xref:q(S, "V"),
2662    {ok, V} = xref:q(S, "X + L + B + U"),
2663
2664    %% X, L, B and U are disjoint.
2665    {ok, []} =
2666    xref:q(S, "X * L + X * B + X * U + L * B + L * U + B * U"),
2667
2668    %% V = UU + XU + LU,
2669    {ok, V} = xref:q(S, "UU + XU + LU"),
2670
2671    %% E = LC + XC
2672    {ok, E} = xref:q(S, "E"),
2673    {ok, E} = xref:q(S, "LC + XC"),
2674
2675    %% U subset of XU,
2676    {ok, []} = xref:q(S, "U - XU"),
2677
2678    %% LU = range LC
2679    {ok, []} = xref:q(S, "(LU - range LC) + (range LC - LU)"),
2680
2681    %% XU = range XC
2682    {ok, []} = xref:q(S, "(XU - range XC) + (range XC - XU)"),
2683
2684    %% LU subset F
2685    {ok, []} = xref:q(S, "LU - F"),
2686
2687    %% UU subset F
2688    {ok, []} = xref:q(S, "UU - F"),
2689
2690    %% OL subset F
2691    {ok, []} = xref:q(S, "OL - F"),
2692
2693    %% ME = (Mod) E
2694    {ok, ME} = xref:q(S, "ME"),
2695    {ok, ME} = xref:q(S, "(Mod) E"),
2696
2697    %% AE = (App) E
2698    {ok, AE} = xref:q(S, "AE"),
2699    {ok, AE} = xref:q(S, "(App) E"),
2700
2701    %% RE = (Rel) E
2702    {ok, RE} = xref:q(S, "RE"),
2703    {ok, RE} = xref:q(S, "(Rel) E"),
2704
2705    %% (Mod) V subset of M
2706    {ok, []} = xref:q(S, "(Mod) V - M"),
2707
2708    %% range UC subset of U
2709    {ok, []} = xref:q(S, "range UC - U"),
2710
2711    %% Some checks on the numbers returned by the info functions.
2712
2713    {Resolved, Unresolved} = info(Info, no_calls),
2714    AllCalls = Resolved + Unresolved,
2715    {ok, AllCalls} = xref:q(S, "# (XLin) E + # (LLin) E"),
2716
2717    {Local, Exported} = info(Info, no_functions),
2718    LX = Local+Exported,
2719    {ok, LXs} = xref:q(S, 'Extra = _:module_info/"(0|1)" + LM,
2720                                  # (F - Extra)'),
2721    true = LX =:= LXs,
2722
2723    {LocalCalls, ExternalCalls, UnresCalls} =
2724    info(Info, no_function_calls),
2725    LEU = LocalCalls + ExternalCalls + UnresCalls,
2726    {ok, LEU} = xref:q(S, "# LC + # XC"),
2727
2728    InterFunctionCalls = info(Info, no_inter_function_calls),
2729    {ok, InterFunctionCalls} = xref:q(S, "# EE"),
2730
2731    %% And some more checks on counters...
2732    check_count(S),
2733
2734    %% ... and more
2735    {ok, []} = xref:q(S, "LM - X - U - B"),
2736
2737    ok.
2738
2739modules_mode_check(S, Info) ->
2740    %% B subset of XU,
2741    {ok, []} = xref:q(S, "B - XU"),
2742
2743    %% M = AM + LM + UM
2744    {ok, M} = xref:q(S, "M"),
2745    {ok, M} = xref:q(S, "AM + LM + UM"),
2746
2747    %% DF is a subset of X U B, etc.
2748    {ok, []} = xref:q(S, "DF - X - B"),
2749    {ok, []} = xref:q(S, "DF_3 - DF"),
2750    {ok, []} = xref:q(S, "DF_2 - DF_3"),
2751    {ok, []} = xref:q(S, "DF_1 - DF_2"),
2752
2753    %% AM, LM and UM are disjoint.
2754    {ok, []} = xref:q(S, "AM * LM + AM * UM + LM * UM"),
2755
2756    %% (App) M subset of A
2757    {ok, []} = xref:q(S, "(App) M - A"),
2758
2759    AM = info(Info, no_analyzed_modules),
2760    {ok, AM} = xref:q(S, "# AM"),
2761
2762    A = info(Info, no_applications),
2763    {ok, A} = xref:q(S, "# A"),
2764
2765    NoR = info(Info, no_releases),
2766    {ok, NoR} = xref:q(S, "# R"),
2767
2768    ok.
2769
2770%% Checks the counters of some of the overall and modules info functions.
2771%% (Applications and releases are not checked.)
2772check_count(S) ->
2773    %%{ok, R} = xref:q(S, 'R'),
2774    %% {ok, A} = xref:q(S, 'A'),
2775    {ok, M} = xref:q(S, 'AM'),
2776
2777    {ok, _} = xref:q(S,
2778                     "Extra := _:module_info/\"(0|1)\" + LM"),
2779
2780    %% info/1:
2781    {ok, NoR} = xref:q(S, '# R'),
2782    {ok, NoA} = xref:q(S, '# A'),
2783    {ok, NoM} = xref:q(S, '# AM'),
2784    {ok, NoCalls} = xref:q(S, '# (XLin) E + # (LLin) E'),
2785    {ok, NoFunCalls} = xref:q(S, '# E'),
2786    {ok, NoXCalls} = xref:q(S, '# XC'),
2787    {ok, NoLCalls} = xref:q(S, '# LC'),
2788    {ok, NoLXCalls} = xref:q(S, '# (XC * LC)'),
2789    NoAllCalls = NoXCalls + NoLCalls,
2790    {ok, NoFun} = xref:q(S, '# (F - Extra)'),
2791    {ok, NoICalls} = xref:q(S, '# EE'),
2792
2793    Info = xref:info(S),
2794    NoR = info(Info, no_releases),
2795    NoA = info(Info, no_applications),
2796    NoM = info(Info, no_analyzed_modules),
2797    {NoResolved, NoUC} = info(Info, no_calls),
2798    NoCalls = NoResolved + NoUC,
2799    {NoLocal, NoExternal, NoUnres} = info(Info, no_function_calls),
2800    NoAllCalls = NoLocal + NoExternal + NoUnres,
2801    NoAllCalls = NoFunCalls + NoLXCalls,
2802    {NoLocalFuns, NoExportedFuns} = info(Info, no_functions),
2803    NoFun = NoLocalFuns + NoExportedFuns,
2804    NoICalls = info(Info, no_inter_function_calls),
2805
2806    %% per module
2807    info_module(M, S),
2808
2809    ok.
2810
2811info_module([M | Ms], S) ->
2812    {ok, NoCalls} = per_module("T = (E | ~p : Mod), # (XLin) T + # (LLin) T",
2813                               M, S),
2814    {ok, NoFunCalls} = per_module("# (E | ~p : Mod)", M, S),
2815    {ok, NoXCalls} = per_module("# (XC | ~p : Mod)", M, S),
2816    {ok, NoLCalls} = per_module("# (LC | ~p : Mod)", M, S),
2817    {ok, NoLXCalls} = per_module("# ((XC * LC) | ~p : Mod)", M, S),
2818    NoAllCalls = NoXCalls + NoLCalls,
2819    {ok, NoFun} = per_module("# (F * ~p : Mod - Extra)", M, S),
2820    {ok, NoICalls} = per_module("# (EE | ~p : Mod)", M, S),
2821
2822    [{_M,Info}] = xref:info(S, modules, M),
2823    {NoResolved, NoUC} = info(Info, no_calls),
2824    NoCalls = NoResolved + NoUC,
2825    {NoLocal, NoExternal, NoUnres} = info(Info, no_function_calls),
2826    NoAllCalls = NoLocal + NoExternal + NoUnres,
2827    NoAllCalls = NoFunCalls + NoLXCalls,
2828    {NoLocalFuns, NoExportedFuns} = info(Info, no_functions),
2829    NoFun = NoLocalFuns + NoExportedFuns,
2830    NoICalls = info(Info, no_inter_function_calls),
2831
2832    info_module(Ms, S);
2833info_module([], _S) ->
2834    ok.
2835
2836per_module(Q, M, S) ->
2837    xref:q(S, f(Q, [M])).
2838
2839info(Info, What) ->
2840    {value, {What, Value}} = lists:keysearch(What, 1, Info),
2841    Value.
2842
2843f(S, A) ->
2844    flatten(io_lib:format(S, A)).
2845
2846fatom(R) ->
2847    list_to_atom(fstring(R)).
2848
2849fstring(R) ->
2850    flatten(xref:format_error(R)).
2851
2852start(Server) ->
2853    case xref:start(Server) of
2854        {error, {already_started, _Pid}} ->
2855            xref:stop(Server),
2856            xref:start(Server);
2857        R -> R
2858    end.
2859
2860add_erts_code_path(KernelPath) ->
2861    VersionDirs =
2862    filelib:is_dir(
2863      filename:join(
2864        [code:lib_dir(),
2865         lists:flatten(
2866           ["kernel-",
2867            [X ||
2868             {kernel,_,X} <-
2869             application_controller:which_applications()]])])),
2870    case VersionDirs of
2871        true ->
2872            case code:lib_dir(erts) of
2873                String when is_list(String) ->
2874                    [KernelPath, fname(String,"ebin")];
2875                _Other1 ->
2876                    [KernelPath]
2877            end;
2878        false ->
2879            % Clearcase?
2880            PrelPath = filename:join([code:lib_dir(),"..","erts","preloaded"]),
2881            case filelib:is_dir(PrelPath) of
2882                true ->
2883                    [KernelPath, fname(PrelPath,"ebin")];
2884                false ->
2885                    [KernelPath]
2886            end
2887    end.
2888