summaryrefslogtreecommitdiff
path: root/tools/run_iwyu.sh
blob: 2b7b87be4d539dc0b8892c81240d54d39610c49b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/sh
# SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
# SPDX-FileCopyrightText: 2019 Philip Chimento <philip.chimento@gmail.com>

SRCDIR=$(pwd)

if [ "$1" = '--help' -o "$1" = '-h' ]; then
    echo "usage: $0 [ COMMIT ]"
    echo "Run include-what-you-use on the GJS codebase."
    echo "If COMMIT is given, analyze only the files changed since that commit,"
    echo "including uncommitted changes."
    echo "Otherwise, analyze all files."
    exit 0
fi

if [ $# -eq 0 ]; then
    files=all
else
    # make stat changes not show up as modifications
    git update-index -q --really-refresh

    files="$(git diff-tree --name-only -r $1..) $(git diff-index --name-only HEAD)"
fi

should_analyze () {
    file=$(realpath --relative-to=$SRCDIR $1)
    case "$files" in
        all) return 0 ;;
        *$file*) return 0 ;;
        *${file%.cpp}.h*) return 0 ;;
        *${file%.cpp}-inl.h*) return 0 ;;
        *${file%.cpp}-private.h*) return 0 ;;
        *${file%.c}.h*) return 0 ;;
        *) return 1 ;;
    esac
}

cd ${BUILDDIR:-_build}
if ! ninja -t compdb > compile_commands.json; then
    echo 'Generating compile_commands.json failed.'
    exit 1
fi

echo "files: $files"

IWYU="python3 $(which iwyu_tool || which iwyu-tool || which iwyu_tool.py) -p ."
IWYU_TOOL_ARGS="-I../gjs"
IWYU_ARGS="-Wno-pragma-once-outside-header"
IWYU_RAW="include-what-you-use -xc++ -std=c++17 -Xiwyu --keep=config.h $IWYU_ARGS"
IWYU_RAW_INC="-I. -I.. $(pkg-config --cflags gobject-introspection-1.0 mozjs-102)"
PRIVATE_MAPPING="-Xiwyu --mapping_file=$SRCDIR/tools/gjs-private-iwyu.imp -Xiwyu --keep=config.h"
PUBLIC_MAPPING="-Xiwyu --mapping_file=$SRCDIR/tools/gjs-public-iwyu.imp"
POSTPROCESS="python3 $SRCDIR/tools/process_iwyu.py"
EXIT=0

# inline-only files with no implementation file don't appear in the compilation
# database, so iwyu_tool cannot process them
for FILE in $SRCDIR/gi/arg-types-inl.h $SRCDIR/gi/js-value-inl.h \
    $SRCDIR/gi/utils-inl.h $SRCDIR/gjs/enum-utils.h \
    $SRCDIR/gjs/jsapi-util-args.h $SRCDIR/gjs/jsapi-util-root.h \
    $SRCDIR/modules/cairo-module.h
do
    if should_analyze $FILE; then
        if ! $IWYU_RAW $(realpath --relative-to=. $FILE) $IWYU_RAW_INC 2>&1 \
            | $POSTPROCESS; then
            EXIT=1
        fi
    fi
done

for FILE in $SRCDIR/gi/*.cpp $SRCDIR/gjs/atoms.cpp $SRCDIR/gjs/byteArray.cpp \
    $SRCDIR/gjs/coverage.cpp $SRCDIR/gjs/debugger.cpp \
    $SRCDIR/gjs/deprecation.cpp $SRCDIR/gjs/engine.cpp \
    $SRCDIR/gjs/error-types.cpp $SRCDIR/gjs/global.cpp \
    $SRCDIR/gjs/internal.cpp $SRCDIR/gjs/importer.cpp \
    $SRCDIR/gjs/jsapi-util*.cpp $SRCDIR/gjs/mainloop.cpp \
    $SRCDIR/gjs/module.cpp $SRCDIR/gjs/native.cpp \
    $SRCDIR/gjs/objectbox.cpp $SRCDIR/gjs/promise.cpp $SRCDIR/gjs/stack.cpp \
    $SRCDIR/gjs/text-encoding.cpp $SRCDIR/modules/cairo-*.cpp \
    $SRCDIR/modules/console.cpp $SRCDIR/modules/print.cpp \
    $SRCDIR/modules/system.cpp $SRCDIR/test/*.cpp $SRCDIR/util/*.cpp \
    $SRCDIR/libgjs-private/*.c
do
    if should_analyze $FILE; then
        if ! $IWYU $FILE -- $PRIVATE_MAPPING $IWYU_TOOL_ARGS | $POSTPROCESS; then
            EXIT=1
        fi
    fi
done

# this header file is named differently from its corresponding implementation
if ( should_analyze $SRCDIR/gjs/jsapi-dynamic-class.cpp || \
    should_analyze $SRCDIR/gjs/jsapi-class.h ); then
    if ! $IWYU $SRCDIR/gjs/jsapi-dynamic-class.cpp -- $PRIVATE_MAPPING \
        $IWYU_TOOL_ARGS \
        -Xiwyu --check_also=*/gjs/jsapi-class.h | $POSTPROCESS; then
        EXIT=1
    fi
fi

# include header files with private implementation along with their main files
for STEM in gjs/context gjs/mem gjs/profiler modules/cairo; do
    if should_analyze $SRCDIR/$STEM.cpp; then
        if ! $IWYU $SRCDIR/$STEM.cpp -- $PRIVATE_MAPPING $IWYU_TOOL_ARGS \
            -Xiwyu --check_also=*/$STEM-private.h | $POSTPROCESS; then
            EXIT=1
        fi
    fi
done

for FILE in $SRCDIR/gjs/console.cpp $SRCDIR/installed-tests/minijasmine.cpp
do
    if should_analyze $FILE; then
        if ! $IWYU $FILE -- $PUBLIC_MAPPING $IWYU_TOOL_ARGS | $POSTPROCESS; then
            EXIT=1
        fi
    fi
done

if test $EXIT -eq 0; then echo "No changes needed."; fi
exit $EXIT