misc-check 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. set -e
  4. # Detect files that are tracked but ignored by git.
  5. check_tracked_ignored_files () {
  6. git -C "${srctree:-.}" ls-files -i -c --exclude-per-directory=.gitignore 2>/dev/null |
  7. sed 's/$/: warning: ignored by one of the .gitignore files/' >&2
  8. }
  9. # Check for missing #include <linux/export.h>
  10. #
  11. # The rule for including <linux/export.h> is very simple:
  12. # Include <linux/export.h> only when you use EXPORT_SYMBOL(). That's it.
  13. #
  14. # However, some headers include <linux/export.h> even though they are completely
  15. # unrelated to EXPORT_SYMBOL().
  16. #
  17. # One example is include/linux/module.h. Please note <linux/module.h> and
  18. # <linux/export.h> are orthogonal. <linux/module.h> should be included by files
  19. # that can be compiled as modules. In other words, <linux/module.h> should be
  20. # included by EXPORT_SYMBOL consumers. In contrast, <linux/export.h> should be
  21. # included from EXPORT_SYMBOL providers, which may or may not be modular.
  22. # Hence, include/linux/module.h should *not* include <linux/export.h>.
  23. #
  24. # Another example is include/linux/linkage.h, which is completely unrelated to
  25. # EXPORT_SYMBOL(). Worse, it is included by most C files, which means, most C
  26. # files end up including <linux/export.h>, even though only some of them
  27. # actually export symbols. Hence, include/linux/linkage.h should *not* include
  28. # <linux/export.h>.
  29. #
  30. # Before fixing such headers, we must ensure that C files using EXPORT_SYMBOL()
  31. # include <linux/export.h> directly, since many C files currently rely on
  32. # <linux/export.h> being included indirectly (likely, via <linux/linkage> etc.).
  33. #
  34. # Therefore, this check.
  35. #
  36. # The problem is simple - the warned files use EXPORT_SYMBOL(), but do not
  37. # include <linux/export.h>. Please add #include <linux/export.h> to them.
  38. #
  39. # If the included headers are sorted alphabetically, please insert
  40. # <linux/export.h> in the appropriate position to maintain the sort order.
  41. # For this reason, this script only checks missing <linux/export.h>, but
  42. # does not automatically fix it.
  43. check_missing_include_linux_export_h () {
  44. git -C "${srctree:-.}" grep --files-with-matches -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_FOR_MODULES)\(.*\)' \
  45. -- '*.[ch]' :^tools/ :^include/linux/export.h |
  46. xargs -r git -C "${srctree:-.}" grep --files-without-match '#include[[:space:]]*<linux/export\.h>' |
  47. xargs -r printf "%s: warning: EXPORT_SYMBOL() is used, but #include <linux/export.h> is missing\n" >&2
  48. }
  49. # If you do not use EXPORT_SYMBOL(), please do not include <linux/export.h>.
  50. # Currently, this is checked for *.c files, but not for *.h files, because some
  51. # *.c files rely on <linux/export.h> being included indirectly.
  52. check_unnecessary_include_linux_export_h () {
  53. git -C "${srctree:-.}" grep --files-with-matches '#include[[:space:]]*<linux/export\.h>' \
  54. -- '*.[c]' :^tools/ |
  55. xargs -r git -C "${srctree:-.}" grep --files-without-match -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_FOR_MODULES)\(.*\)' |
  56. xargs -r printf "%s: warning: EXPORT_SYMBOL() is not used, but #include <linux/export.h> is present\n" >&2
  57. }
  58. case "${KBUILD_EXTRA_WARN}" in
  59. *1*)
  60. check_tracked_ignored_files
  61. ;;
  62. esac
  63. case "${KBUILD_EXTRA_WARN}" in
  64. *2*)
  65. check_missing_include_linux_export_h
  66. check_unnecessary_include_linux_export_h
  67. ;;
  68. esac