xref: /qemu/scripts/clean-includes (revision fd3e39a40ca2ee26b09a5de3149af8b056b85233)
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
16# Usage:
17#   clean-includes [--git subjectprefix] file ...
18#
19# If the --git subjectprefix option is given, then after making
20# the changes to the files this script will create a git commit
21# with the subject line "subjectprefix: Clean up includes"
22# and a boilerplate commit message.
23
24# This script requires Coccinelle to be installed.
25
26# .c files will have the osdep.h included added, and redundant
27# includes removed.
28# .h files will have redundant includes (including includes of osdep.h)
29# removed.
30# Other files (including C++ and ObjectiveC) can't be handled by this script.
31
32# The following one-liner may be handy for finding files to run this on.
33# However some caution is required regarding files that might be part
34# of the guest agent or standalone tests.
35
36# for i in `git ls-tree --name-only HEAD`  ; do test -f $i && \
37#   grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
38#   echo $i ; done
39
40
41GIT=no
42
43if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
44    if [ $# -eq 1 ]; then
45        echo "--git option requires an argument"
46        exit 1
47    fi
48    GITSUBJ="$2"
49    GIT=yes
50    shift
51    shift
52fi
53
54if [ $# -eq 0 ]; then
55    echo "Usage: clean-includes [--git subjectprefix] foo.c ..."
56    echo "(modifies the files in place)"
57    exit 1
58fi
59
60# Annoyingly coccinelle won't read a scriptfile unless its
61# name ends '.cocci', so write it out to a tempfile with the
62# right kind of name.
63COCCIFILE="$(mktemp --suffix=.cocci)"
64
65trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
66
67cat >"$COCCIFILE" <<EOT
68@@
69@@
70
71(
72+ #include "qemu/osdep.h"
73 #include "..."
74|
75+ #include "qemu/osdep.h"
76 #include <...>
77)
78EOT
79
80
81for f in "$@"; do
82  case "$f" in
83    *.c)
84      MODE=c
85      ;;
86    *include/qemu/osdep.h | \
87    *include/qemu/compiler.h | \
88    *include/config.h | \
89    *include/standard-headers/ )
90      # Removing include lines from osdep.h itself would be counterproductive.
91      echo "SKIPPING $f (special case header)"
92      continue
93      ;;
94    *include/standard-headers/*)
95      echo "SKIPPING $f (autogenerated header)"
96      continue
97      ;;
98    *.h)
99      MODE=h
100      ;;
101    *)
102      echo "WARNING: ignoring $f (cannot handle non-C files)"
103      continue
104      ;;
105  esac
106
107  if [ "$MODE" = "c" ]; then
108    # First, use Coccinelle to add qemu/osdep.h before the first existing include
109    # (this will add two lines if the file uses both "..." and <...> #includes,
110    # but we will remove the extras in the next step)
111    spatch  --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
112
113    # Now remove any duplicate osdep.h includes
114    perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
115  else
116    # Remove includes of osdep.h itself
117    perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
118                            ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
119  fi
120
121  # Remove includes that osdep.h already provides
122  perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
123                          ! (grep { $_ eq $1 } qw (
124           "config-host.h" "qemu/compiler.h" "config.h"
125           <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
126           <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
127           <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
128           <sys/stat.h> <sys/time.h> <assert.h> <signal.h>
129           "glib-compat.h" "qapi/error.h"
130            ))' "$f"
131
132done
133
134if [ "$GIT" = "yes" ]; then
135    git add -- "$@"
136    git commit --signoff -F - <<EOF
137$GITSUBJ: Clean up includes
138
139Clean up includes so that osdep.h is included first and headers
140which it implies are not included manually.
141
142This commit was created with scripts/clean-includes.
143
144EOF
145
146fi
147