Modify apply-iwyu to utilize compile_commands.json

Bug: webrtc:13532
Change-Id: I06e5987469e732296deb1eefd317e2478add98b8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/246040
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: (Daniel.L) Byoungchan Lee <daniel.l@hpcnt.com>
Cr-Commit-Position: refs/heads/main@{#35674}
This commit is contained in:
Byoungchan Lee
2022-01-13 01:04:58 +09:00
committed by WebRTC LUCI CQ
parent 5e05369cb2
commit e286a7bbf7

View File

@ -6,14 +6,43 @@
# The script uses a subsequent grep pass to remove #include files from .cc # The script uses a subsequent grep pass to remove #include files from .cc
# that are also in the .h file, or are problematic to include. # that are also in the .h file, or are problematic to include.
# #
# In order to handle include paths correctly, you need to provide
# a compile DB (aka compile_commands.json).
# You can create it in one of the following ways:
# "gn gen --export-compile-commands path/to/out"
# "tools/clang/scripts/generate_compdb.py -p path/to/out > compile_commands.json"
#
# To get iwyu on Debian/glinux, do "sudo apt-get install iwyu". # To get iwyu on Debian/glinux, do "sudo apt-get install iwyu".
set -e set -e
set -x set -x
FILE=$1
IWYU_TOOL="${IWYU_TOOL:-/usr/bin/iwyu_tool}"
FIX_INCLUDE="${FIX_INCLUDE:-/usr/bin/fix_include}"
# If you want to exclude files that are in $FILE.h from $FILE.cc, set # If you want to exclude files that are in $FILE.h from $FILE.cc, set
# the following variable to "yes". This is a style guide violation. # the following variable to "yes". This is a style guide violation.
REMOVE_CC_INCLUDES=no REMOVE_CC_INCLUDES=no
COMPILE_COMMANDS=''
error() {
echo "$*" >&2
exit 1
}
while getopts 'c:r' opts; do
case "${opts}" in
c) COMPILE_COMMANDS="${OPTARG}" ;;
r) REMOVE_CC_INCLUDES=yes ;;
*) error "Unexpected option ${opts}" ;;
esac
done
shift $(expr $OPTIND - 1 )
if [[ -z "$COMPILE_COMMANDS" ]]; then
error "compile_commands.json must be passed."
fi
FILE="$1"
if [ ! -f $FILE ]; then if [ ! -f $FILE ]; then
# See if we have the root name of a .cc/.h pair # See if we have the root name of a .cc/.h pair
@ -34,11 +63,8 @@ else
fi fi
# IWYU has a confusing set of exit codes. Discard it. # IWYU has a confusing set of exit codes. Discard it.
iwyu -Xiwyu --no_fwd_decls -D__X86_64__ -DWEBRTC_POSIX -I . \ "$IWYU_TOOL" -p "$COMPILE_COMMANDS" "$FILE_CC" \
-I third_party/abseil-cpp \ >& /tmp/includefixes$$ || echo "IWYU done, code $?"
-I third_party/googletest/src/googlemock/include \
-I third_party/googletest/src/googletest/include \
$FILE_CC >& /tmp/includefixes$$ || echo "IWYU done, code $?"
if grep 'fatal error' /tmp/includefixes$$; then if grep 'fatal error' /tmp/includefixes$$; then
echo "iwyu run failed" echo "iwyu run failed"
@ -46,7 +72,11 @@ if grep 'fatal error' /tmp/includefixes$$; then
rm /tmp/includefixes$$ rm /tmp/includefixes$$
exit 1 exit 1
else else
fix_include < /tmp/includefixes$$ || echo "Some files modified" # In compile_commands.json, the file name is recorded
# as a relative path to the build directory.
pushd "$(dirname "$COMPILE_COMMANDS")" || error "pushd failed"
"$FIX_INCLUDE" < /tmp/includefixes$$ || echo "Some files modified"
popd
rm /tmp/includefixes$$ rm /tmp/includefixes$$
fi fi
@ -70,5 +100,3 @@ if [ -n "$FILE_H" ]; then
fi fi
echo "Finished. Check diff, compile and git cl format before uploading." echo "Finished. Check diff, compile and git cl format before uploading."