xref.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. [[xref]]
  2. == Xref
  3. Xref is a cross reference tool for analyzing dependencies
  4. between functions, modules, applications and releases.
  5. Erlang.mk provides an interface to analyzing all except
  6. the releases.
  7. Both predefined checks and custom queries are supported
  8. in Erlang.mk.
  9. === Usage
  10. To run Xref with the default predefined checks:
  11. [source,bash]
  12. $ make xref
  13. Erlang.mk will error out when warnings are found.
  14. The following predefined checks can be used:
  15. * `undefined_function_calls`
  16. * `undefined_functions` (similar to the previous)
  17. * `locals_not_used` (detected by the compiler)
  18. * `exports_not_used`
  19. * `deprecated_function_calls` (also detected by the compiler)
  20. * `deprecated_functions` (similar to the previous)
  21. * `{deprecated_function_calls, Flag}`
  22. * `{deprecated_functions, Flag}` (similar to the previous)
  23. Erlang.mk will only run the `undefined_function_calls`
  24. check by default.
  25. To change the check the `XREF_CHECKS` variable can be used:
  26. [source,bash]
  27. $ make xref XREF_CHECKS=exports_not_used
  28. Multiple checks can be run at once. The checks variable
  29. must be defined as an Erlang list:
  30. [source,bash]
  31. $ make xref XREF_CHECKS="[undefined_function_calls, exports_not_used]"
  32. Erlang.mk also supports informational analyses. Those will
  33. not error out when results are found, since they are not
  34. errors. To find all modules that call `cowboy_req` functions:
  35. [source,bash]
  36. $ make xref XREF_CHECKS="{module_use, cowboy_req}"
  37. The following informational checks are supported:
  38. * `{call, MFA}` - functions that MFA calls
  39. * `{use, MFA}` - functions that call MFA
  40. * `{module_call, Mod}` - modules that Mod calls (Mod depends on them)
  41. * `{module_use, Mod}` - modules that call Mod (they depend on Mod)
  42. * `{application_call, App}` - apps that App calls (App depends on them)
  43. * `{application_use, App}` - apps that call App (they depend on App)
  44. The scope might need to be increased in order to obtain
  45. the complete results of informational checks. This is
  46. especially true for module and applications, with
  47. application results being dependent on the applications
  48. being added to the scope to be found.
  49. === Queries
  50. Erlang.mk provides an interface to the Xref query
  51. functions. To perform a query, the `q` variable
  52. must be used instead of `XREF_CHECKS`. For example,
  53. to obtain all unresolved calls:
  54. [source,bash]
  55. $ make xref q=UC
  56. The query language is documented at the top of the
  57. link:https://www.erlang.org/doc/man/xref.html[XRef manual page].
  58. === Analysis scope
  59. Erlang.mk will set the scope of analysis to the current
  60. project by default. The scope can be automatically
  61. extended to the applications from multi-application
  62. repositories, to dependencies and to the built-in
  63. Erlang/OTP applications themselves.
  64. To change the scope, the `XREF_SCOPE` variable can be
  65. set. The variable can either be set in your Makefile
  66. or from the command line. The following values can
  67. be defined:
  68. * `app` - the current project
  69. * `apps` - applications from multi-application repositories
  70. * `deps` - dependencies
  71. * `otp` - Erlang/OTP applications
  72. To get the most complete analysis possible they should
  73. all be added to the variable:
  74. [source,bash]
  75. ----
  76. $ make xref XREF_CHECKS="{application_use, ssl}" XREF_SCOPE="app apps deps otp"
  77. Application ssl is used by:
  78. - my_app
  79. - diameter
  80. - eldap
  81. - ftp
  82. - inets
  83. - ssl
  84. ----
  85. Additional applications can be provided using the
  86. `XREF_EXTRA_APP_DIRS` variable. Note that these
  87. applications will need to be compiled before they
  88. can be found by Xref.
  89. Similarly, non-application directories can be
  90. added using `XREF_EXTRA_DIRS`. The directory
  91. to be provided must be the one that contains
  92. the beam files.
  93. === Ignoring warnings
  94. Sometimes it is necessary to ignore warnings because
  95. they are expected. This is the case for example
  96. when multiple Erlang/OTP versions must be supported
  97. and modules or functions have been added or removed.
  98. Erlang.mk supports both project-wide configuration
  99. and Rebar-compatible inline ignores. To ignore
  100. warnings for a function in the current module the
  101. following line can be added to the source file:
  102. [source,erlang]
  103. ----
  104. -ignore_xref({log, 1}).
  105. ----
  106. The module name can be specified explicitly:
  107. [source,erlang]
  108. ----
  109. -ignore_xref({my_mod, log, 1}).
  110. ----
  111. As well as a full module can be ignored:
  112. [source,erlang]
  113. ----
  114. -ignore_xref(my_mod).
  115. ----
  116. The ignored functions can be provided as a list:
  117. [source,erlang]
  118. ----
  119. -ignore_xref([{log, 1}, {pretty_print, 1}]).
  120. ----
  121. The `XREF_IGNORE` variable can be used to define
  122. functions and modules to ignore project-wide. It
  123. accepts either MFAs or modules:
  124. [source,make]
  125. XREF_IGNORE = {my_mod, log, 1}
  126. Multiple ignores must be provided as an Erlang
  127. list:
  128. [source,make]
  129. XREF_IGNORE = [{my_mod, log, 1}, other_mod]
  130. By default Erlang.mk will ignore unused exports
  131. for behavior callbacks when the `exports_not_used`
  132. check is run. It is possible to override this
  133. behavior, or to ignore the callbacks for queries
  134. and other checks, by defining the `XREF_IGNORE_CALLBACKS`
  135. variable:
  136. [source,bash]
  137. $ make xref XREF_CHECKS=exports_not_used XREF_IGNORE_CALLBACKS=0