|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +bazel=$(readlink -f /proc/${PPID}/exe) |
| 5 | + |
| 6 | +args=$(printf " union %s" "${@}" | sed 's/^ union \(.*\)/\1/') |
| 7 | +targets="${args:-//...}" |
| 8 | + |
| 9 | +bazel_tidy_config=(\ |
| 10 | +"--aspects=@@WORKSPACE@//clang_tidy:clang_tidy.bzl%clang_tidy_aspect" \ |
| 11 | +"--@@WORKSPACE@//:clang_tidy_executable=@TIDY_BINARY@" \ |
| 12 | +"--@@WORKSPACE@//:clang_tidy_config=@TIDY_CONFIG@" \ |
| 13 | +"--output_groups=report") |
| 14 | + |
| 15 | +cd $BUILD_WORKSPACE_DIRECTORY |
| 16 | + |
| 17 | +exported_fixes=$("$bazel" aquery \ |
| 18 | + "mnemonic(\"ClangTidy\", kind(\"cc_.* rule\", $targets))" \ |
| 19 | + --noshow_progress \ |
| 20 | + --ui_event_filters=-info \ |
| 21 | + "${bazel_tidy_config[@]}" \ |
| 22 | + | grep 'Outputs:' \ |
| 23 | + | sed 's:^\s\+Outputs\: \[\(.*\)\]$:\1:') |
| 24 | + |
| 25 | +"$bazel" build \ |
| 26 | + --noshow_progress \ |
| 27 | + --ui_event_filters=-info,-error,-stdout,-stderr \ |
| 28 | + --keep_going \ |
| 29 | + "${bazel_tidy_config[@]}" \ |
| 30 | + "${@:-//...}" || true |
| 31 | + |
| 32 | +for file in $exported_fixes; do |
| 33 | + # get the build directory which is probably some sandbox |
| 34 | + build_dir=$(grep --max-count=1 'BuildDirectory:' "$file" \ |
| 35 | + | sed "s:\s\+BuildDirectory\:\s\+'\(.*\)':\1:" || true) |
| 36 | + |
| 37 | + # if we didn't find BuildDirectory, it's probably an empty file |
| 38 | + if [ -z "$build_dir" ]; then |
| 39 | + continue |
| 40 | + fi |
| 41 | + |
| 42 | + # relative path of a fix file copied to BUILD_WORKSPACE_DIRECTORY |
| 43 | + suggested_fixes=$(basename "$file") |
| 44 | + |
| 45 | + # strip the build_dir prefix |
| 46 | + # and set BuildDirectory to empty |
| 47 | + # so clang-apply-replacements won't look for it |
| 48 | + sed "s:$build_dir/::" "$file" \ |
| 49 | + | sed "s:$build_dir::" \ |
| 50 | + > "$suggested_fixes" |
| 51 | + |
| 52 | + # resolve symlinks and relative paths |
| 53 | + while path=$(grep --max-count=1 '_virtual_includes\|\./' "$suggested_fixes" \ |
| 54 | + | sed "s:\s\+FilePath\:\s\+'\(.*\)':\1:" || true); do |
| 55 | + if [ -z "$path" ]; then |
| 56 | + break |
| 57 | + fi |
| 58 | + |
| 59 | + sed -i "s:$path:$(readlink -f $path):" "$suggested_fixes" |
| 60 | + done |
| 61 | + |
| 62 | + # remove the original exported fixes, otherwise they are found by |
| 63 | + # clang-apply-replacements |
| 64 | + rm -f "$file" |
| 65 | +done |
| 66 | + |
| 67 | +@APPLY_REPLACEMENTS_BINARY@ -remove-change-desc-files . |
0 commit comments