File 2940-Suppress-xref-output-for-unused-export-on-behaviour-.patch of Package erlang
From d74e9b343a8562aa2ffceb07ad44a1fa8fd45f48 Mon Sep 17 00:00:00 2001
From: "Paulo F. Oliveira" <paulo.ferraz.oliveira@gmail.com>
Date: Thu, 17 Sep 2020 00:25:20 +0100
Subject: [PATCH] Suppress xref output for unused export on behaviour's
behaviour_info/1
This is a special (albeit optional) case, generated internally
by Erlang, so should not be part of the analysis' output
Tools depending on the analysis output will force the consumer
to add exceptions to them
---
lib/tools/src/xref_reader.erl | 2 ++
lib/tools/test/xref_SUITE.erl | 26 +++++++++++++++++--
.../test/xref_SUITE_data/lib_test/bi.erl | 3 +++
.../test/xref_SUITE_data/lib_test/no_bi.erl | 6 +++++
4 files changed, 35 insertions(+), 2 deletions(-)
create mode 100644 lib/tools/test/xref_SUITE_data/lib_test/bi.erl
create mode 100644 lib/tools/test/xref_SUITE_data/lib_test/no_bi.erl
diff --git a/lib/tools/src/xref_reader.erl b/lib/tools/src/xref_reader.erl
index d28bdb78db..87c02db9eb 100644
--- a/lib/tools/src/xref_reader.erl
+++ b/lib/tools/src/xref_reader.erl
@@ -95,6 +95,8 @@ form({function, _, module_info, 0, _Clauses}, S) ->
S;
form({function, _, module_info, 1, _Clauses}, S) ->
S;
+form({function, 0 = _Line, behaviour_info, 1, _Clauses}, S) ->
+ S;
form({function, Anno, Name, Arity, Clauses}, S) ->
MFA0 = {S#xrefr.module, Name, Arity},
MFA = adjust_arity(S, MFA0),
diff --git a/lib/tools/test/xref_SUITE.erl b/lib/tools/test/xref_SUITE.erl
index c84f47c207..b5b3ff7796 100644
--- a/lib/tools/test/xref_SUITE.erl
+++ b/lib/tools/test/xref_SUITE.erl
@@ -48,7 +48,8 @@
fun_mfa/1, fun_mfa_r14/1,
fun_mfa_vars/1, qlc/1]).
--export([analyze/1, basic/1, md/1, q/1, variables/1, unused_locals/1]).
+-export([analyze/1, basic/1, md/1, q/1, variables/1, unused_locals/1,
+ behaviour_info_t/1, fake_behaviour_info_t/1]).
-export([format_error/1, otp_7423/1, otp_7831/1, otp_10192/1, otp_13708/1,
otp_14464/1, otp_14344/1]).
@@ -83,7 +84,7 @@ groups() ->
fun_mfa_r14, fun_mfa_vars, qlc]},
{analyses, [],
- [analyze, basic, md, q, variables, unused_locals]},
+ [analyze, basic, md, q, variables, unused_locals, behaviour_info_t, fake_behaviour_info_t]},
{misc, [], [format_error, otp_7423, otp_7831, otp_10192, otp_13708,
otp_14464, otp_14344]}].
@@ -2825,3 +2826,24 @@ add_erts_code_path(KernelPath) ->
[KernelPath]
end
end.
+
+behaviour_info_t(Config) ->
+ bi_t(_Module = bi,
+ _IsExportNotUsed = false,
+ Config).
+
+fake_behaviour_info_t(Config) ->
+ bi_t(_Module = no_bi,
+ _IsExportNotUsed = true,
+ Config).
+
+bi_t(Module, IsExportNotUsed, Conf) ->
+ LibTestDir = fname(?copydir, "lib_test"),
+ XRefServer = s,
+ {ok, Module} = compile:file(fname(LibTestDir, Module),
+ [debug_info, {outdir, LibTestDir}]),
+ {ok, _} = start(XRefServer),
+ {ok, Module} = xref:add_module(XRefServer, fname(LibTestDir, Module)),
+ {ok, MFAs} = xref:analyze(XRefServer, exports_not_used),
+ true = lists:member({Module, behaviour_info, 1}, MFAs) =:= IsExportNotUsed,
+ _ = xref:stop(XRefServer).
diff --git a/lib/tools/test/xref_SUITE_data/lib_test/bi.erl b/lib/tools/test/xref_SUITE_data/lib_test/bi.erl
new file mode 100644
index 0000000000..e083fa0f3c
--- /dev/null
+++ b/lib/tools/test/xref_SUITE_data/lib_test/bi.erl
@@ -0,0 +1,3 @@
+-module(bi).
+
+-callback a() -> ok.
diff --git a/lib/tools/test/xref_SUITE_data/lib_test/no_bi.erl b/lib/tools/test/xref_SUITE_data/lib_test/no_bi.erl
new file mode 100644
index 0000000000..5aac4a193e
--- /dev/null
+++ b/lib/tools/test/xref_SUITE_data/lib_test/no_bi.erl
@@ -0,0 +1,6 @@
+-module(no_bi).
+
+-export([behaviour_info/1]).
+
+behaviour_info(_) ->
+ ok.
--
2.26.2