121 lines · bash
1#!/bin/bash -u2 3if [[ $# -lt 2 || $# -gt 4 ]]; then4 echo "Usage: $0 <build dir> <file or dir path> [rejects dir] [checks]"5 echo " - <build dir> has to be a LLVM build directory (you should use CCACHE!)."6 echo " - <file or dir path> is the path that contains the .cpp files to update."7 echo " - [rejects dir] is a directory where rejected patch (build failure) will be stored."8 echo " - [checks] is an optional space-separated list of check to use instead of auto-detecting"9 echo " Also define the env var CLANG_TIDY the path to use for the clang-tidy binary (default to 'clang-tidy' in the PATH)"10 echo " Also define the env var TIMING_TIDY to 'time' to prefix clang-tidy execution with it"11 echo ""12 echo "This tool will execute clang-tidy on every .cpp file in the provided path and"13 echo "rerun the tests. On success, a commit is added to the repo for each individual"14 echo "pair <clang-tidy check, file>."15 exit 116fi17BUILD_DIR=$118SRCS=$219REJECT_DIR=${3:-}20PRESET_CHECKS=${4:-}21SRC_DIR=$PWD22if [[ -v CLANG_TIDY ]] && [[ ! -z "$CLANG_TIDY" ]] ; then23 CLANG_TIDY=$(realpath $CLANG_TIDY)24 if [[ ! -f "$CLANG_TIDY" ]]; then25 echo "Invalid path '$CLANG_TIDY'"26 exit 127 fi28else29 CLANG_TIDY=clang-tidy30fi31TIMING_TIDY=${TIMING_TIDY:-}32echo "Using: '$CLANG_TIDY"33 34if [[ ! -z "$REJECT_DIR" ]] && [[ ! -d "$REJECT_DIR" ]]; then35 echo "Expects 'rejects dir' to be a directory, got '$REJECT_DIR'"36 exit 137fi38 39ensure_clean_build() {40 git reset --hard HEAD41 time ninja -C $BUILD_DIR check-mlir-build-only > ${REJECT_DIR}/ninja.clean.log 2>&142 if [[ $? != 0 ]] ; then43 echo "-- Build failed on clean state, cleaning TableGen files and retry"44 # Reinitialize the TableGen generated file to have a clean state45 find $BUILD_DIR/tools/mlir/ | grep '\.inc' | while read file ; do rm $file ; done46 time ninja -C $BUILD_DIR check-mlir-build-only > ${REJECT_DIR}/ninja.clean.log 2>&147 if [[ $? != 0 ]] ; then48 echo "check-mlir-build-only failed on clean state! (see ninja.clean.log)"49 git status50 exit 151 fi52 fi53}54 55tmpfile=$(mktemp /tmp/mhlo-temp-checks.XXXXXX)56find $SRCS | grep ".cpp$" | sort | while read file ; do57 echo "================================"58 echo "======= Processing $file ======="59 date60 echo "================================"61 CHECKS=62 if [[ ! -z "$PRESET_CHECKS" ]]; then63 CHECKS="$PRESET_CHECKS"64 else65 CHECKS=$($CLANG_TIDY $file -p $BUILD_DIR --list-checks \66 | grep -v "Enabled checks:" | grep -v "^$" \67 | while read check ; do echo -n "${check} " ; done;)68 fi69 echo "-----------------------------------"70 echo "-- Reset state before applying all checks on file $file"71 ensure_clean_build72 73 echo "-----------------------------------"74 echo "-- Apply all checks on file $file"75 echo "$TIMING_TIDY $CLANG_TIDY -p $BUILD_DIR $file -fix -fix-errors"76 $TIMING_TIDY $CLANG_TIDY -p $BUILD_DIR $file -fix -fix-errors \77 | grep "warning:.*\]$" | sed -r 's#.*\[(.*)]$#\1#' | sort -u > $tmpfile78 git clang-format -f79 if [[ $(git diff --stat) == '' ]]; then80 echo 'Nothing was applied, skip'81 continue82 fi83 echo "-----------------------------------"84 echo "-- Got some diff, run one check at a time now"85 cat $tmpfile | while read check ; do86 echo "-----------------------------------"87 echo "-- Reset state before applying check $check on file $file"88 ensure_clean_build89 90 echo "-----------------------------------"91 echo "-- Apply check $check on file $file"92 COMMAND=$(echo "$TIMING_TIDY $CLANG_TIDY -p $BUILD_DIR $file --checks="-*,$check" -fix -fix-errors")93 echo $COMMAND94 { $TIMING_TIDY $CLANG_TIDY -p $BUILD_DIR $file --checks="-*,$check" -fix -fix-errors ; } 2>&195 git clang-format -f96 if [[ $(git diff --stat) == '' ]]; then97 echo 'Nothing was applied, skip'98 continue99 fi100 echo "-----------------------------------"101 echo "-- Test check $check on file $file"102 # Clang-tidy sometimes update files in the build directory, erase the .inc file generate by tablegen103 # to force them to be regenerated now.104 find $BUILD_DIR/tools/mlir/ | grep '\.inc' | while read file ; do rm $file ; done105 echo $COMMAND > ${REJECT_DIR}/ninja.${check}.$(basename $file).log106 ninja -C $BUILD_DIR --quiet check-mlir >> ${REJECT_DIR}/${check}.$(basename $file).ninja.log 2>&1107 if [[ $? != 0 ]] ; then108 echo "check-mlir failed! (see ninja.${check}.${file}.log)"109 [[ ! -z "$REJECT_DIR" ]] && git diff > "${REJECT_DIR}/${check}_$(basename ${file}).reject.diff"110 continue111 fi112 rm -f ${REJECT_DIR}/ninja.${check}.$(basename $file).log113 114 echo "-----------------------------------"115 echo "-- Success, commit changes for check $check on file $file"116 git clang-format -f117 118 git commit -a -m "[MLIR] Apply clang-tidy fixes for $check in $(basename $file) (NFC)"119 done120done121