2
0

clean-includes 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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|pc-bios|disas/libvixl)'
  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. for f in "$@"; do
  99. case "$f" in
  100. *.inc.c)
  101. # These aren't standalone C source files
  102. echo "SKIPPING $f (not a standalone source file)"
  103. continue
  104. ;;
  105. *.c)
  106. MODE=c
  107. ;;
  108. *include/qemu/osdep.h | \
  109. *include/qemu/compiler.h | \
  110. *include/qemu/qemu-plugin.h | \
  111. *include/glib-compat.h | \
  112. *include/sysemu/os-posix.h | \
  113. *include/sysemu/os-win32.h | \
  114. *include/standard-headers/ )
  115. # Removing include lines from osdep.h itself would be counterproductive.
  116. echo "SKIPPING $f (special case header)"
  117. continue
  118. ;;
  119. *include/standard-headers/*)
  120. echo "SKIPPING $f (autogenerated header)"
  121. continue
  122. ;;
  123. *.h)
  124. MODE=h
  125. ;;
  126. *)
  127. echo "WARNING: ignoring $f (cannot handle non-C files)"
  128. continue
  129. ;;
  130. esac
  131. if [ "$MODE" = "c" ]; then
  132. # First, use Coccinelle to add qemu/osdep.h before the first existing include
  133. # (this will add two lines if the file uses both "..." and <...> #includes,
  134. # but we will remove the extras in the next step)
  135. spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
  136. # Now remove any duplicate osdep.h includes
  137. perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
  138. else
  139. # Remove includes of osdep.h itself
  140. perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
  141. ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
  142. fi
  143. # Remove includes that osdep.h already provides
  144. perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
  145. ! (grep { $_ eq $1 } qw (
  146. "config-host.h" "config-target.h" "qemu/compiler.h"
  147. <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
  148. <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
  149. <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
  150. <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
  151. <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
  152. "sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h"
  153. "qemu/typedefs.h"
  154. ))' "$f"
  155. done
  156. if [ "$DUPHEAD" = "yes" ]; then
  157. egrep "^[[:space:]]*#[[:space:]]*include" "$@" | tr -d '[:blank:]' \
  158. | sort | uniq -c | awk '{if ($1 > 1) print $0}'
  159. if [ $? -eq 0 ]; then
  160. echo "Found duplicate header file includes. Please check the above files manually."
  161. exit 1
  162. fi
  163. fi
  164. if [ "$GIT" = "yes" ]; then
  165. git add -- "$@"
  166. git commit --signoff -F - <<EOF
  167. $GITSUBJ: Clean up includes
  168. Clean up includes so that osdep.h is included first and headers
  169. which it implies are not included manually.
  170. This commit was created with scripts/clean-includes.
  171. EOF
  172. fi