xref: /linux/scripts/misc-check (revision 08215f5486ec4d7c39cf14987ffb133e1e1f6c10)
191ecf7ffSMasahiro Yamada#!/bin/sh
291ecf7ffSMasahiro Yamada# SPDX-License-Identifier: GPL-2.0-only
391ecf7ffSMasahiro Yamada
491ecf7ffSMasahiro Yamadaset -e
591ecf7ffSMasahiro Yamada
689e7fecfSMasahiro Yamada# Detect files that are tracked but ignored by git.
791ecf7ffSMasahiro Yamadacheck_tracked_ignored_files () {
83a44052bSMasahiro Yamada	git -C "${srctree:-.}" ls-files -i -c --exclude-per-directory=.gitignore 2>/dev/null |
991ecf7ffSMasahiro Yamada		sed 's/$/: warning: ignored by one of the .gitignore files/' >&2
1091ecf7ffSMasahiro Yamada}
1191ecf7ffSMasahiro Yamada
12a934a57aSMasahiro Yamada# Check for missing #include <linux/export.h>
13a934a57aSMasahiro Yamada#
14a934a57aSMasahiro Yamada# The rule for including <linux/export.h> is very simple:
15a934a57aSMasahiro Yamada# Include <linux/export.h> only when you use EXPORT_SYMBOL(). That's it.
16a934a57aSMasahiro Yamada#
17a934a57aSMasahiro Yamada# However, some headers include <linux/export.h> even though they are completely
18a934a57aSMasahiro Yamada# unrelated to EXPORT_SYMBOL().
19a934a57aSMasahiro Yamada#
20a934a57aSMasahiro Yamada# One example is include/linux/module.h. Please note <linux/module.h> and
21a934a57aSMasahiro Yamada# <linux/export.h> are orthogonal. <linux/module.h> should be included by files
22a934a57aSMasahiro Yamada# that can be compiled as modules. In other words, <linux/module.h> should be
23a934a57aSMasahiro Yamada# included by EXPORT_SYMBOL consumers. In contrast, <linux/export.h> should be
24a934a57aSMasahiro Yamada# included from EXPORT_SYMBOL providers, which may or may not be modular.
25a934a57aSMasahiro Yamada# Hence, include/linux/module.h should *not* include <linux/export.h>.
26a934a57aSMasahiro Yamada#
27a934a57aSMasahiro Yamada# Another example is include/linux/linkage.h, which is completely unrelated to
28a934a57aSMasahiro Yamada# EXPORT_SYMBOL(). Worse, it is included by most C files, which means, most C
29a934a57aSMasahiro Yamada# files end up including <linux/export.h>, even though only some of them
30a934a57aSMasahiro Yamada# actually export symbols. Hence, include/linux/linkage.h should *not* include
31a934a57aSMasahiro Yamada# <linux/export.h>.
32a934a57aSMasahiro Yamada#
33a934a57aSMasahiro Yamada# Before fixing such headers, we must ensure that C files using EXPORT_SYMBOL()
34a934a57aSMasahiro Yamada# include <linux/export.h> directly, since many C files currently rely on
35a934a57aSMasahiro Yamada# <linux/export.h> being included indirectly (likely, via <linux/linkage> etc.).
36a934a57aSMasahiro Yamada#
37a934a57aSMasahiro Yamada# Therefore, this check.
38a934a57aSMasahiro Yamada#
39a934a57aSMasahiro Yamada# The problem is simple - the warned files use EXPORT_SYMBOL(), but do not
40a934a57aSMasahiro Yamada# include <linux/export.h>. Please add #include <linux/export.h> to them.
41a934a57aSMasahiro Yamada#
42a934a57aSMasahiro Yamada# If the included headers are sorted alphabetically, please insert
43a934a57aSMasahiro Yamada# <linux/export.h> in the appropriate position to maintain the sort order.
44a934a57aSMasahiro Yamada# For this reason, this script only checks missing <linux/export.h>, but
45a934a57aSMasahiro Yamada# does not automatically fix it.
46a934a57aSMasahiro Yamadacheck_missing_include_linux_export_h () {
47a934a57aSMasahiro Yamada
48a934a57aSMasahiro Yamada	git -C "${srctree:-.}" grep --files-with-matches -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_GPL_FOR_MODULES)\(.*\)' \
49a934a57aSMasahiro Yamada	    -- '*.[ch]' :^tools/ :^include/linux/export.h |
50a934a57aSMasahiro Yamada	xargs -r git -C "${srctree:-.}" grep --files-without-match '#include[[:space:]]*<linux/export\.h>' |
51a934a57aSMasahiro Yamada	xargs -r printf "%s: warning: EXPORT_SYMBOL() is used, but #include <linux/export.h> is missing\n" >&2
52a934a57aSMasahiro Yamada}
53a934a57aSMasahiro Yamada
547d95680dSMasahiro Yamada# If you do not use EXPORT_SYMBOL(), please do not include <linux/export.h>.
557d95680dSMasahiro Yamada# Currently, this is checked for *.c files, but not for *.h files, because some
567d95680dSMasahiro Yamada# *.c files rely on <linux/export.h> being included indirectly.
577d95680dSMasahiro Yamadacheck_unnecessary_include_linux_export_h () {
587d95680dSMasahiro Yamada
597d95680dSMasahiro Yamada	git -C "${srctree:-.}" grep --files-with-matches '#include[[:space:]]*<linux/export\.h>' \
607d95680dSMasahiro Yamada	    -- '*.[c]' :^tools/ |
617d95680dSMasahiro Yamada	xargs -r git -C "${srctree:-.}" grep --files-without-match -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_GPL_FOR_MODULES)\(.*\)' |
627d95680dSMasahiro Yamada	xargs -r printf "%s: warning: EXPORT_SYMBOL() is not used, but #include <linux/export.h> is present\n" >&2
637d95680dSMasahiro Yamada}
647d95680dSMasahiro Yamada
65*a6a7946bSMasahiro Yamadacase "${KBUILD_EXTRA_WARN}" in
66*a6a7946bSMasahiro Yamada*1*)
6791ecf7ffSMasahiro Yamada	check_tracked_ignored_files
68*a6a7946bSMasahiro Yamada	;;
69*a6a7946bSMasahiro Yamadaesac
70*a6a7946bSMasahiro Yamada
71*a6a7946bSMasahiro Yamadacase "${KBUILD_EXTRA_WARN}" in
72*a6a7946bSMasahiro Yamada*2*)
73a934a57aSMasahiro Yamada	check_missing_include_linux_export_h
747d95680dSMasahiro Yamada	check_unnecessary_include_linux_export_h
75*a6a7946bSMasahiro Yamada	;;
76*a6a7946bSMasahiro Yamadaesac
77