2
0

clean-includes 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/sh -e
  2. #
  3. # Clean up QEMU #include lines by ensuring that qemu/osdep.h
  4. # is the first include listed in .c files, and no headers provided
  5. # by osdep.h itself are redundantly included in either .c or .h files.
  6. #
  7. # Copyright (c) 2015 Linaro Limited
  8. #
  9. # Authors:
  10. # Peter Maydell <peter.maydell@linaro.org>
  11. #
  12. # This work is licensed under the terms of the GNU GPL, version 2
  13. # or (at your option) any later version. See the COPYING file in
  14. # the top-level directory.
  15. # Usage:
  16. # clean-includes [--git subjectprefix] [--check-dup-head] file ...
  17. # or
  18. # clean-includes [--git subjectprefix] [--check-dup-head] --all
  19. #
  20. # If the --git subjectprefix option is given, then after making
  21. # the changes to the files this script will create a git commit
  22. # with the subject line "subjectprefix: Clean up includes"
  23. # and a boilerplate commit message.
  24. #
  25. # If --check-dup-head is specified, additionally check for duplicate
  26. # header includes.
  27. #
  28. # Using --all will cause clean-includes to run on the whole source
  29. # tree (excluding certain directories which are known not to need
  30. # handling).
  31. # This script requires Coccinelle to be installed.
  32. # .c files will have the osdep.h included added, and redundant
  33. # includes removed.
  34. # .h files will have redundant includes (including includes of osdep.h)
  35. # removed.
  36. # Other files (including C++ and ObjectiveC) can't be handled by this script.
  37. # The following one-liner may be handy for finding files to run this on.
  38. # However some caution is required regarding files that might be part
  39. # of the guest agent or standalone tests.
  40. # for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \
  41. # grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
  42. # echo $i ; done
  43. GIT=no
  44. DUPHEAD=no
  45. # Extended regular expression defining files to ignore when using --all
  46. XDIRREGEX='^(tests/tcg|tests/multiboot|tests/fp|tests/plugin|tests/uefi-test-tools|pc-bios|subprojects|contrib/plugins|tools/ebpf|ebpf/rss.bpf.skeleton.h|linux-user/(mips64|x86_64)/(cpu_loop|signal).c)'
  47. while true
  48. do
  49. case $1 in
  50. "--git")
  51. if [ $# -eq 1 ]; then
  52. echo "--git option requires an argument"
  53. exit 1
  54. fi
  55. GITSUBJ="$2"
  56. GIT=yes
  57. shift
  58. shift
  59. ;;
  60. "--check-dup-head")
  61. DUPHEAD=yes
  62. shift
  63. ;;
  64. "--")
  65. shift
  66. break
  67. ;;
  68. *)
  69. break
  70. ;;
  71. esac
  72. done
  73. if [ $# -eq 0 ]; then
  74. echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]"
  75. echo "(modifies the files in place)"
  76. exit 1
  77. fi
  78. if [ "$1" = "--all" ]; then
  79. # We assume there are no files in the tree with spaces in their name
  80. set -- $(git ls-files '*.[ch]' | grep -E -v "$XDIRREGEX")
  81. fi
  82. # Annoyingly coccinelle won't read a scriptfile unless its
  83. # name ends '.cocci', so write it out to a tempfile with the
  84. # right kind of name.
  85. COCCIFILE="$(mktemp --suffix=.cocci)"
  86. trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
  87. cat >"$COCCIFILE" <<EOT
  88. @@
  89. @@
  90. (
  91. + #include "qemu/osdep.h"
  92. #include "..."
  93. |
  94. + #include "qemu/osdep.h"
  95. #include <...>
  96. )
  97. EOT
  98. files=
  99. for f in "$@"; do
  100. if [ -L "$f" ]; then
  101. echo "SKIPPING $f (symbolic link)"
  102. continue
  103. fi
  104. case "$f" in
  105. *.c.inc)
  106. # These aren't standalone C source files
  107. echo "SKIPPING $f (not a standalone source file)"
  108. continue
  109. ;;
  110. *.c)
  111. MODE=c
  112. ;;
  113. *include/qemu/osdep.h | \
  114. *include/qemu/compiler.h | \
  115. *include/qemu/qemu-plugin.h | \
  116. *include/glib-compat.h | \
  117. *include/system/os-posix.h | \
  118. *include/system/os-win32.h | \
  119. *include/standard-headers/ )
  120. # Removing include lines from osdep.h itself would be counterproductive.
  121. echo "SKIPPING $f (special case header)"
  122. continue
  123. ;;
  124. *include/standard-headers/*)
  125. echo "SKIPPING $f (autogenerated header)"
  126. continue
  127. ;;
  128. *.h)
  129. MODE=h
  130. ;;
  131. *)
  132. echo "WARNING: ignoring $f (cannot handle non-C files)"
  133. continue
  134. ;;
  135. esac
  136. files="$files $f"
  137. if [ "$MODE" = "c" ]; then
  138. # First, use Coccinelle to add qemu/osdep.h before the first existing include
  139. # (this will add two lines if the file uses both "..." and <...> #includes,
  140. # but we will remove the extras in the next step)
  141. spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
  142. # Now remove any duplicate osdep.h includes
  143. perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
  144. else
  145. # Remove includes of osdep.h itself
  146. perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
  147. ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
  148. fi
  149. # Remove includes that osdep.h already provides
  150. perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
  151. ! (grep { $_ eq $1 } qw (
  152. "config-host.h" "config-target.h" "qemu/compiler.h"
  153. <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
  154. <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
  155. <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
  156. <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
  157. <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
  158. "system/os-posix.h, system/os-win32.h "glib-compat.h"
  159. "qemu/typedefs.h"
  160. ))' "$f"
  161. done
  162. if [ "$DUPHEAD" = "yes" ] && [ -n "$files" ]; then
  163. if egrep "^[[:space:]]*#[[:space:]]*include" $files | tr -d '[:blank:]' \
  164. | sort | uniq -c | grep -v '^ *1 '; then
  165. echo "Found duplicate header file includes. Please check the above files manually."
  166. exit 1
  167. fi
  168. fi
  169. if [ "$GIT" = "yes" ]; then
  170. git add -- $files
  171. git commit --signoff -F - <<EOF
  172. $GITSUBJ: Clean up includes
  173. This commit was created with scripts/clean-includes.
  174. All .c should include qemu/osdep.h first. The script performs three
  175. related cleanups:
  176. * Ensure .c files include qemu/osdep.h first.
  177. * Including it in a .h is redundant, since the .c already includes
  178. it. Drop such inclusions.
  179. * Likewise, including headers qemu/osdep.h includes is redundant.
  180. Drop these, too.
  181. EOF
  182. fi