1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2000-2018. 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(t,test_server).
29-define(datadir, "xref_SUITE_data").
30-define(privdir, "xref_SUITE_priv").
31-define(copydir, "xref_SUITE_priv/datacopy").
32-else.
33-include_lib("common_test/include/ct.hrl").
34-define(format(S, A), ok).
35-define(datadir, proplists:get_value(data_dir, Conf)).
36-define(privdir, proplists:get_value(priv_dir, Conf)).
37-define(copydir, proplists:get_value(copy_dir, Conf)).
38-endif.
39
40-export([all/0, suite/0, groups/0,
41         init_per_suite/1, end_per_suite/1]).
42
43-export([addrem/1, convert/1, intergraph/1, lines/1, loops/1,
44         no_data/1, modules/1]).
45
46-export([add/1, default/1, info/1, lib/1, read/1, read2/1, remove/1,
47         replace/1, update/1, deprecated/1, trycatch/1,
48         fun_mfa/1, fun_mfa_r14/1,
49         fun_mfa_vars/1, qlc/1]).
50
51-export([analyze/1, basic/1, md/1, q/1, variables/1, unused_locals/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_r14, fun_mfa_vars, qlc]},
84     {analyses, [],
85
86      [analyze, basic, md, q, variables, unused_locals]},
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    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        _ ->
746            true
747    end,
748    {error, _, {file_error, _, _}} =
749    xref_base:add_release(S, fname(["/a/b/c/d/e/f","__foo"])),
750    {ok, release2, S2} =
751    xref_base:add_release(S1, Dir, [{name,release2}]),
752    {error, _, {module_clash, {x, _, _}}} =
753    xref_base:add_module(S2, Xbeam),
754    {ok, S3} = xref_base:remove_release(S2, release2),
755    {ok, rel2, S4} = xref_base:add_release(S3, Dir),
756    {error, _, {release_clash, {rel2, _, _}}} =
757    xref_base:add_release(S4, Dir),
758    {ok, S5} = xref_base:remove_release(S4, rel2),
759    %% One unreadable file and one JAM file found (no verification here):
760    {ok, [], S6} = xref_base:add_directory(S5, fname(CopyDir,"dir"),
761                                           [{recurse,true}, {warnings,true}]),
762    case os:type() of
763        {unix, _} ->
764            {error, _, {file_error, _, _}} =
765            xref_base:add_directory(S6, UDir);
766        _ ->
767            true
768    end,
769    {ok, app1, S7} = xref_base:add_application(S6, A1_1),
770    {error, _, {application_clash, {app1, _, _}}} =
771    xref_base:add_application(S7, A1_1),
772    {ok, S8} = xref_base:remove_application(S7, app1),
773    ok = xref_base:delete(S8),
774    ok = file:delete(Xbeam),
775    ok = file:delete(Ybeam),
776    case os:type() of
777        {unix, _} ->
778            ok = file:del_dir(UDir),
779            ok = file:delete(UFile);
780        _ ->
781            true
782    end,
783    ok.
784
785%% Default values of options
786default(Conf) when is_list(Conf) ->
787    S = new(),
788    {error, _, {invalid_options,[not_an_option]}} =
789    xref_base:set_default(S, not_an_option, true),
790    {error, _, {invalid_options,[{builtins, not_a_value}]}} =
791    xref_base:set_default(S, builtins, not_a_value),
792    {error, _, {invalid_options,[not_an_option]}} =
793    xref_base:get_default(S, not_an_option),
794    {error, _, {invalid_options,[not_an_option]}} =
795    xref_base:set_default(S, [not_an_option]),
796
797    D = xref_base:get_default(S),
798    [{builtins,false},{recurse,false},{verbose,false},{warnings,true}] =
799    D,
800
801    ok = xref_base:delete(S),
802    ok.
803
804%% The info functions
805info(Conf) when is_list(Conf) ->
806    CopyDir = ?copydir,
807    Dir = fname(CopyDir,"rel2"),
808    LDir = fname(CopyDir,"lib_test"),
809    X = fname(Dir, "x.erl"),
810    Y = fname(Dir, "y.erl"),
811    A1_1 = fname([Dir,"lib","app1-1.1"]),
812    A2 = fname([Dir,"lib","app2-1.1"]),
813    EB1_1 = fname(A1_1, "ebin"),
814    EB2 = fname(A2, "ebin"),
815    Xbeam = fname(EB2, "x.beam"),
816    Ybeam = fname(EB1_1, "y.beam"),
817
818    {ok, x} = compile:file(X, [debug_info, {outdir,EB2}]),
819    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
820
821    {ok, _} = start(s),
822    {error, _, {no_such_info, release}} = xref:info(s, release),
823    {error, _, {no_such_info, release}} = xref:info(s, release, rel),
824    {error, _, {no_such_module, mod}} = xref:info(s, modules, mod),
825    {error, _, {no_such_application, app}} =
826    xref:info(s, applications, app),
827    {error, _, {no_such_release, rel}} = xref:info(s, releases, rel),
828    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
829    {ok, rel2} = xref:add_release(s, Dir),
830    9 = length(xref:info(s)),
831    [{x,_}, {y, _}] = xref:info(s, modules),
832    [{app1,_}, {app2, _}] = xref:info(s, applications),
833    [{rel2,_}] = xref:info(s, releases),
834    [] = xref:info(s, libraries),
835    [{x,_}] = xref:info(s, modules, x),
836    [{rel2,_}] = xref:info(s, releases, rel2),
837    {error, _, {no_such_library, foo}} = xref:info(s, libraries, [foo]),
838
839    {ok, lib1} =
840    compile:file(fname(LDir,lib1),[debug_info,{outdir,LDir}]),
841    {ok, lib2} =
842    compile:file(fname(LDir,lib2),[debug_info,{outdir,LDir}]),
843    ok = xref:set_library_path(s, [LDir], [{verbose,false}]),
844    [{lib1,_}, {lib2, _}] = xref:info(s, libraries),
845    [{lib1,_}, {lib2, _}] = xref:info(s, libraries, [lib1,lib2]),
846    ok = file:delete(fname(LDir, "lib1.beam")),
847    ok = file:delete(fname(LDir, "lib2.beam")),
848
849    check_state(s),
850
851    xref:stop(s),
852
853    ok = file:delete(Xbeam),
854    ok = file:delete(Ybeam),
855
856    ok.
857
858%% Library modules
859lib(Conf) when is_list(Conf) ->
860    CopyDir = ?copydir,
861    Dir = fname(CopyDir,"lib_test"),
862    UDir = fname([CopyDir,"dir","non_existent"]),
863
864    {ok, lib1} = compile:file(fname(Dir,lib1),[debug_info,{outdir,Dir}]),
865    {ok, lib2} = compile:file(fname(Dir,lib2),[debug_info,{outdir,Dir}]),
866    {ok, lib3} = compile:file(fname(Dir,lib3),[debug_info,{outdir,Dir}]),
867    {ok, t} = compile:file(fname(Dir,t),[debug_info,{outdir,Dir}]),
868
869    {ok, _} = start(s),
870    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
871    {ok, t} = xref:add_module(s, fname(Dir,"t.beam")),
872    {error, _, {invalid_options,[not_an_option]}} =
873    xref:set_library_path(s, ["foo"], [not_an_option]),
874    {error, _, {invalid_path,otp}} = xref:set_library_path(s,otp),
875    {error, _, {invalid_path,[""]}} = xref:set_library_path(s,[""]),
876    {error, _, {invalid_path,[[$a | $b]]}} =
877    xref:set_library_path(s,[[$a | $b]]),
878    {error, _, {invalid_path,[otp]}} = xref:set_library_path(s,[otp]),
879    {ok, []} = xref:get_library_path(s),
880    ok = xref:set_library_path(s, [Dir], [{verbose,false}]),
881    {ok, UnknownFunctions} = xref:q(s, "U"),
882    [{lib1,unknown,0}, {lib2,local,0},
883     {lib2,unknown,0}, {unknown,unknown,0}]
884    = UnknownFunctions,
885    {ok, [{lib2,f,0},{lib3,f,0}]} = xref:q(s, "DF"),
886    {ok, []} = xref:q(s, "DF_1"),
887    {ok, [{lib2,f,0}]} = xref:q(s, "DF_2"),
888    {ok, [{lib2,f,0}]} = xref:q(s, "DF_3"),
889
890    {ok, [unknown]} = xref:q(s, "UM"),
891    {ok, UnknownDefAt} = xref:q(s, "(Lin)U"),
892    [{{lib1,unknown,0},0},{{lib2,local,0},0}, {{lib2,unknown,0},0},
893     {{unknown,unknown,0},0}] = UnknownDefAt,
894    {ok, LibFuns} = xref:q(s, "X * LM"),
895    [{lib2,f,0},{lib3,f,0}] = LibFuns,
896    {ok, LibMods} = xref:q(s, "LM"),
897    [lib1,lib2,lib3] = LibMods,
898    {ok, [{{lib2,f,0},0},{{lib3,f,0},0}]} = xref:q(s, "(Lin) (LM * X)"),
899    {ok, [{{lib1,unknown,0},0}, {{lib2,f,0},0}, {{lib2,local,0},0},
900          {{lib2,unknown,0},0}, {{lib3,f,0},0}]} = xref:q(s,"(Lin)LM"),
901    {ok,[lib1,lib2,lib3,t,unknown]} = xref:q(s,"M"),
902    {ok,[{lib2,f,0},{lib3,f,0},{t,t,0}]} = xref:q(s,"X * M"),
903    check_state(s),
904
905    copy_file(fname(Dir, "lib1.erl"), fname(Dir,"lib1.beam")),
906    ok = xref:set_library_path(s, [Dir]),
907    {error, _, _} = xref:q(s, "U"),
908
909    %% OTP-3921. AM and LM not always disjoint.
910    {ok, lib1} = compile:file(fname(Dir,lib1),[debug_info,{outdir,Dir}]),
911    {ok, lib1} = xref:add_module(s, fname(Dir,"lib1.beam")),
912    check_state(s),
913
914    {error, _, {file_error, _, _}} = xref:set_library_path(s, [UDir]),
915
916    xref:stop(s),
917    ok = file:delete(fname(Dir, "lib1.beam")),
918    ok = file:delete(fname(Dir, "lib2.beam")),
919    ok = file:delete(fname(Dir, "lib3.beam")),
920    ok = file:delete(fname(Dir, "t.beam")),
921
922    {ok, cp} = compile:file(fname(Dir,cp),[debug_info,{outdir,Dir}]),
923    {ok, _} = start(s),
924    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
925    {ok, cp} = xref:add_module(s, fname(Dir,"cp.beam")),
926    {ok, [{lists, sort, 1}]} = xref:q(s, "U"),
927    ok = xref:set_library_path(s, code_path),
928    {ok, []} = xref:q(s, "U"),
929    check_state(s),
930    xref:stop(s),
931    ok = file:delete(fname(Dir, "cp.beam")),
932    ok.
933
934%% Data read from the Abstract Code
935read(Conf) when is_list(Conf) ->
936    CopyDir = ?copydir,
937    Dir = fname(CopyDir,"read"),
938    File = fname(Dir, "read"),
939    Beam = fname(Dir, "read.beam"),
940    {ok, read} = compile:file(File, [debug_info,{outdir,Dir}]),
941    do_read(File, abstract_v2),
942    copy_file(fname(Dir, "read.beam.v1"), Beam),
943    do_read(File, abstract_v1),
944    ok = file:delete(Beam),
945    ok.
946
947do_read(File, Version) ->
948    {ok, _} = start(s),
949    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
950    {ok, read} = xref:add_module(s, File),
951
952    {U, OK, OKB} = read_expected(Version),
953
954    %% {ok, UC} = xref:q(s, "(Lin) UC"),
955    %% RR = to_external(converse(family_to_relation(family(UC)))),
956    %% lists:foreach(fun(X) -> io:format("~w~n", [X]) end, RR),
957    Unres = to_external(relation_to_family(converse(from_term(U)))),
958    {ok, Unres} =	xref:q(s, "(Lin) UC"),
959
960    %% {ok, EE} = xref:q(s, "(Lin) (E - UC)"),
961    %% AA = to_external(converse(family_to_relation(family(EE)))),
962    %% lists:foreach(fun(X) -> io:format("~w~n", [X]) end, AA),
963    Calls = to_external(relation_to_family(converse(from_term(OK)))),
964    {ok, Calls} = xref:q(s, "(Lin) (E - UC) "),
965
966    ok = check_state(s),
967    {ok, UM} = xref:q(s, "UM"),
968    true = member('$M_EXPR', UM),
969
970    {ok, X} = xref:q(s, "X"),
971    true = member({read, module_info, 0}, X),
972    false = member({foo, module_info, 0}, X),
973    false = member({erlang, module_info, 0}, X),
974    {ok, Unknowns} = xref:q(s, "U"),
975    false = member({read, module_info, 0}, Unknowns),
976    true = member({foo, module_info, 0}, Unknowns),
977    true = member({erlang, module_info, 0}, Unknowns),
978    {ok, LC} = xref:q(s, "LC"),
979    true = member({{read,bi,0},{read,bi,0}}, LC),
980
981    ok = xref:set_library_path(s, add_erts_code_path(fname(code:lib_dir(kernel),ebin))),
982    io:format("~p~n",[(catch xref:get_library_path(s))]),
983    {ok, X2} = xref:q(s, "X"),
984    ok = check_state(s),
985    true = member({read, module_info, 0}, X2),
986    false = member({foo, module_info, 0}, X2),
987    true = member({erlang, module_info, 0}, X2),
988    {ok, Unknowns2} = xref:q(s, "U"),
989    false = member({read, module_info, 0}, Unknowns2),
990    true = member({foo, module_info, 0}, Unknowns2),
991    false = member({erlang, module_info, 0}, Unknowns2),
992
993    ok = xref:remove_module(s, read),
994    {ok, read} = xref:add_module(s, File, [{builtins,true}]),
995
996    UnresB = to_external(relation_to_family(converse(from_term(U)))),
997    {ok, UnresB} = xref:q(s, "(Lin) UC"),
998    CallsB = to_external(relation_to_family(converse(from_term(OKB)))),
999    {ok, CallsB} = xref:q(s, "(Lin) (E - UC) "),
1000    ok = check_state(s),
1001    {ok, XU} = xref:q(s, "XU"),
1002    Erl = set([{erlang,length,1},{erlang,integer,1},
1003               {erlang,binary_to_term,1}]),
1004    [{erlang,binary_to_term,1},{erlang,length,1}] =
1005    to_external(intersection(set(XU), Erl)),
1006    xref:stop(s).
1007
1008%% What is expected when xref_SUITE_data/read/read.erl is added:
1009read_expected(Version) ->
1010    %% Line positions in xref_SUITE_data/read/read.erl:
1011    POS1 = 28, POS2 = POS1+10, POS3 = POS2+6, POS4 = POS3+6, POS5 = POS4+10,
1012    POS6 = POS5+5, POS7 = POS6+6, POS8 = POS7+6, POS9 = POS8+8,
1013    POS10 = POS9+10, POS11 = POS10+7, POS12 = POS11+8, POS13 = POS12+10,
1014    POS14 = POS13+18, POS15 = POS14+23,
1015
1016    FF = {read,funfuns,0},
1017    U = [{POS1+5,{FF,{dist,'$F_EXPR',0}}},
1018         {POS1+8,{FF,{dist,'$F_EXPR',0}}},
1019         {POS2+8,{{read,funfuns,0},{expr,'$F_EXPR',1}}},
1020         {POS3+4,{FF,{expr,'$F_EXPR',2}}},
1021         {POS4+2,{FF,{modul,'$F_EXPR',1}}},
1022         {POS4+4,{FF,{spm,'$F_EXPR',1}}},
1023         {POS4+6,{FF,{spm,'$F_EXPR',1}}},
1024         {POS4+8,{FF,{spm,'$F_EXPR',1}}},
1025         {POS5+1,{FF,{'$M_EXPR','$F_EXPR',0}}},
1026         {POS5+2,{FF,{'$M_EXPR','$F_EXPR',0}}},
1027         {POS5+3,{FF,{'$M_EXPR','$F_EXPR',0}}},
1028         {POS6+1,{FF,{'$M_EXPR','$F_EXPR',0}}},
1029         {POS6+2,{FF,{'$M_EXPR','$F_EXPR',0}}},
1030         {POS6+4,{FF,{n,'$F_EXPR',-1}}},
1031         {POS7+1,{FF,{'$M_EXPR',f,1}}},
1032         {POS7+2,{FF,{'$M_EXPR',f,1}}},
1033         {POS8+2,{FF,{hej,'$F_EXPR',1}}},
1034         {POS8+3,{FF,{t,'$F_EXPR',1}}},
1035         {POS8+5,{FF,{a,'$F_EXPR',1}}},
1036         {POS8+7,{FF,{m,'$F_EXPR',1}}},
1037         {POS9+1,{FF,{'$M_EXPR',f,1}}},
1038         {POS9+3,{FF,{a,'$F_EXPR',1}}},
1039         {POS10+1,{FF,{'$M_EXPR',foo,1}}},
1040         {POS10+2,{FF,{'$M_EXPR','$F_EXPR',1}}},
1041         {POS10+3,{FF,{'$M_EXPR','$F_EXPR',2}}},
1042         {POS10+4,{FF,{'$M_EXPR','$F_EXPR',1}}},
1043         {POS10+5,{FF,{'$M_EXPR',san,1}}},
1044         {POS10+6,{FF,{'$M_EXPR','$F_EXPR',1}}},
1045         {POS11+1,{FF,{'$M_EXPR','$F_EXPR',1}}},
1046         {POS11+2,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1047         {POS11+3,{FF,{m,f,-1}}},
1048         {POS11+4,{FF,{m,f,-1}}},
1049         {POS11+5,{FF,{'$M_EXPR','$F_EXPR',1}}},
1050         {POS11+6,{FF,{'$M_EXPR','$F_EXPR',1}}},
1051         {POS12+1,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1052         {POS12+4,{FF,{'$M_EXPR','$F_EXPR',2}}},
1053         {POS12+7,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1054         {POS12+8,{FF,{m4,f4,-1}}},
1055         {POS13+2,{FF,{debug,'$F_EXPR',0}}},
1056         {POS13+3,{FF,{'$M_EXPR','$F_EXPR',-1}}},
1057         {POS14+8,{{read,bi,0},{'$M_EXPR','$F_EXPR',1}}}],
1058
1059    O1 = [{20,{{read,lc,0},{ets,new,0}}},
1060          {21,{{read,lc,0},{ets,tab2list,1}}},
1061          {POS1+1,{FF,{erlang,spawn,1}}},
1062          {POS1+1,{FF,{mod17,fun17,0}}},
1063          {POS1+2,{FF,{erlang,spawn,1}}},
1064          {POS1+2,{FF,{read,local,0}}},
1065          {POS1+3,{FF,{erlang,spawn,1}}},
1066          {POS1+4,{FF,{dist,func,0}}},
1067          {POS1+4,{FF,{erlang,spawn,1}}},
1068          {POS1+5,{FF,{erlang,spawn,1}}},
1069          {POS1+6,{FF,{erlang,spawn_link,1}}},
1070          {POS1+6,{FF,{mod17,fun17,0}}},
1071          {POS1+7,{FF,{dist,func,0}}},
1072          {POS1+7,{FF,{erlang,spawn_link,1}}},
1073          {POS1+8,{FF,{erlang,spawn_link,1}}},
1074          {POS2+1,{FF,{d,f,0}}},
1075          {POS2+1,{FF,{dist,func,2}}},
1076          {POS2+1,{FF,{erlang,spawn,2}}},
1077          {POS2+2,{FF,{dist,func,2}}},
1078          {POS2+2,{FF,{erlang,spawn,2}}},
1079          {POS2+2,{FF,{mod42,func,0}}},
1080          {POS2+3,{FF,{d,f,0}}},
1081          {POS2+3,{FF,{dist,func,2}}},
1082          {POS2+3,{FF,{erlang,spawn_link,2}}},
1083          {POS2+4,{FF,{dist,func,2}}},
1084          {POS2+4,{FF,{erlang,spawn_link,2}}},
1085          {POS2+4,{FF,{mod42,func,0}}},
1086          {POS3+1,{FF,{dist,func,2}}},
1087          {POS3+3,{FF,{dist,func,2}}},
1088          {POS4+1,{FF,{erlang,spawn,4}}},
1089          {POS4+1,{FF,{modul,function,0}}},
1090          {POS4+2,{FF,{erlang,spawn,4}}},
1091          {POS4+3,{FF,{dist,func,2}}},
1092          {POS4+3,{FF,{erlang,spawn,4}}},
1093          {POS4+3,{FF,{spm,spf,2}}},
1094          {POS4+4,{FF,{dist,func,2}}},
1095          {POS4+4,{FF,{erlang,spawn,4}}},
1096          {POS4+5,{FF,{dist,func,2}}},
1097          {POS4+5,{FF,{erlang,spawn_link,4}}},
1098          {POS4+5,{FF,{spm,spf,2}}},
1099          {POS4+6,{FF,{dist,func,2}}},
1100          {POS4+6,{FF,{erlang,spawn_link,4}}},
1101          {POS4+7,{FF,{erlang,spawn_opt,4}}},
1102          {POS4+7,{FF,{read,bi,0}}},
1103          {POS4+7,{FF,{spm,spf,2}}},
1104          {POS4+8,{FF,{erlang,spawn_opt,4}}},
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          {POS7+2,{FF,{erlang,spawn_opt,4}}},
1113          {POS8+1,{FF,{hej,san,1}}},
1114          {POS8+4,{FF,{a,b,1}}},
1115          {POS8+6,{FF,{m,f,1}}},
1116          {POS9+1,{FF,{read,bi,0}}},
1117          {POS9+2,{FF,{a,b,1}}},
1118          {POS9+4,{FF,{erlang,not_a_function,1}}},
1119          {POS9+5,{FF,{mod,func,2}}},
1120          {POS9+6,{FF,{erlang,apply,1}}},
1121          {POS9+7,{FF,{math,add3,1}}},
1122          {POS9+8,{FF,{q,f,1}}},
1123          {POS10+5,{FF,{mod1,fun1,1}}},
1124          {POS12+5,{FF,{m3,f3,2}}},
1125          {POS13+1,{FF,{dm,df,1}}},
1126          {POS13+6,{{read,bi,0},{foo,module_info,0}}},
1127          {POS13+7,{{read,bi,0},{read,module_info,0}}},
1128          {POS13+9,{{read,bi,0},{t,foo,1}}},
1129          {POS14+11,{{read,bi,0},{erlang,module_info,0}}},
1130          {POS14+17,{{read,bi,0},{read,bi,0}}}],
1131
1132    OK = case Version of
1133             abstract_v1 ->
1134                 [{0,{FF,{read,'$F_EXPR',178}}},
1135                  {0,{FF,{modul,'$F_EXPR',179}}}]
1136                 ++ O1;
1137             _ ->
1138                 [{16,{FF,{read,'$F_EXPR',178}}},
1139                  {17,{FF,{modul,'$F_EXPR',179}}}]
1140                 ++
1141                 O1
1142         end,
1143
1144    %% When builtins =:= true:
1145    OKB1 = [{POS13+1,{FF,{erts_debug,apply,4}}},
1146            {POS13+2,{FF,{erts_debug,apply,4}}},
1147            {POS13+3,{FF,{erts_debug,apply,4}}},
1148            {POS1+3, {FF,{erlang,binary_to_term,1}}},
1149            {POS3+1, {FF,{erlang,spawn,3}}},
1150            {POS3+2, {FF,{erlang,spawn,3}}},
1151            {POS3+3,  {FF,{erlang,spawn_link,3}}},
1152            {POS3+4, {FF,{erlang,spawn_link,3}}},
1153            {POS6+4, {FF,{erlang,spawn,3}}},
1154            {POS8+4,{FF,{erlang,apply,2}}},
1155            {POS8+5,{FF,{erlang,apply,2}}},
1156            {POS8+6,{FF,{erlang,apply,3}}},
1157            {POS8+7,{FF,{erlang,apply,3}}},
1158            {POS9+1,{FF,{erlang,apply,3}}},
1159            {POS9+2,{FF,{erlang,apply,2}}},
1160            {POS9+3,{FF,{erlang,apply,2}}},
1161            {POS9+4,{FF,{erlang,apply,2}}},
1162            {POS9+5,{FF,{erlang,apply,3}}},
1163            {POS9+7,{FF,{erlang,apply,2}}},
1164            {POS10+4,{FF,{erlang,apply,2}}},
1165            {POS11+1,{FF,{erlang,apply,3}}},
1166            {POS11+2,{FF,{erlang,apply,3}}},
1167            {POS11+3,{FF,{erlang,apply,3}}},
1168            {POS11+4,{FF,{erlang,apply,3}}},
1169            {POS11+6,{FF,{erlang,apply,2}}},
1170            {POS12+1,{FF,{erlang,apply,2}}},
1171            {POS12+4,{FF,{erlang,apply,2}}},
1172            {POS12+5,{FF,{erlang,apply,3}}},
1173            {POS12+7,{FF,{erlang,apply,2}}},
1174            {POS12+8,{FF,{erlang,apply,3}}},
1175            {POS13+5, {{read,bi,0},{erlang,length,1}}},
1176            {POS14+3, {{read,bi,0},{erlang,length,1}}}],
1177
1178    %% Operators (OTP-8647):
1179    OKB = case Version of
1180              abstract_v1 ->
1181                  [{POS8+3, {FF,{erlang,apply,3}}},
1182                   {POS10+1, {FF,{erlang,apply,3}}},
1183                   {POS10+6, {FF,{erlang,apply,3}}}];
1184              _ ->
1185                  [{POS13+16, {{read,bi,0},{erlang,'!',2}}},
1186                   {POS13+16, {{read,bi,0},{erlang,'-',1}}},
1187                   {POS13+16, {{read,bi,0},{erlang,self,0}}},
1188                   {POS15+1,  {{read,bi,0},{erlang,'>',2}}},
1189                   {POS15+2,  {{read,bi,0},{erlang,'-',2}}},
1190                   {POS15+2,  {{read,bi,0},{erlang,'*',2}}},
1191                   {POS15+8,  {{read,bi,0},{erlang,'/',2}}}]
1192          end
1193    ++ [{POS14+19, {{read,bi,0},{erlang,'+',2}}},
1194        {POS14+21, {{read,bi,0},{erlang,'+',2}}},
1195        {POS13+16, {{read,bi,0},{erlang,'==',2}}},
1196        {POS14+15, {{read,bi,0},{erlang,'==',2}}},
1197        {POS13+5,  {{read,bi,0},{erlang,'>',2}}},
1198        {POS14+3,  {{read,bi,0},{erlang,'>',2}}}]
1199    ++ OKB1 ++ OK,
1200
1201    {U, OK, OKB}.
1202
1203%% Data read from the Abstract Code (cont)
1204read2(Conf) when is_list(Conf) ->
1205    %% Handles the spawn_opt versions added in R9 (OTP-4180).
1206    %% Expected augmentations: try/catch, cond.
1207    CopyDir = ?copydir,
1208    Dir = fname(CopyDir,"read"),
1209    File = fname(Dir, "read2.erl"),
1210    MFile = fname(Dir, "read2"),
1211    Beam = fname(Dir, "read2.beam"),
1212    Test = <<"-module(read2).
1213              -compile(export_all).
1214
1215              f() ->
1216                  spawn_opt({read2,f}, % POS2
1217                            [f()]),
1218                  spawn_opt(fun() -> foo end, [link]),
1219                  spawn_opt(f(),
1220                            {read2,f}, [{min_heap_size,1000}]),
1221                  spawn_opt(f(),
1222                            fun() -> f() end, [flopp]),
1223                  spawn_opt(f(),
1224                            read2, f, [], []);
1225              f() ->
1226                  %% Duplicated unresolved calls are ignored:
1227                  (f())(foo,bar),(f())(foo,bar). % POS1
1228
1229              %% Warning forms must be ignored.
1230              -warning(must_not_crash).
1231             ">>,
1232    ok = file:write_file(File, Test),
1233    {ok, read2} = compile:file(File, [debug_info,{outdir,Dir}]),
1234
1235    {ok, _} = xref:start(s),
1236    {ok, read2} = xref:add_module(s, MFile),
1237    {U0, OK0} = read2_expected(),
1238
1239    U = to_external(relation_to_family(converse(from_term(U0)))),
1240    OK = to_external(relation_to_family(converse(from_term(OK0)))),
1241    {ok, U2} = xref:q(s, "(Lin) UC"),
1242    {ok, OK2} = xref:q(s, "(Lin) (E - UC)"),
1243    true = U =:= U2,
1244    true = OK =:= OK2,
1245    ok = check_state(s),
1246    xref:stop(s),
1247
1248    ok = file:delete(File),
1249    ok = file:delete(Beam),
1250    ok.
1251
1252
1253read2_expected() ->
1254    POS1 = 16,
1255    POS2 = 5,
1256    FF = {read2,f,0},
1257    U =  [{POS1,{FF,{'$M_EXPR','$F_EXPR',2}}}],
1258    OK = [{POS2,{FF,{erlang,spawn_opt,2}}},
1259          {POS2,{FF,FF}},
1260          {POS2+1,{FF,FF}},
1261          {POS2+2,{FF,{erlang,spawn_opt,2}}},
1262          {POS2+3,{FF,{erlang,spawn_opt,3}}},
1263          {POS2+3,{FF,FF}},
1264          {POS2+3,{FF,FF}},
1265          {POS2+5,{FF,{erlang,spawn_opt,3}}},
1266          {POS2+5,{FF,FF}},
1267          {POS2+6,{FF,FF}},
1268          {POS2+7,{FF,{erlang,spawn_opt,5}}},
1269          {POS2+7,{FF,FF}},
1270          {POS2+7,{FF,FF}},
1271          {POS1,{FF,FF}}],
1272    {U, OK}.
1273
1274%% Remove modules, applications, releases
1275remove(Conf) when is_list(Conf) ->
1276    S = new(),
1277    {error, _, {no_such_module, mod}} =
1278    xref_base:remove_module(S, mod),
1279    {error, _, {no_such_application, app}} =
1280    xref_base:remove_application(S, app),
1281    {error, _, {no_such_release, rel}} =
1282    xref_base:remove_release(S, rel),
1283    ok = xref_base:delete(S),
1284    ok.
1285
1286%% Replace modules, applications, releases
1287replace(Conf) when is_list(Conf) ->
1288    CopyDir = ?copydir,
1289    Dir = fname(CopyDir,"rel2"),
1290    X = fname(Dir, "x.erl"),
1291    Y = fname(Dir, "y.erl"),
1292    A1_0 = fname(Dir, fname("lib","app1-1.0")),
1293    A1_1 = fname(Dir, fname("lib","app1-1.1")),
1294    A2 = fname(Dir, fname("lib","app2-1.1")),
1295    EB1_0 = fname(A1_0, "ebin"),
1296    EB1_1 = fname(A1_1, "ebin"),
1297    Xbeam = fname(EB1_1, "x.beam"),
1298    Ybeam = fname(EB1_1, "y.beam"),
1299
1300    {ok, x} = compile:file(X, [debug_info, {outdir,EB1_0}]),
1301    {ok, x} = compile:file(X, [debug_info, {outdir,EB1_1}]),
1302    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
1303
1304    {ok, _} = start(s),
1305    {ok, false} = xref:set_default(s, verbose, false),
1306    {ok, true} = xref:set_default(s, warnings, false),
1307    {ok, rel2} = xref:add_release(s, Dir, []),
1308    {error, _, _} = xref:replace_application(s, app1, "no_data"),
1309    {error, _, {no_such_application, app12}} =
1310    xref:replace_application(s, app12, A1_0, []),
1311    {error, _, {invalid_filename,{foo,bar}}} =
1312    xref:replace_application(s, app1, {foo,bar}, []),
1313    {error, _, {invalid_options,[not_an_option]}} =
1314    xref:replace_application(s, foo, bar, [not_an_option]),
1315    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
1316    xref:replace_application(s, foo, bar, [{builtins,not_a_value}]),
1317    {ok, app1} =
1318    xref:replace_application(s, app1, A1_0),
1319    [{_, AppInfo}] = xref:info(s, applications, app1),
1320    {value, {release, [rel2]}} = keysearch(release, 1, AppInfo),
1321
1322    {error, _, {no_such_module, xx}} =
1323    xref:replace_module(s, xx, Xbeam, []),
1324    {error, _, {invalid_options,[{builtins,true},not_an_option]}} =
1325    xref:replace_module(s, foo, bar,[{builtins,true},not_an_option]),
1326    {error, _, {invalid_options,[{builtins,not_a_value}]}} =
1327    xref:replace_module(s, foo, bar, [{builtins,not_a_value}]),
1328    {error, _, {invalid_filename,{foo,bar}}} =
1329    xref:replace_module(s, x, {foo,bar}),
1330    {ok, x} = xref:replace_module(s, x, Xbeam),
1331    [{x, ModInfo}] = xref:info(s, modules, x),
1332    {value, {application, [app1]}} =
1333    keysearch(application, 1, ModInfo),
1334
1335    {ok, x} = compile:file(X, [no_debug_info, {outdir,EB1_1}]),
1336    {error, _, {no_debug_info, _}} = xref:replace_module(s, x, Xbeam),
1337    {error, _, {module_mismatch, x,y}} =
1338    xref:replace_module(s, x, Ybeam),
1339    case os:type() of
1340        {unix, _} ->
1341            hide_file(Ybeam),
1342            {error, _, {file_error, _, _}} =
1343            xref:replace_module(s, x, Ybeam);
1344        _ ->
1345            true
1346    end,
1347    ok = xref:remove_module(s, x),
1348    {error, _, {no_debug_info, _}} = xref:add_module(s, Xbeam),
1349
1350    %% "app2" is ignored, the old application name is kept
1351    {ok, app1} = xref:replace_application(s, app1, A2),
1352
1353    xref:stop(s),
1354    ok = file:delete(fname(EB1_0, "x.beam")),
1355    ok = file:delete(Xbeam),
1356    ok = file:delete(Ybeam),
1357    ok.
1358
1359%% The update() function
1360update(Conf) when is_list(Conf) ->
1361    CopyDir = ?copydir,
1362    Dir = fname(CopyDir,"update"),
1363    Source = fname(Dir, "x.erl"),
1364    Beam = fname(Dir, "x.beam"),
1365    copy_file(fname(Dir, "x.erl.1"), Source),
1366    {ok, x} = compile:file(Source, [debug_info, {outdir,Dir}]),
1367
1368    {ok, _} = start(s),
1369    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
1370    {ok, [x]} = xref:add_directory(s, Dir, [{builtins,true}]),
1371    {error, _, {invalid_options,[not_an_option]}} =
1372    xref:update(s, [not_an_option]),
1373    {ok, []} = xref:update(s),
1374    {ok, [{erlang,atom_to_list,1}]} = xref:q(s, "XU"),
1375
1376    [{x, ModInfo}] = xref:info(s, modules, x),
1377    case keysearch(directory, 1, ModInfo) of
1378        {value, {directory, Dir}} -> ok
1379    end,
1380
1381    timer:sleep(2000), % make sure modification time has changed
1382    copy_file(fname(Dir, "x.erl.2"), Source),
1383    {ok, x} = compile:file(Source, [debug_info, {outdir,Dir}]),
1384    {ok, [x]} = xref:update(s, []),
1385    {ok, [{erlang,list_to_atom,1}]} = xref:q(s, "XU"),
1386
1387    timer:sleep(2000),
1388    {ok, x} = compile:file(Source, [no_debug_info,{outdir,Dir}]),
1389    {error, _, {no_debug_info, _}} = xref:update(s),
1390
1391    xref:stop(s),
1392    ok = file:delete(Beam),
1393    ok = file:delete(Source),
1394    ok.
1395
1396%% OTP-4695: Deprecated functions.
1397deprecated(Conf) when is_list(Conf) ->
1398    Dir = ?copydir,
1399    File = fname(Dir, "depr.erl"),
1400    MFile_r9c = fname(Dir, "depr_r9c"),
1401    MFile = fname(Dir, "depr"),
1402    Beam = fname(Dir, "depr.beam"),
1403    %% This file has been compiled to ?datadir/depr_r9c.beam
1404    %% using the R9C compiler. From R10B and onwards the linter
1405    %% checks the 'deprecated' attribute as well.
1406    %     Test = <<"-module(depr).
1407
1408    %               -export([t/0,f/1,bar/2,f/2,g/3]).
1409
1410    %               -deprecated([{f,1},                             % DF
1411    %                            {bar,2,eventually}]).              % DF_3
1412    %               -deprecated([{f,1,next_major_release}]).        % DF_2 (again)
1413    %               -deprecated([{frutt,0,next_version}]).          % message...
1414    %               -deprecated([{f,2,next_major_release},          % DF_2
1415    %                            {g,3,next_version},                % DF_1
1416    %                            {ignored,10,100}]).                % message...
1417    %               -deprecated([{does_not_exist,1}]).              % message...
1418
1419    %               -deprecated(foo).                               % message...
1420
1421    %               t() ->
1422    %                   frutt(1),
1423    %                   g(1,2, 3),
1424    %                   ?MODULE:f(10).
1425
1426    %               f(A) ->
1427    %                   ?MODULE:f(A,A).
1428
1429    %               f(X, Y) ->
1430    %                   ?MODULE:g(X, Y, X).
1431
1432    %               g(F, G, H) ->
1433    %                   ?MODULE:bar(F, {G,H}).
1434
1435    %               bar(_, _) ->
1436    %                   true.
1437
1438    %               frutt(_) ->
1439    %                   frutt().
1440
1441    %               frutt() ->
1442    %                   true.
1443    %              ">>,
1444
1445    %    ok = file:write_file(File, Test),
1446    %    {ok, depr_r9c} = compile:file(File, [debug_info,{outdir,Dir}]),
1447
1448    {ok, _} = xref:start(s),
1449    {ok, depr_r9c} = xref:add_module(s, MFile_r9c),
1450    M9 = depr_r9c,
1451    DF_1 = usort([{{M9,f,2},{M9,g,3}}]),
1452    DF_2 = usort(DF_1++[{{M9,f,1},{M9,f,2}},{{M9,t,0},{M9,f,1}}]),
1453    DF_3 = usort(DF_2++[{{M9,g,3},{M9,bar,2}}]),
1454    DF = usort(DF_3++[{{M9,t,0},{M9,f,1}}]),
1455
1456    {ok,DF} = xref:analyze(s, deprecated_function_calls),
1457    {ok,DF_1} =
1458    xref:analyze(s, {deprecated_function_calls,next_version}),
1459    {ok,DF_2} =
1460    xref:analyze(s, {deprecated_function_calls,next_major_release}),
1461    {ok,DF_3} =
1462    xref:analyze(s, {deprecated_function_calls,eventually}),
1463
1464    D = to_external(range(from_term(DF))),
1465    D_1 = to_external(range(from_term(DF_1))),
1466    D_2 = to_external(range(from_term(DF_2))),
1467    D_3 = to_external(range(from_term(DF_3))),
1468
1469    {ok,D} = xref:analyze(s, deprecated_functions),
1470    {ok,D_1} =
1471    xref:analyze(s, {deprecated_functions,next_version}),
1472    {ok,D_2} =
1473    xref:analyze(s, {deprecated_functions,next_major_release}),
1474    {ok,D_3} =
1475    xref:analyze(s, {deprecated_functions,eventually}),
1476
1477    ok = check_state(s),
1478    xref:stop(s),
1479
1480    Test2= <<"-module(depr).
1481
1482              -export([t/0,f/1,bar/2,f/2,g/3]).
1483
1484              -deprecated([{'_','_',eventually}]).            % DF_3
1485              -deprecated([{f,'_',next_major_release}]).      % DF_2
1486              -deprecated([{g,'_',next_version}]).            % DF_1
1487              -deprecated([{bar,2}]).                         % DF
1488
1489              t() ->
1490                  g(1,2, 3),
1491                  ?MODULE:f(10).
1492
1493              f(A) ->
1494                  ?MODULE:f(A,A).
1495
1496              f(X, Y) ->
1497                  ?MODULE:g(X, Y, X).
1498
1499              g(F, G, H) ->
1500                  ?MODULE:bar(F, {G,H}).
1501
1502              bar(_, _) ->
1503                  ?MODULE:t().
1504             ">>,
1505
1506    ok = file:write_file(File, Test2),
1507    {ok, depr} = compile:file(File, [debug_info,{outdir,Dir}]),
1508
1509    {ok, _} = xref:start(s),
1510    {ok, depr} = xref:add_module(s, MFile),
1511
1512    M = depr,
1513    DFa_1 = usort([{{M,f,2},{M,g,3}}]),
1514    DFa_2 = usort(DFa_1++[{{M,f,1},{M,f,2}},{{M,t,0},{M,f,1}}]),
1515    DFa_3 = usort(DFa_2++[{{M,bar,2},{M,t,0}},{{M,g,3},{M,bar,2}}]),
1516    DFa = DFa_3,
1517
1518    {ok,DFa} = xref:analyze(s, deprecated_function_calls),
1519    {ok,DFa_1} =
1520    xref:analyze(s, {deprecated_function_calls,next_version}),
1521    {ok,DFa_2} =
1522    xref:analyze(s, {deprecated_function_calls,next_major_release}),
1523    {ok,DFa_3} =
1524    xref:analyze(s, {deprecated_function_calls,eventually}),
1525
1526    ok = check_state(s),
1527    xref:stop(s),
1528
1529    %% All of the module is deprecated.
1530    Test3= <<"-module(depr).
1531
1532              -export([t/0,f/1,bar/2,f/2,g/3]).
1533
1534              -deprecated([{f,'_',next_major_release}]).      % DF_2
1535              -deprecated([{g,'_',next_version}]).            % DF_1
1536              -deprecated(module).                            % DF
1537
1538              t() ->
1539                  g(1,2, 3),
1540                  ?MODULE:f(10).
1541
1542              f(A) ->
1543                  ?MODULE:f(A,A).
1544
1545              f(X, Y) ->
1546                  ?MODULE:g(X, Y, X).
1547
1548              g(F, G, H) ->
1549                  ?MODULE:bar(F, {G,H}).
1550
1551              bar(_, _) ->
1552                  ?MODULE:t().
1553             ">>,
1554
1555    ok = file:write_file(File, Test3),
1556    {ok, depr} = compile:file(File, [debug_info,{outdir,Dir}]),
1557
1558    {ok, _} = xref:start(s),
1559    {ok, depr} = xref:add_module(s, MFile),
1560
1561    DFb_1 = usort([{{M,f,2},{M,g,3}}]),
1562    DFb_2 = usort(DFb_1++[{{M,f,1},{M,f,2}},{{M,t,0},{M,f,1}}]),
1563    DFb_3 = DFb_2,
1564    DFb = usort(DFb_2++[{{M,bar,2},{M,t,0}},{{M,g,3},{M,bar,2}}]),
1565
1566    {ok,DFb} = xref:analyze(s, deprecated_function_calls),
1567    {ok,DFb_1} =
1568    xref:analyze(s, {deprecated_function_calls,next_version}),
1569    {ok,DFb_2} =
1570    xref:analyze(s, {deprecated_function_calls,next_major_release}),
1571    {ok,DFb_3} =
1572    xref:analyze(s, {deprecated_function_calls,eventually}),
1573
1574    ok = check_state(s),
1575    xref:stop(s),
1576
1577    ok = file:delete(File),
1578    ok = file:delete(Beam),
1579    ok.
1580
1581
1582%% OTP-5152: try/catch, final (?) version.
1583trycatch(Conf) when is_list(Conf) ->
1584    Dir = ?copydir,
1585    File = fname(Dir, "trycatch.erl"),
1586    MFile = fname(Dir, "trycatch"),
1587    Beam = fname(Dir, "trycatch.beam"),
1588    Test = <<"-module(trycatch).
1589
1590              -export([trycatch/0]).
1591
1592              trycatch() ->
1593                  try
1594                     foo:bar(),
1595                     bar:foo() of
1596                        1 -> foo:foo();
1597                        2 -> bar:bar()
1598                  catch
1599                     error:a -> err:e1();
1600                     error:b -> err:e2()
1601                  after
1602                     fini:shed()
1603                  end.
1604             ">>,
1605
1606    ok = file:write_file(File, Test),
1607    {ok, trycatch} = compile:file(File, [debug_info,{outdir,Dir}]),
1608
1609    {ok, _} = xref:start(s),
1610    {ok, trycatch} = xref:add_module(s, MFile),
1611    A = trycatch,
1612    {ok,[{{{A,A,0},{bar,bar,0}},[10]},
1613         {{{A,A,0},{bar,foo,0}},[8]},
1614         {{{A,A,0},{err,e1,0}},[12]},
1615         {{{A,A,0},{err,e2,0}},[13]},
1616         {{{A,A,0},{fini,shed,0}},[15]},
1617         {{{A,A,0},{foo,bar,0}},[7]},
1618         {{{A,A,0},{foo,foo,0}},[9]}]} =
1619    xref:q(s, "(Lin) (E | trycatch:trycatch/0)"),
1620
1621    ok = check_state(s),
1622    xref:stop(s),
1623
1624    ok = file:delete(File),
1625    ok = file:delete(Beam),
1626    ok.
1627
1628
1629%% OTP-5653: fun M:F/A.
1630fun_mfa(Conf) when is_list(Conf) ->
1631    Dir = ?copydir,
1632    File = fname(Dir, "fun_mfa.erl"),
1633    MFile = fname(Dir, "fun_mfa"),
1634    Beam = fname(Dir, "fun_mfa.beam"),
1635    Test = <<"-module(fun_mfa).
1636
1637              -export([t/0, t1/0, t2/0, t3/0]).
1638
1639              t() ->
1640                  F = fun ?MODULE:t/0,
1641                  (F)().
1642
1643              t1() ->
1644                  F = fun t/0,
1645                  (F)().
1646
1647              t2() ->
1648                  fun ?MODULE:t/0().
1649
1650              t3() ->
1651                  fun t3/0().
1652             ">>,
1653
1654    ok = file:write_file(File, Test),
1655    A = fun_mfa,
1656    {ok, A} = compile:file(File, [debug_info,{outdir,Dir}]),
1657    {ok, _} = xref:start(s),
1658    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1659    {ok, [{{{A,t,0},{'$M_EXPR','$F_EXPR',0}},[7]},
1660          {{{A,t,0},{A,t,0}},[6]},
1661          {{{A,t1,0},{'$M_EXPR','$F_EXPR',0}},[11]},
1662          {{{A,t1,0},{A,t,0}},[10]},
1663          {{{A,t2,0},{A,t,0}},[14]},
1664          {{{A,t3,0},{fun_mfa,t3,0}},[17]}]} =
1665    xref:q(s, "(Lin) E"),
1666
1667    ok = check_state(s),
1668    xref:stop(s),
1669
1670    ok = file:delete(File),
1671    ok = file:delete(Beam),
1672    ok.
1673
1674%% Same as the previous test case, except that we use a BEAM file
1675%% that was compiled by an R14 compiler to test backward compatibility.
1676fun_mfa_r14(Conf) when is_list(Conf) ->
1677    Dir = proplists:get_value(data_dir, Conf),
1678    MFile = fname(Dir, "fun_mfa_r14"),
1679
1680    A = fun_mfa_r14,
1681    {ok, _} = xref:start(s),
1682    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1683    {ok, [{{{A,t,0},{'$M_EXPR','$F_EXPR',0}},[7]},
1684          {{{A,t,0},{A,t,0}},[6]},
1685          {{{A,t1,0},{'$M_EXPR','$F_EXPR',0}},[11]},
1686          {{{A,t1,0},{A,t,0}},[10]},
1687          {{{A,t2,0},{A,t,0}},[14]},
1688          {{{A,t3,0},{A,t3,0}},[17]}]} =
1689    xref:q(s, "(Lin) E"),
1690
1691    ok = check_state(s),
1692    xref:stop(s),
1693
1694    ok.
1695
1696%% fun M:F/A with varibles.
1697fun_mfa_vars(Conf) when is_list(Conf) ->
1698    Dir = ?copydir,
1699    File = fname(Dir, "fun_mfa_vars.erl"),
1700    MFile = fname(Dir, "fun_mfa_vars"),
1701    Beam = fname(Dir, "fun_mfa_vars.beam"),
1702    Test = <<"-module(fun_mfa_vars).
1703
1704              -export([t/1, t1/1, t2/3]).
1705
1706              t(Mod) ->
1707                  F = fun Mod:bar/2,
1708                  (F)(a, b).
1709
1710              t1(Name) ->
1711                  F = fun ?MODULE:Name/1,
1712                  (F)(a).
1713
1714              t2(Mod, Name, Arity) ->
1715                  F = fun Mod:Name/Arity,
1716                  (F)(a).
1717
1718              t3(Arity) ->
1719                  F = fun ?MODULE:t/Arity,
1720                  (F)(1, 2, 3).
1721
1722              t4(Mod, Name) ->
1723                  F = fun Mod:Name/3,
1724                  (F)(a, b, c).
1725
1726              t5(Mod, Arity) ->
1727                  F = fun Mod:t/Arity,
1728                  (F)().
1729             ">>,
1730
1731    ok = file:write_file(File, Test),
1732    A = fun_mfa_vars,
1733    {ok, A} = compile:file(File, [report,debug_info,{outdir,Dir}]),
1734    {ok, _} = xref:start(s),
1735    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1736    {ok, [{{{A,t,1},{'$M_EXPR','$F_EXPR',2}},[7]},
1737          {{{A,t,1},{'$M_EXPR',bar,2}},[6]},
1738          {{{A,t1,1},{'$M_EXPR','$F_EXPR',1}},[11]},
1739          {{{A,t1,1},{A,'$F_EXPR',1}},[10]},
1740          {{{A,t2,3},{'$M_EXPR','$F_EXPR',-1}},[14]},
1741          {{{A,t2,3},{'$M_EXPR','$F_EXPR',1}},[15]},
1742          {{{A,t3,1},{'$M_EXPR','$F_EXPR',3}},[19]},
1743          {{{A,t3,1},{fun_mfa_vars,t,-1}},[18]},
1744          {{{A,t4,2},{'$M_EXPR','$F_EXPR',3}},[22,23]},
1745          {{{A,t5,2},{'$M_EXPR','$F_EXPR',0}},[27]},
1746          {{{A,t5,2},{'$M_EXPR',t,-1}},[26]}]} =
1747    xref:q(s, "(Lin) E"),
1748
1749    ok = check_state(s),
1750    xref:stop(s),
1751
1752    ok = file:delete(File),
1753    ok = file:delete(Beam),
1754    ok.
1755
1756%% OTP-5195: A bug fix when using qlc:q/1,2.
1757qlc(Conf) when is_list(Conf) ->
1758    Dir = ?copydir,
1759    File = fname(Dir, "qlc.erl"),
1760    MFile = fname(Dir, "qlc"),
1761    Beam = fname(Dir, "qlc.beam"),
1762    Test = <<"-module(qlc).
1763
1764              -include_lib(\"stdlib/include/qlc.hrl\").
1765
1766              -export([t/0]).
1767
1768              t() ->
1769                  dets:open_file(t, []),
1770                  dets:insert(t, [{1,a},{2,b},{3,c},{4,d}]),
1771                  MS = ets:fun2ms(fun({X,Y}) when (X > 1) or (X < 5) -> {Y}
1772                                  end),
1773                  QH1 = dets:table(t, [{traverse, {select, MS}}]),
1774                  QH2 = qlc:q([{Y} || {X,Y} <- dets:table(t),
1775                                      (X > 1) or (X < 5)]),
1776                  true = qlc:info(QH1) =:= qlc:info(QH2),
1777                  dets:close(t),
1778                  ok.
1779             ">>,
1780
1781    ok = file:write_file(File, Test),
1782    A = qlc,
1783    {ok, A} = compile:file(File, [debug_info,{outdir,Dir}]),
1784    {ok, _} = xref:start(s),
1785    {ok, A} = xref:add_module(s, MFile, {warnings,false}),
1786    {ok, _} = xref:q(s, "(Lin) E"), % is can be loaded
1787
1788    ok = check_state(s),
1789    xref:stop(s),
1790
1791    ok = file:delete(File),
1792    ok = file:delete(Beam),
1793    ok.
1794
1795
1796
1797%% Simple analyses
1798analyze(Conf) when is_list(Conf) ->
1799    S0 = new(),
1800    {{error, _, {invalid_options,[not_an_option]}}, _} =
1801      xref_base:analyze(S0, undefined_function_calls, [not_an_option]),
1802    {{error, _, {invalid_query,{q}}}, _} = xref_base:q(S0,{q}),
1803    {{error, _, {unknown_analysis,foo}}, _} = xref_base:analyze(S0, foo),
1804    {{error, _, {unknown_constant,"foo:bar/-1"}}, _} =
1805      xref_base:analyze(S0, {use,{foo,bar,-1}}),
1806
1807    CopyDir = ?copydir,
1808    Dir = fname(CopyDir,"rel2"),
1809    X = fname(Dir, "x.erl"),
1810    Y = fname(Dir, "y.erl"),
1811    A1_1 = fname([Dir,"lib","app1-1.1"]),
1812    A2 = fname([Dir,"lib","app2-1.1"]),
1813    EB1_1 = fname(A1_1, "ebin"),
1814    EB2 = fname(A2, "ebin"),
1815    Xbeam = fname(EB2, "x.beam"),
1816    Ybeam = fname(EB1_1, "y.beam"),
1817
1818    {ok, x} = compile:file(X, [debug_info, {outdir,EB2}]),
1819    {ok, y} = compile:file(Y, [debug_info, {outdir,EB1_1}]),
1820
1821    {ok, rel2, S1} = xref_base:add_release(S0, Dir, [{verbose,false}]),
1822    S = set_up(S1),
1823
1824    {ok, _} = analyze(undefined_function_calls, [{{x,xx,0},{x,undef,0}}], S),
1825    {ok, _} = analyze(undefined_functions, [{x,undef,0}], S),
1826    {ok, _} = analyze(locals_not_used, [{x,l,0},{x,l1,0}], S),
1827    {ok, _} = analyze(exports_not_used, [{x,xx,0},{y,t,0}], S),
1828
1829    {ok, _} = analyze(deprecated_function_calls, [{{y,t,0},{x,t,0}}], S),
1830    {ok, _} = analyze({deprecated_function_calls,next_version}, [], S),
1831    {ok, _} = analyze({deprecated_function_calls,next_major_release}, [], S),
1832    {ok, _} = analyze({deprecated_function_calls,eventually},
1833                      [{{y,t,0},{x,t,0}}], S),
1834    {ok, _} = analyze(deprecated_functions, [{x,t,0}], S),
1835    {ok, _} = analyze({deprecated_functions,next_version}, [], S),
1836    {ok, _} = analyze({deprecated_functions,next_major_release}, [], S),
1837    {ok, _} = analyze({deprecated_functions,eventually}, [{x,t,0}], S),
1838
1839    {ok, _} = analyze({call, {x,xx,0}}, [{x,undef,0}], S),
1840    {ok, _} = analyze({call, [{x,xx,0},{x,l,0}]}, [{x,l1,0},{x,undef,0}], S),
1841    {ok, _} = analyze({use, {x,l,0}}, [{x,l1,0}], S),
1842    {ok, _} = analyze({use, [{x,l,0},{x,l1,0}]}, [{x,l,0},{x,l1,0}], S),
1843
1844    {ok, _} = analyze({module_call, x}, [x], S),
1845    {ok, _} = analyze({module_call, [x,y]}, [x], S),
1846    {ok, _} = analyze({module_use, x}, [x,y], S),
1847    {ok, _} = analyze({module_use, [x,y]}, [x,y], S),
1848
1849    {ok, _} = analyze({application_call, app1}, [app2], S),
1850    {ok, _} = analyze({application_call, [app1,app2]}, [app2], S),
1851    {ok, _} = analyze({application_use, app2}, [app1,app2], S),
1852    {ok, _} = analyze({application_use, [app1,app2]}, [app1,app2], S),
1853
1854    ok = xref_base:delete(S),
1855    ok = file:delete(Xbeam),
1856    ok = file:delete(Ybeam),
1857    ok.
1858
1859%% Use of operators
1860basic(Conf) when is_list(Conf) ->
1861    S0 = new(),
1862
1863    F1 = {m1,f1,1},
1864    F6 = {m1,f2,6}, % X
1865    F2 = {m2,f1,2},
1866    F3 = {m2,f2,3}, % X
1867    F7 = {m2,f3,7}, % X
1868    F4 = {m3,f1,4}, % X
1869    F5 = {m3,f2,5},
1870
1871    UF1 = {m1,f12,17},
1872    UF2 = {m17,f17,177},
1873
1874    E1 = {F1,F3}, % X
1875    E2 = {F6,F7}, % X
1876    E3 = {F2,F6}, % X
1877    E4 = {F1,F4}, % X
1878    E5 = {F4,F5},
1879    E6 = {F7,F4}, % X
1880    E7 = {F1,F6},
1881
1882    UE1 = {F2,UF2}, % X
1883    UE2 = {F5,UF1}, % X
1884
1885    D1 = {F1,12},
1886    D6 = {F6,3},
1887    DefAt_m1 = [D1,D6],
1888    X_m1 = [F6],
1889    % L_m1 = [F1],
1890    XC_m1 = [E1,E2,E4],
1891    LC_m1 = [E7],
1892    LCallAt_m1 = [{E7,12}],
1893    XCallAt_m1 = [{E1,13},{E2,17},{E4,7}],
1894    Info1 = #xref_mod{name = m1, app_name = [a1]},
1895    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
1896                    XC_m1, LC_m1),
1897
1898    D2 = {F2,7},
1899    D3 = {F3,9},
1900    D7 = {F7,19},
1901    DefAt_m2 = [D2,D3,D7],
1902    X_m2 = [F3,F7],
1903    % L_m2 = [F2],
1904    XC_m2 = [E3,E6,UE1],
1905    LC_m2 = [],
1906    LCallAt_m2 = [],
1907    XCallAt_m2 = [{E3,96},{E6,12},{UE1,77}],
1908    Info2 = #xref_mod{name = m2, app_name = [a2]},
1909    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
1910                    XC_m2, LC_m2),
1911
1912    D4 = {F4,6},
1913    D5 = {F5,97},
1914    DefAt_m3 = [D4,D5],
1915    X_m3 = [F4],
1916    % L_m3 = [F5],
1917    XC_m3 = [UE2],
1918    LC_m3 = [E5],
1919    LCallAt_m3 = [{E5,19}],
1920    XCallAt_m3 = [{UE2,22}],
1921    Info3 = #xref_mod{name = m3, app_name = [a3]},
1922    S3 = add_module(S2, Info3, DefAt_m3, X_m3, LCallAt_m3, XCallAt_m3,
1923                    XC_m3, LC_m3),
1924
1925    Info4 = #xref_mod{name = m4, app_name = [a2]},
1926    S4 = add_module(S3, Info4, [], [], [], [], [], []),
1927
1928    AppInfo1 = #xref_app{name = a1, rel_name = [r1]},
1929    S9 = add_application(S4, AppInfo1),
1930    AppInfo2 = #xref_app{name = a2, rel_name = [r1]},
1931    S10 = add_application(S9, AppInfo2),
1932    AppInfo3 = #xref_app{name = a3, rel_name = [r2]},
1933    S11 = add_application(S10, AppInfo3),
1934
1935    RelInfo1 = #xref_rel{name = r1},
1936    S12 = add_release(S11, RelInfo1),
1937    RelInfo2 = #xref_rel{name = r2},
1938    S13 = add_release(S12, RelInfo2),
1939
1940    S = set_up(S13),
1941
1942    {ok, _} = eval("[m1,m2] + m:f/1", unknown_constant, S),
1943    {ok, _} = eval("[m1, m2, m:f/1]", type_mismatch, S),
1944
1945    {ok, _} = eval("[m1, m1->m2]", type_mismatch, S),
1946    {ok, _} = eval("components:f/1", unknown_constant, S),
1947    {ok, _} = eval("'of':f/1", unknown_constant, S),
1948    {ok, _} = eval("of:f/1", parse_error, S),
1949    {ok, _} = eval("components", unknown_constant, S),
1950    {ok, _} = eval("[components, of, closure]", parse_error, S),
1951    {ok, _} = eval("[components, 'of', closure]", unknown_constant, S),
1952
1953    {ok, _} = eval("[a1->a2,m1->m2]", type_mismatch, S),
1954    {ok, _} = eval("a1->a2,m1->m2", parse_error, S),
1955
1956    {ok, _} = eval("m1->a1", type_mismatch, S),
1957    {ok, _} = eval("[{m1,f1,1}] : App", parse_error, S),
1958    {ok, _} = eval("[{m1,f1,1}] : Fun", [F1], S),
1959    {ok, _} = eval("range X", type_error, S),
1960    {ok, _} = eval("domain X", type_error, S),
1961    {ok, _} = eval("range M", type_error, S),
1962    {ok, _} = eval("domain M", type_error, S),
1963
1964    % Misc.
1965    {ok, _} = eval("not_a_prefix_operator m1", parse_error, S),
1966    {ok, _} = eval(f("(Mod) ~p", [[F1,F6,F5]]), [m1,m3], S),
1967    {ok, _} = eval("(Lin) M - (Lin) m1",
1968                   [{F2,7},{F3,9},{F7,19},{F4,6},{F5,97},{UF2,0}], S),
1969    {ok, _} = eval(f("(Lin) M * (Lin) ~p", [[F1,F6]]),
1970                   [{F1,12},{F6,3}], S),
1971
1972    {ok, _} = eval(f("X * ~p", [[F1, F2, F3, F4, F5]]), [F3, F4], S),
1973    {ok, _} = eval("X", [F6,F3,F7,F4], S),
1974    {ok, _} = eval("X * AM", [F6,F3,F7,F4], S),
1975    {ok, _} = eval("X * a2", [F3,F7], S),
1976
1977    {ok, _} = eval("L * r1", [F1,F2], S),
1978    {ok, _} = eval("U", [UF1, UF2], S),
1979    {ok, _} = eval("U * AM", [UF1], S),
1980    {ok, _} = eval("U * UM", [UF2], S),
1981    {ok, _} = eval("XU * [m1, m2]", [F6,F3,F7,UF1], S),
1982    {ok, _} = eval("LU * [m3, m4]", [F5], S),
1983    {ok, _} = eval("UU", [F1,F2], S),
1984
1985    {ok, _} = eval("XC | m1", [E1,E2,E4], S),
1986    {ok, _} = eval(f("XC | ~p", [F1]), [E1,E4], S),
1987    {ok, _} = eval(f("(XXL) (Lin) (XC | ~p)", [F1]),
1988                   [{{D1,D3},[13]},{{D1,D4},[7]}],S),
1989    {ok, _} = eval(f("XC | (~p + ~p)", [F1, F2]), [E1,E4,E3,UE1], S),
1990    {ok, _} = eval(f("(XXL) (Lin) (XC | ~p)", [F1]),
1991                   [{{D1,D3},[13]},{{D1,D4},[7]}], S),
1992    {ok, _} = eval("LC | m3", [E5], S),
1993    {ok, _} = eval(f("LC | ~p", [F1]), [E7], S),
1994    {ok, _} = eval(f("LC | (~p + ~p)", [F1, F4]), [E7, E5], S),
1995    {ok, _} = eval("E | m1", [E1,E2,E4,E7], S),
1996    {ok, _} = eval(f("E | ~p", [F1]), [E1,E7,E4], S),
1997    {ok, _} = eval(f("E | (~p + ~p)", [F1, F2]), [E1,E7,E4,E3,UE1], S),
1998
1999    {ok, _} = eval("XC || m1", [E3,UE2], S),
2000    {ok, _} = eval(f("XC || ~p", [F6]), [E3], S),
2001    {ok, _} = eval(f("XC || (~p + ~p)", [F4, UF2]), [UE1,E4,E6], S),
2002    {ok, _} = eval("LC || m3", [E5], S),
2003    {ok, _} = eval(f("LC || ~p", [F1]), [], S),
2004    {ok, _} = eval(f("LC || ~p", [F6]), [E7], S),
2005    {ok, _} = eval(f("LC || (~p + ~p)", [F5, F6]), [E7,E5], S),
2006    {ok, _} = eval("E || m1", [E3,UE2,E7], S),
2007    {ok, _} = eval(f("E || ~p", [F6]), [E3,E7], S),
2008    {ok, _} = eval(f("E || (~p + ~p)", [F3,F4]), [E1,E4,E6], S),
2009
2010    {ok, _} = eval(f("~p + ~p", [F1,F2]), [F1,F2], S),
2011    {ok, _} = eval(f("~p * ~p", [m1,[F1,F6,F2]]), [F1,F6], S),
2012    {ok, _} = eval(f("~p * ~p", [F1,F2]), [], S),
2013
2014    %% range, domain
2015    {ok, _} = eval("range (E || m1)", [F6,UF1], S),
2016    {ok, _} = eval("domain (E || m1)", [F1,F2,F5], S),
2017    {ok, _} = eval(f("E | domain ~p", [[E1, {F2,F4}]]),
2018                   [E1,E7,E4,E3,UE1], S),
2019
2020    %% components, condensation, use, call
2021    {ok, _} = eval("(Lin) components E", type_error, S),
2022    {ok, _} = eval("components (Lin) E", type_error, S),
2023    {ok, _} = eval("components V", type_error, S),
2024    {ok, _} = eval("components E + components E", type_error, S),
2025
2026    {ok, _} = eval(f("range (closure E | ~p)", [[F1,F2]]),
2027                   [F6,F3,F7,F4,F5,UF1,UF2], S),
2028    {ok, _} =
2029    eval(f("domain (closure E || ~p)", [[UF2,F7]]), [F1,F2,F6], S),
2030    {ok, _} = eval("components E", [], S),
2031    {ok, _} = eval("components (Mod) E", [[m1,m2,m3]], S),
2032    {ok, _} = eval("components closure (Mod) E", [[m1,m2,m3]], S),
2033    {ok, _} = eval("condensation (Mod) E",
2034                   [{[m1,m2,m3],[m17]}], S),
2035    {ok, _} = eval("condensation closure (Mod) E",
2036                   [{[m1,m2,m3],[m17]}], S),
2037    {ok, _} = eval("condensation closure closure closure (Mod) E",
2038                   [{[m1,m2,m3],[m17]}], S),
2039    {ok, _} = eval("weak condensation (Mod) E",
2040                   [{[m1,m2,m3],[m1,m2,m3]},{[m1,m2,m3],[m17]},{[m17],[m17]}], S),
2041    {ok, _} = eval("strict condensation (Mod) E",
2042                   [{[m1,m2,m3],[m17]}], S),
2043    {ok, _} = eval("range condensation (Mod) E",
2044                   [[m17]], S),
2045    {ok, _} = eval("domain condensation (Mod) E",
2046                   [[m1,m2,m3]], S),
2047
2048    %% |, ||, |||
2049    {ok, _} = eval("(Lin) E || V", type_error, S),
2050    {ok, _} = eval("E ||| (Lin) V", type_error, S),
2051    {ok, _} = eval("E ||| m1", [E7], S),
2052    {ok, _} = eval("closure E ||| m1", [E7,{F1,UF1},{F6,UF1}], S),
2053    {ok, _} = eval("closure E ||| [m1,m2]",
2054                   [{F1,UF1},{F2,F7},{F1,F7},{F6,UF1},{F2,UF1},{F7,UF1},E7,E1,E2,E3], S),
2055    {ok, _} = eval("AE | a1", [{a1,a1},{a1,a2},{a1,a3}], S),
2056
2057    %% path ('of')
2058    {ok, _} = eval("(Lin) {m1,m2} of E", type_error, S),
2059    {ok, _} = eval("{m1,m2} of (Lin) E", type_error, S),
2060    [m1,m2] = eval("{m1,m2} of {m1,m2}", S),
2061    {ok, _} = eval("{m1,m2} of m1", type_error, S),
2062    {ok, _} = eval("{a3,m1} of ME", type_mismatch, S),
2063    [m1,m1] = eval("{m1} of ME", S),
2064    [m1,m1] = eval("{m1} of closure closure ME", S),
2065    false = eval("{m17} of ME", S),
2066    [m2,m1,m2] = eval("{m2} : Mod of ME", S),
2067    [m1,m2,m17] = eval("{m1, m17} of ME", S),
2068    [m1,m2,m17] = eval("m1 -> m17 of ME", S),
2069    {ok, _} = eval("[m1->m17,m17->m1] of ME", type_error, S),
2070    case eval(f("~p of E", [{F1,F7,UF1}]), S) of
2071        [F1,F6,F7,F4,F5,UF1] -> ok
2072    end,
2073    [a2,a1,a2] = eval("{a2} of AE", S),
2074
2075    %% weak/strict
2076    {ok, _} = eval("weak {m1,m2}", [{m1,m1},{m1,m2},{m2,m2}], S),
2077    {ok, _} = eval("strict [{m1,m1},{m1,m2},{m2,m2}]", [{m1,m2}], S),
2078    {ok, _} = eval("range weak [{m1,m2}] : Mod", [m1,m2], S),
2079    {ok, _} = eval("domain strict [{m1,m1},{m1,m2},{m2,m2}]", [m1], S),
2080
2081    %% #, number of
2082    {ok, _} = eval("# [{r1,r2}] : Rel", 1, S),
2083    {ok, _} = eval("# [{a3,a1}] : App", 1, S),
2084    {ok, _} = eval("# AE", 7, S),
2085    {ok, _} = eval("# ME", 8, S),
2086    {ok, _} = eval("# AE + # ME", 15, S),
2087    {ok, _} = eval("# AE * # ME", 56, S),
2088    {ok, _} = eval("# AE - # ME", -1, S),
2089    {ok, _} = eval("# E", 9, S),
2090    {ok, _} = eval("# V", 9, S),
2091    {ok, _} = eval("# (Lin) E", 9, S),
2092    {ok, _} = eval("# (ELin) E", 7, S),
2093    {ok, _} = eval("# closure E", type_error, S),
2094    {ok, _} = eval("# weak {m1,m2}", 3, S),
2095    {ok, _} = eval("#strict condensation (Mod) E", 1, S),
2096    {ok, _} = eval("#components closure (Mod) E", 1, S),
2097    {ok, _} = eval("# range strict condensation (Mod) E", 1, S),
2098    ok.
2099
2100%% The xref:m() and xref:d() functions
2101md(Conf) when is_list(Conf) ->
2102    CopyDir = ?copydir,
2103    Dir = fname(CopyDir,"md"),
2104    X = fname(Dir, "x__x.erl"),
2105    Y = fname(Dir, "y__y.erl"),
2106    Xbeam = fname(Dir, "x__x.beam"),
2107    Ybeam = fname(Dir, "y__y.beam"),
2108
2109    {error, _, {invalid_filename,{foo,bar}}} = xref:m({foo,bar}),
2110    {error, _, {invalid_filename,{foo,bar}}} = xref:d({foo,bar}),
2111
2112    {ok, x__x} = compile:file(X, [debug_info, {outdir,Dir}]),
2113    {ok, y__y} = compile:file(Y, [debug_info, {outdir,Dir}]),
2114
2115    {error, _, {no_such_module, foo_bar}} = xref:m(foo_bar),
2116    OldPath = code:get_path(),
2117    true = code:set_path([Dir | OldPath]),
2118    MInfo = xref:m(x__x),
2119    [{{x__x,t,1},{y__y,t,2}}] = info_tag(MInfo, undefined),
2120    [] = info_tag(MInfo, unused),
2121    [] = info_tag(MInfo, deprecated),
2122    DInfo = xref:d(Dir),
2123    [{{x__x,t,1},{y__y,t,2}}] = info_tag(DInfo, undefined),
2124    [{y__y,l,0},{y__y,l1,0}] = info_tag(DInfo, unused),
2125    [] = info_tag(MInfo, deprecated),
2126
2127    %% Switch from 'functions' mode to 'modules' mode.
2128    {ok, x__x} = compile:file(X, [no_debug_info, {outdir,Dir}]),
2129    {ok, y__y} = compile:file(Y, [no_debug_info, {outdir,Dir}]),
2130    MInfoMod = xref:m(x__x),
2131    [{y__y,t,2}] = info_tag(MInfoMod, undefined),
2132    [] = info_tag(MInfo, deprecated),
2133    DInfoMod = xref:d(Dir),
2134    [{y__y,t,2}] = info_tag(DInfoMod, undefined),
2135    [] = info_tag(MInfo, deprecated),
2136
2137    true = code:set_path(OldPath),
2138    ok = file:delete(Xbeam),
2139    ok = file:delete(Ybeam),
2140    ok.
2141
2142%% User queries
2143q(Conf) when is_list(Conf) ->
2144    S0 = new(),
2145    {ok, _} = eval("'foo", parse_error, S0),
2146    {ok, _} = eval("TT = E, TT = V", variable_reassigned, S0),
2147    {ok, _} = eval("TT = E, TTT", unknown_variable, S0),
2148    {ok, S} = eval("TT := E", [], S0),
2149    {ok, S1} = eval("TT * TT * TT", [], S),
2150    {ok, _S2} = xref_base:forget(S1, 'TT'),
2151    ok.
2152
2153%% Setting and getting values of query variables
2154variables(Conf) when is_list(Conf) ->
2155    Sa = new(),
2156    {{error, _, {invalid_options,[not_an_option]}}, _} =
2157    xref_base:variables(Sa, [not_an_option]),
2158    {error, _, {not_user_variable,foo}} = xref_base:forget(Sa, foo),
2159    Sa1 = set_up(Sa),
2160    {error, _, {not_user_variable,foo}} = xref_base:forget(Sa1, foo),
2161    ok = xref_base:delete(Sa1),
2162
2163    S0 = new(),
2164
2165    F1 = {m1,f1,1},
2166    F2 = {m2,f1,2},
2167    Lib = {lib1,f1,1}, % undefined
2168
2169    E1 = {F1,F2},
2170    E2 = {F2,F1},
2171    E3 = {F1,Lib},
2172
2173    D1 = {F1,12},
2174    DefAt_m1 = [D1],
2175    X_m1 = [F1],
2176    % L_m1 = [],
2177    XC_m1 = [E1,E3],
2178    LC_m1 = [],
2179    LCallAt_m1 = [],
2180    XCallAt_m1 = [{E1,13},{E3,17}],
2181    Info1 = #xref_mod{name = m1, app_name = [a1]},
2182    S1 = add_module(S0, Info1, DefAt_m1, X_m1, LCallAt_m1, XCallAt_m1,
2183                    XC_m1, LC_m1),
2184
2185    D2 = {F2,7},
2186    DefAt_m2 = [D2],
2187    X_m2 = [F2],
2188    % L_m2 = [],
2189    XC_m2 = [E2],
2190    LC_m2 = [],
2191    LCallAt_m2 = [],
2192    XCallAt_m2 = [{E2,96}],
2193    Info2 = #xref_mod{name = m2, app_name = [a2]},
2194    S2 = add_module(S1, Info2, DefAt_m2, X_m2, LCallAt_m2, XCallAt_m2,
2195                    XC_m2, LC_m2),
2196
2197    S = set_up(S2),
2198
2199    eval("T1=E, T2=E*T1, T3 = T2*T2, T4=range T3, T5=T3|T4, T5",
2200         [E1,E2,E3], S),
2201    eval("((E*E)*(E*E)) | (range ((E*E)*(E*E)))",
2202         [E1,E2,E3], S),
2203    eval("T1=V*V,T2=T1*V,T3=V*V*V,T3",
2204         [F1,F2,Lib], S),
2205    eval("T1=V*V, T2=V*V, T1*T2",
2206         [F1,F2,Lib], S),
2207
2208    {ok, S100} = eval("T0 := E", [E1, E2, E3], S),
2209    {ok, S101} = eval("T1 := E  | m1", [E1, E3], S100),
2210    {ok, S102} = eval("T2 := E  | m2", [E2], S101),
2211    {{ok, [{user, ['T0', 'T1', 'T2']}]}, _} = xref_base:variables(S102),
2212    {ok, S103} = xref_base:forget(S102, 'T0'),
2213    {{ok, [{user, ['T1', 'T2']}]}, S104} =
2214    xref_base:variables(S103, [user]),
2215    {ok, S105} = xref_base:forget(S104),
2216    {{ok, [{user, []}]}, S106} = xref_base:variables(S105),
2217    {{ok, [{predefined,_}]}, S107_0} =
2218    xref_base:variables(S106, [predefined]),
2219
2220    {ok, S107_1} =
2221    eval("TT := E, TT2 := V, TT1 := TT * TT", [E1,E2,E3], S107_0),
2222    {{ok, [{user, ['TT', 'TT1', 'TT2']}]}, _} =
2223    xref_base:variables(S107_1),
2224    {ok, S107} = xref_base:forget(S107_1),
2225
2226    CopyDir = ?copydir,
2227    Dir = fname(CopyDir,"lib_test"),
2228    Beam = fname(Dir, "lib1.beam"),
2229
2230    copy_file(fname(Dir, "lib1.erl"), Beam),
2231    {ok, S108} =
2232    xref_base:set_library_path(S107, [Dir], [{verbose,false}]),
2233    {{error, _, _}, _} = xref_base:variables(S108, [{verbose,false}]),
2234    {ok, S109} = xref_base:set_library_path(S108, [], [{verbose,false}]),
2235
2236    NoOfTables = erlang:system_info(ets_count),
2237
2238    {ok, S110} = eval("Eplus := closure E, TT := Eplus",
2239                      'closure()', S109),
2240    {{ok, [{user, ['Eplus','TT']}]}, S111} = xref_base:variables(S110),
2241    {ok, S112} = xref_base:forget(S111, ['TT','Eplus']),
2242    true = NoOfTables =:= erlang:system_info(ets_count),
2243
2244    {ok, NS0} = eval("Eplus := closure E", 'closure()', S112),
2245    {{ok, [{user, ['Eplus']}]}, NS} = xref_base:variables(NS0),
2246    ok = xref_base:delete(NS),
2247    true = NoOfTables =:= erlang:system_info(ets_count),
2248
2249    ok = file:delete(Beam),
2250    ok.
2251
2252%% OTP-5071. Too many unused functions.
2253unused_locals(Conf) when is_list(Conf) ->
2254    Dir = ?copydir,
2255
2256    File1 = fname(Dir, "a.erl"),
2257    MFile1 = fname(Dir, "a"),
2258    Beam1 = fname(Dir, "a.beam"),
2259    Test1 = <<"-module(a).
2260               -export([f/1, g/2]).
2261
2262               f(X) ->
2263                   Y = b:f(X),
2264                   Z = b:g(Y),
2265                   start(b, h, [Z]).
2266
2267               g(X, Y) ->
2268                   ok.
2269
2270               start(M, F, A) ->
2271                   spawn(M, F, A).
2272             ">>,
2273    ok = file:write_file(File1, Test1),
2274    {ok, a} = compile:file(File1, [debug_info,{outdir,Dir}]),
2275
2276    File2 = fname(Dir, "b.erl"),
2277    MFile2 = fname(Dir, "b"),
2278    Beam2 = fname(Dir, "b.beam"),
2279    Test2 = <<"-module(b).
2280               -export([f/1, g/2]).
2281
2282               f(X) ->
2283                   io:write(\"~w\", [X]),
2284                   a:start(timer, sleep, [1000]).
2285
2286               g(X, Y) ->
2287                   apply(a, g, [X, Y]).
2288             ">>,
2289
2290    ok = file:write_file(File2, Test2),
2291    {ok, b} = compile:file(File2, [debug_info,{outdir,Dir}]),
2292
2293    {ok, _} = xref:start(s),
2294    {ok, a} = xref:add_module(s, MFile1),
2295    {ok, b} = xref:add_module(s, MFile2),
2296    {ok, []} = xref:analyse(s, locals_not_used),
2297    ok = check_state(s),
2298    xref:stop(s),
2299
2300    ok = file:delete(File1),
2301    ok = file:delete(Beam1),
2302    ok = file:delete(File2),
2303    ok = file:delete(Beam2),
2304    ok.
2305
2306
2307%% Format error messages
2308format_error(Conf) when is_list(Conf) ->
2309    {ok, _Pid} = start(s),
2310    ok = xref:set_default(s, [{verbose,false}, {warnings, false}]),
2311
2312    %% Parse error messages.
2313    "Invalid regular expression \"add(\"" ++ _ = fstring(xref:q(s,'"add("')),
2314    'Invalid operator foo\n' = fatom(xref:q(s,'foo E')),
2315    'Invalid wildcard variable \'_Var\' (only \'_\' is allowed)\n'
2316      = fatom(xref:q(s,"module:function/_Var")),
2317    'Missing type of regular expression ".*"\n'
2318      = fatom(xref:q(s,'".*"')),
2319    'Type does not match structure of constant: \'M\' : Fun\n'
2320      = fatom(xref:q(s,"'M' : Fun")),
2321    'Type does not match structure of constant: ".*" : Fun\n'
2322      = fatom(xref:q(s,'".*" : Fun')),
2323    'Type does not match structure of constant: [m:f/1, m1:f2/3] : App\n'
2324      = fatom(xref:q(s,"[m:f/1,m1:f2/3] : App")),
2325    'Parse error on line 1: syntax error before: \'-\'\n'
2326      = fatom(xref:q(s,"E + -")),
2327    "Parse error on line 1: unterminated atom starting with 'foo'\n"
2328      = flatten(xref:format_error(xref:q(s,"'foo"))),
2329    'Parse error at end of string: syntax error before: \n'
2330      = fatom(xref:q(s,"E +")),
2331    'Parse error on line 1: syntax error before: \'Lin\'\n'
2332      = fatom(xref:q(s,"Lin")),
2333
2334    %% Other messages
2335    'Variable \'QQ\' used before set\n' = fatom(xref:q(s,"QQ")),
2336    'Unknown constant a\n' = fatom(xref:q(s,"{a} of E")),
2337
2338    %% Testing xref_parser:t2s/1.
2339    'Variable assigned more than once: E := E + E\n' = fatom(xref:q(s,"E:=E + E")),
2340    'Variable assigned more than once: E = E + E\n' = fatom(xref:q(s,"E=E + E")),
2341    "Operator applied to argument(s) of different or invalid type(s): "
2342    "E + V * V\n" = flatten(xref:format_error(xref:q(s,"E + (V * V)"))),
2343    {error,xref_compiler,{type_error,"(V + V) * E"}} = xref:q(s,"(V + V) * E"),
2344    "Type does not match structure of constant: [m:f/3 -> g:h/17] : "
2345    "App\n" = flatten(xref:format_error(xref:q(s,"[{{m,f,3},{g,h,17}}] : App"))),
2346    'Type does not match structure of constant: [m -> f, g -> h] : Fun\n'
2347      = fatom(xref:q(s,"[{m,f},g->h] : Fun")),
2348    'Type does not match structure of constant: {m, n, o} : Fun\n' =
2349    fatom(xref:q(s,"{m,n,o} : Fun")),
2350    {error,xref_compiler,{type_error,"range (Lin) V"}} =
2351      xref:q(s,"range ((Lin) V)"),
2352    {error,xref_compiler,{type_error,"condensation range E"}} =
2353      xref:q(s,"condensation (range E)"),
2354    {error,xref_compiler,{type_error,"condensation (# E + # V)"}} =
2355      xref:q(s,"condensation (# E + # V)"),
2356    {error,xref_compiler,{type_error,"range (# E + # E)"}} =
2357      xref:q(s,"range (#E + #E)"),
2358    {error,xref_compiler,{type_error,"range (# E)"}} =
2359      xref:q(s,"range #E"), % Hm...
2360    {error,xref_compiler,{type_error,"E + # E"}} =
2361      xref:q(s,"E + #E + #E"), % Hm...
2362    {error,xref_compiler,{type_error,"V * E || V | V"}} =
2363      xref:q(s,"V * (E || V) | V"),
2364    {error,xref_compiler,{type_error,"E || (E | V)"}} =
2365      xref:q(s,"V * E || (E | V)"),
2366    {error,xref_compiler,{type_error,"E * \"m\" : Mod"}} =
2367      xref:q(s,'E * "m" : Mod'),
2368    {error,xref_compiler,{type_error,"E * (\"m\":f/_ + m:\"f\"/3)"}} =
2369      xref:q(s,'E * ("m":f/_ + m:"f"/3)'),
2370
2371    xref:stop(s),
2372    ok.
2373
2374%% OTP-7423. Xref scanner bug.
2375otp_7423(Conf) when is_list(Conf) ->
2376    {ok, _Pid} = start(s),
2377    S = "E | [compiler] : App || [{erlang,
2378                                   size,
2379                                   1}] : Fun",
2380    {error,xref_compiler,{unknown_constant,"compiler"}} = xref:q(s,S),
2381    xref:stop(s),
2382    ok.
2383
2384%% OTP-7831. Allow anonymous Xref processes.
2385otp_7831(Conf) when is_list(Conf) ->
2386    {ok, Pid1} = xref:start([]),
2387    xref:stop(Pid1),
2388    {ok, Pid2} = xref:start([{xref_mode, modules}]),
2389    xref:stop(Pid2),
2390    ok.
2391
2392%% OTP-10192. Allow filenames with character codes greater than 126.
2393otp_10192(Conf) when is_list(Conf) ->
2394    PrivDir = ?privdir,
2395    {ok, _Pid} = xref:start(s),
2396    Dir = filename:join(PrivDir, "ä"),
2397    ok = file:make_dir(Dir),
2398    {ok, []} = xref:add_directory(s, Dir),
2399    xref:stop(s),
2400    ok.
2401
2402otp_13708(Conf) when is_list(Conf) ->
2403    {ok, _} = start(s),
2404    ok = xref:set_default(s, [{verbose, true}]),
2405    {ok, []} = xref:q(s,"E"),
2406    xref:stop(s),
2407
2408    CopyDir = ?copydir,
2409    Dir = fname(CopyDir,"lib_test"),
2410    {ok, _} = start(s),
2411    ok = xref:set_library_path(s, [Dir], [{verbose, true}]),
2412    xref:stop(s).
2413
2414%% OTP-14464. Unicode atoms.
2415otp_14464(Conf) when is_list(Conf) ->
2416    Dir = ?copydir,
2417
2418    File1 = fname(Dir, "a.erl"),
2419    MFile1 = fname(Dir, "a"),
2420    Beam1 = fname(Dir, "a.beam"),
2421    Test1 = "-module(a).
2422             -export([ärlig/0, 'кlирилли́ческий атомB'/0]).
2423
2424              ärlig() ->
2425                  'кlирилли́ческий атомB'.
2426
2427              'кlирилли́ческий атомB'() ->
2428                  foo.
2429             ",
2430    ok = file:write_file(File1, unicode:characters_to_binary(Test1)),
2431    {ok, a} = compile:file(File1, [debug_info,{outdir,Dir}]),
2432
2433    {ok, _} = xref:start(s),
2434    {ok, a} = xref:add_module(s, MFile1),
2435
2436    {ok, [{arlig,0}]} = xref:q(s, 'a:"ärlig"/0'),
2437    {ok, [{a,'кlирилли́ческий атомB',0}]} =
2438        xref:q(s, 'a:"кlирилли́ческий атомB"/0'),
2439
2440    xref:stop(s),
2441    ok = file:delete(File1),
2442    ok = file:delete(Beam1).
2443
2444%% OTP-14344. -on_load() attribute.
2445otp_14344(Conf) when is_list(Conf) ->
2446    Dir = ?copydir,
2447
2448    File1 = fname(Dir, "a.erl"),
2449    MFile1 = fname(Dir, "a"),
2450    Beam1 = fname(Dir, "a.beam"),
2451    Test1 = <<"-module(a).
2452               -on_load(doit/0).
2453               doit() -> ok.
2454              ">>,
2455    ok = file:write_file(File1, Test1),
2456    {ok, a} = compile:file(File1, [debug_info,{outdir,Dir}]),
2457
2458    {ok, _} = xref:start(s),
2459    {ok, a} = xref:add_module(s, MFile1),
2460
2461    {ok, [{a,doit,0}]} = xref:q(s, "OL"),
2462    {ok, []} = xref:analyze(s, locals_not_used),
2463
2464    xref:stop(s),
2465    ok = file:delete(File1),
2466    ok = file:delete(Beam1).
2467
2468%%%
2469%%% Utilities
2470%%%
2471
2472copy_file(Src, Dest) ->
2473    file:copy(Src, Dest).
2474
2475fname(N) ->
2476    filename:join(N).
2477
2478fname(Dir, Basename) ->
2479    filename:join(Dir, Basename).
2480
2481new() ->
2482    {ok, S} = xref_base:new(),
2483    S.
2484
2485set_up(S) ->
2486    {ok, S1} = xref_base:set_up(S, [{verbose, false}]),
2487    S1.
2488
2489eval(Query, E, S) ->
2490    ?format("------------------------------~n", []),
2491    ?format("Evaluating ~p~n", [Query]),
2492    {Answer, NewState} = xref_base:q(S, Query, [{verbose, false}]),
2493    {Reply, Expected} =
2494    case Answer of
2495        {ok, R} when is_list(E) ->
2496            {unsetify(R), sort(E)};
2497        {ok, R} ->
2498            {unsetify(R), E};
2499        {error, _Module, Reason} ->
2500            {element(1, Reason), E}
2501    end,
2502    if
2503        Reply =:= Expected ->
2504            ?format("As expected, got ~n~p~n", [Expected]),
2505            {ok, NewState};
2506        true ->
2507            ?format("Expected ~n~p~nbut got ~n~p~n", [Expected, Reply]),
2508            not_ok
2509    end.
2510
2511analyze(Query, E, S) ->
2512    ?format("------------------------------~n", []),
2513    ?format("Evaluating ~p~n", [Query]),
2514    {{ok, L}, NewState} =
2515    xref_base:analyze(S, Query, [{verbose, false}]),
2516    case {unsetify(L), sort(E)} of
2517        {X,X} ->
2518            ?format("As was expected, got ~n~p~n", [X]),
2519            {ok, NewState};
2520        {_R,_X} ->
2521            ?format("Expected ~n~p~nbut got ~n~p~n", [_X, _R]),
2522            not_ok
2523    end.
2524
2525unsetify(S) ->
2526    case is_sofs_set(S) of
2527        true -> to_external(S);
2528        false -> S
2529    end.
2530
2531%% Note: assumes S has been set up; the new state is not returned
2532eval(Query, S) ->
2533    {{ok, Answer}, _NewState} =
2534    xref_base:q(S, Query, [{verbose, false}]),
2535    unsetify(Answer).
2536
2537add_module(S, XMod, DefAt, X, LCallAt, XCallAt, XC, LC) ->
2538    Attr = {[], [], []},
2539    Depr0 = {[], [], [], []},
2540    DBad = [],
2541    Depr = {Depr0,DBad},
2542    OL = [],
2543    Data = {DefAt, LCallAt, XCallAt, LC, XC, X, Attr, Depr, OL},
2544    Unres = [],
2545    {ok, _Module, _Bad, State} =
2546    xref_base:do_add_module(S, XMod, Unres, Data),
2547    State.
2548
2549add_application(S, XApp) ->
2550    xref_base:do_add_application(S, XApp).
2551
2552add_release(S, XRel) ->
2553    xref_base:do_add_release(S, XRel).
2554
2555remove_module(S, M) ->
2556    xref_base:do_remove_module(S, M).
2557
2558info_tag(Info, Tag) ->
2559    {value, {_Tag, Value}} = lists:keysearch(Tag, 1, Info),
2560    Value.
2561
2562make_ufile(FileName) ->
2563    ok = file:write_file(FileName, term_to_binary(foo)),
2564    hide_file(FileName).
2565
2566make_udir(Dir) ->
2567    ok = file:make_dir(Dir),
2568    hide_file(Dir).
2569
2570hide_file(FileName) ->
2571    {ok, FileInfo} = file:read_file_info(FileName),
2572    NewFileInfo = FileInfo#file_info{mode = 0},
2573    ok = file:write_file_info(FileName, NewFileInfo).
2574
2575%% Note that S has to be set up before calling this checking function.
2576check_state(S) ->
2577    Info = xref:info(S),
2578
2579    modules_mode_check(S, Info),
2580    case info(Info, mode) of
2581        modules ->
2582            ok;
2583        functions ->
2584            functions_mode_check(S, Info)
2585    end.
2586
2587%% The manual mentions some facts that should always hold.
2588%% Here they are again.
2589functions_mode_check(S, Info) ->
2590    %% F = L + X,
2591    {ok, F} = xref:q(S, "F"),
2592    {ok, F} = xref:q(S, "L + X"),
2593
2594    %% V = X + L + B + U,
2595    {ok, V} = xref:q(S, "V"),
2596    {ok, V} = xref:q(S, "X + L + B + U"),
2597
2598    %% X, L, B and U are disjoint.
2599    {ok, []} =
2600    xref:q(S, "X * L + X * B + X * U + L * B + L * U + B * U"),
2601
2602    %% V = UU + XU + LU,
2603    {ok, V} = xref:q(S, "UU + XU + LU"),
2604
2605    %% E = LC + XC
2606    {ok, E} = xref:q(S, "E"),
2607    {ok, E} = xref:q(S, "LC + XC"),
2608
2609    %% U subset of XU,
2610    {ok, []} = xref:q(S, "U - XU"),
2611
2612    %% LU = range LC
2613    {ok, []} = xref:q(S, "(LU - range LC) + (range LC - LU)"),
2614
2615    %% XU = range XC
2616    {ok, []} = xref:q(S, "(XU - range XC) + (range XC - XU)"),
2617
2618    %% LU subset F
2619    {ok, []} = xref:q(S, "LU - F"),
2620
2621    %% UU subset F
2622    {ok, []} = xref:q(S, "UU - F"),
2623
2624    %% OL subset F
2625    {ok, []} = xref:q(S, "OL - F"),
2626
2627    %% ME = (Mod) E
2628    {ok, ME} = xref:q(S, "ME"),
2629    {ok, ME} = xref:q(S, "(Mod) E"),
2630
2631    %% AE = (App) E
2632    {ok, AE} = xref:q(S, "AE"),
2633    {ok, AE} = xref:q(S, "(App) E"),
2634
2635    %% RE = (Rel) E
2636    {ok, RE} = xref:q(S, "RE"),
2637    {ok, RE} = xref:q(S, "(Rel) E"),
2638
2639    %% (Mod) V subset of M
2640    {ok, []} = xref:q(S, "(Mod) V - M"),
2641
2642    %% range UC subset of U
2643    {ok, []} = xref:q(S, "range UC - U"),
2644
2645    %% Some checks on the numbers returned by the info functions.
2646
2647    {Resolved, Unresolved} = info(Info, no_calls),
2648    AllCalls = Resolved + Unresolved,
2649    {ok, AllCalls} = xref:q(S, "# (XLin) E + # (LLin) E"),
2650
2651    {Local, Exported} = info(Info, no_functions),
2652    LX = Local+Exported,
2653    {ok, LXs} = xref:q(S, 'Extra = _:module_info/"(0|1)" + LM,
2654                                  # (F - Extra)'),
2655    true = LX =:= LXs,
2656
2657    {LocalCalls, ExternalCalls, UnresCalls} =
2658    info(Info, no_function_calls),
2659    LEU = LocalCalls + ExternalCalls + UnresCalls,
2660    {ok, LEU} = xref:q(S, "# LC + # XC"),
2661
2662    InterFunctionCalls = info(Info, no_inter_function_calls),
2663    {ok, InterFunctionCalls} = xref:q(S, "# EE"),
2664
2665    %% And some more checks on counters...
2666    check_count(S),
2667
2668    %% ... and more
2669    {ok, []} = xref:q(S, "LM - X - U - B"),
2670
2671    ok.
2672
2673modules_mode_check(S, Info) ->
2674    %% B subset of XU,
2675    {ok, []} = xref:q(S, "B - XU"),
2676
2677    %% M = AM + LM + UM
2678    {ok, M} = xref:q(S, "M"),
2679    {ok, M} = xref:q(S, "AM + LM + UM"),
2680
2681    %% DF is a subset of X U B, etc.
2682    {ok, []} = xref:q(S, "DF - X - B"),
2683    {ok, []} = xref:q(S, "DF_3 - DF"),
2684    {ok, []} = xref:q(S, "DF_2 - DF_3"),
2685    {ok, []} = xref:q(S, "DF_1 - DF_2"),
2686
2687    %% AM, LM and UM are disjoint.
2688    {ok, []} = xref:q(S, "AM * LM + AM * UM + LM * UM"),
2689
2690    %% (App) M subset of A
2691    {ok, []} = xref:q(S, "(App) M - A"),
2692
2693    AM = info(Info, no_analyzed_modules),
2694    {ok, AM} = xref:q(S, "# AM"),
2695
2696    A = info(Info, no_applications),
2697    {ok, A} = xref:q(S, "# A"),
2698
2699    NoR = info(Info, no_releases),
2700    {ok, NoR} = xref:q(S, "# R"),
2701
2702    ok.
2703
2704%% Checks the counters of some of the overall and modules info functions.
2705%% (Applications and releases are not checked.)
2706check_count(S) ->
2707    %%{ok, R} = xref:q(S, 'R'),
2708    %% {ok, A} = xref:q(S, 'A'),
2709    {ok, M} = xref:q(S, 'AM'),
2710
2711    {ok, _} = xref:q(S,
2712                     "Extra := _:module_info/\"(0|1)\" + LM"),
2713
2714    %% info/1:
2715    {ok, NoR} = xref:q(S, '# R'),
2716    {ok, NoA} = xref:q(S, '# A'),
2717    {ok, NoM} = xref:q(S, '# AM'),
2718    {ok, NoCalls} = xref:q(S, '# (XLin) E + # (LLin) E'),
2719    {ok, NoFunCalls} = xref:q(S, '# E'),
2720    {ok, NoXCalls} = xref:q(S, '# XC'),
2721    {ok, NoLCalls} = xref:q(S, '# LC'),
2722    {ok, NoLXCalls} = xref:q(S, '# (XC * LC)'),
2723    NoAllCalls = NoXCalls + NoLCalls,
2724    {ok, NoFun} = xref:q(S, '# (F - Extra)'),
2725    {ok, NoICalls} = xref:q(S, '# EE'),
2726
2727    Info = xref:info(S),
2728    NoR = info(Info, no_releases),
2729    NoA = info(Info, no_applications),
2730    NoM = info(Info, no_analyzed_modules),
2731    {NoResolved, NoUC} = info(Info, no_calls),
2732    NoCalls = NoResolved + NoUC,
2733    {NoLocal, NoExternal, NoUnres} = info(Info, no_function_calls),
2734    NoAllCalls = NoLocal + NoExternal + NoUnres,
2735    NoAllCalls = NoFunCalls + NoLXCalls,
2736    {NoLocalFuns, NoExportedFuns} = info(Info, no_functions),
2737    NoFun = NoLocalFuns + NoExportedFuns,
2738    NoICalls = info(Info, no_inter_function_calls),
2739
2740    %% per module
2741    info_module(M, S),
2742
2743    ok.
2744
2745info_module([M | Ms], S) ->
2746    {ok, NoCalls} = per_module("T = (E | ~p : Mod), # (XLin) T + # (LLin) T",
2747                               M, S),
2748    {ok, NoFunCalls} = per_module("# (E | ~p : Mod)", M, S),
2749    {ok, NoXCalls} = per_module("# (XC | ~p : Mod)", M, S),
2750    {ok, NoLCalls} = per_module("# (LC | ~p : Mod)", M, S),
2751    {ok, NoLXCalls} = per_module("# ((XC * LC) | ~p : Mod)", M, S),
2752    NoAllCalls = NoXCalls + NoLCalls,
2753    {ok, NoFun} = per_module("# (F * ~p : Mod - Extra)", M, S),
2754    {ok, NoICalls} = per_module("# (EE | ~p : Mod)", M, S),
2755
2756    [{_M,Info}] = xref:info(S, modules, M),
2757    {NoResolved, NoUC} = info(Info, no_calls),
2758    NoCalls = NoResolved + NoUC,
2759    {NoLocal, NoExternal, NoUnres} = info(Info, no_function_calls),
2760    NoAllCalls = NoLocal + NoExternal + NoUnres,
2761    NoAllCalls = NoFunCalls + NoLXCalls,
2762    {NoLocalFuns, NoExportedFuns} = info(Info, no_functions),
2763    NoFun = NoLocalFuns + NoExportedFuns,
2764    NoICalls = info(Info, no_inter_function_calls),
2765
2766    info_module(Ms, S);
2767info_module([], _S) ->
2768    ok.
2769
2770per_module(Q, M, S) ->
2771    xref:q(S, f(Q, [M])).
2772
2773info(Info, What) ->
2774    {value, {What, Value}} = lists:keysearch(What, 1, Info),
2775    Value.
2776
2777f(S, A) ->
2778    flatten(io_lib:format(S, A)).
2779
2780fatom(R) ->
2781    list_to_atom(fstring(R)).
2782
2783fstring(R) ->
2784    flatten(xref:format_error(R)).
2785
2786start(Server) ->
2787    case xref:start(Server) of
2788        {error, {already_started, _Pid}} ->
2789            xref:stop(Server),
2790            xref:start(Server);
2791        R -> R
2792    end.
2793
2794add_erts_code_path(KernelPath) ->
2795    VersionDirs =
2796    filelib:is_dir(
2797      filename:join(
2798        [code:lib_dir(),
2799         lists:flatten(
2800           ["kernel-",
2801            [X ||
2802             {kernel,_,X} <-
2803             application_controller:which_applications()]])])),
2804    case VersionDirs of
2805        true ->
2806            case code:lib_dir(erts) of
2807                String when is_list(String) ->
2808                    [KernelPath, fname(String,"ebin")];
2809                _Other1 ->
2810                    [KernelPath]
2811            end;
2812        false ->
2813            % Clearcase?
2814            PrelPath = filename:join([code:lib_dir(),"..","erts","preloaded"]),
2815            case filelib:is_dir(PrelPath) of
2816                true ->
2817                    [KernelPath, fname(PrelPath,"ebin")];
2818                false ->
2819                    [KernelPath]
2820            end
2821    end.
2822