pci core: function pci_bus_init() cleanup
[qemu/ar7.git] / scripts / clean-includes
blob1af1f824b8e140a0faa41642b4eb178e2d96964b
1 #!/bin/sh -e
3 # Clean up QEMU #include lines by ensuring that qemu/osdep.h
4 # is the first include listed.
6 # Copyright (c) 2015 Linaro Limited
8 # Authors:
9 # Peter Maydell <peter.maydell@linaro.org>
11 # This work is licensed under the terms of the GNU GPL, version 2
12 # or (at your option) any later version. See the COPYING file in
13 # the top-level directory.
15 # Usage:
16 # clean-includes [--git subjectprefix] file ...
18 # If the --git subjectprefix option is given, then after making
19 # the changes to the files this script will create a git commit
20 # with the subject line "subjectprefix: Clean up includes"
21 # and a boilerplate commit message.
23 # This script requires Coccinelle to be installed.
26 # The following one-liner may be handy for finding files to run this on.
27 # However some caution is required regarding files that might be part
28 # of the guest agent or standalone tests.
30 # for i in `git ls-tree --name-only HEAD` ; do test -f $i && \
31 # grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
32 # echo $i ; done
35 GIT=no
37 if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
38 if [ $# -eq 1 ]; then
39 echo "--git option requires an argument"
40 exit 1
42 GITSUBJ="$2"
43 GIT=yes
44 shift
45 shift
48 if [ $# -eq 0 ]; then
49 echo "Usage: clean-includes [--git subjectprefix] foo.c ..."
50 echo "(modifies the files in place)"
51 exit 1
54 # Annoyingly coccinelle won't read a scriptfile unless its
55 # name ends '.cocci', so write it out to a tempfile with the
56 # right kind of name.
57 COCCIFILE="$(mktemp --suffix=.cocci)"
59 trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
61 cat >"$COCCIFILE" <<EOT
66 + #include "qemu/osdep.h"
67 #include "..."
69 + #include "qemu/osdep.h"
70 #include <...>
72 EOT
75 for f in "$@"; do
76 # First, use coccinelle to add qemu/osdep.h before the first existing include
77 # (this will add two lines if the file uses both "..." and <...> #includes,
78 # but we will remove the extras in the next step)
79 spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
81 # Now remove any duplicate osdep.h includes
82 perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
84 # Remove includes that osdep.h already provides
85 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
86 ! (grep { $_ eq $1 } qw (
87 "config-host.h" "qemu/compiler.h" "config.h"
88 <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
89 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
90 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
91 <sys/stat.h> <sys/time.h> <assert.h> <signal.h>
92 "glib-compat.h" "qapi/error.h"
93 ))' "$f"
95 done
97 if [ "$GIT" = "yes" ]; then
98 git add -- "$@"
99 git commit --signoff -F - <<EOF
100 $GITSUBJ: Clean up includes
102 Clean up includes so that osdep.h is included first and headers
103 which it implies are not included manually.
105 This commit was created with scripts/clean-includes.