#!/bin/bash
set -e
set -u
OUT_DIR="out/rewrite"
if [ "$1" != "" ]
then
OUT_DIR="$1"
fi
COMPILE_DIRS=.
EDIT_DIRS=.
mv third_party/llvm-build third_party/llvm-build-upstream
echo "*** Building the rewriter ***"
time tools/clang/scripts/build.py \
--without-android \
--without-fuchsia \
--extra-tools rewrite_raw_ptr_fields
tools/clang/rewrite_raw_ptr_fields/tests/run_all_tests.py
echo "*** Preparing targets ***"
gn gen $OUT_DIR
GEN_H_TARGETS=`ninja -C $OUT_DIR -t targets all | grep '^gen/.*\(\.h\|inc\|css_tokenizer_codepoints.cc\)' | cut -d : -f 1`
time ninja -C $OUT_DIR $GEN_H_TARGETS
if grep -qE '^\s*target_os\s*=\s*("win"|win)' $OUT_DIR/args.gn
then
TARGET_OS_OPTION="--target_os=win"
fi
echo "*** Generating the ignore list ***"
time tools/clang/scripts/run_tool.py \
$TARGET_OS_OPTION \
--tool rewrite_raw_ptr_fields \
--generate-compdb \
-p $OUT_DIR \
$COMPILE_DIRS > ~/scratch/rewriter.out
cat ~/scratch/rewriter.out \
| sed '/^==== BEGIN FIELD FILTERS ====$/,/^==== END FIELD FILTERS ====$/{//!b};d' \
| sort | uniq > ~/scratch/automated-fields-to-ignore.txt
cat ~/scratch/automated-fields-to-ignore.txt \
tools/clang/rewrite_raw_ptr_fields/manual-fields-to-ignore.txt \
| grep -v "base::FileDescriptorWatcher::Controller::watcher_" \
> ~/scratch/combined-fields-to-ignore.txt
echo "*** Running the main rewrite phase ***"
time tools/clang/scripts/run_tool.py \
$TARGET_OS_OPTION \
--tool rewrite_raw_ptr_fields \
--tool-arg=--exclude-fields=$HOME/scratch/combined-fields-to-ignore.txt \
-p $OUT_DIR \
$COMPILE_DIRS > ~/scratch/rewriter.main.out
echo "*** Applying edits ***"
cat ~/scratch/rewriter.main.out | \
tools/clang/scripts/extract_edits.py | \
tools/clang/scripts/apply_edits.py -p $OUT_DIR $EDIT_DIRS
echo "*** Formatting ***"
time git cl format
rm -r -f third_party/llvm-build
mv third_party/llvm-build-upstream third_party/llvm-build