generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathclang_tidy_wrapper.bash
executable file
·84 lines (79 loc) · 2.48 KB
/
clang_tidy_wrapper.bash
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
#!/usr/bin/env bash
# This is a wrapper for clang-tidy which gives us control over error handling
# Usage: clang_tidy_wrapper.bash <clang-tidy-path> <file1> <file2> ... -- <compiler-args>
#
# Controls:
# - CLANG_TIDY__VERBOSE: If set, be verbose
# - CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE: If set, write stdout and stderr to this file
# - CLANG_TIDY__EXIT_CODE_OUTPUT_FILE: If set, write the highest exit code
# to this file and return success
# First arg is clang-tidy path
clang_tidy=$1
shift
if [[ -n $CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE ]]; then
# Create the file if it doesn't exist
touch $CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE
# Clear the file if it does exist
> $CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE
if [[ -n $CLANG_TIDY__VERBOSE ]]; then
echo "Output > ${CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE}"
fi
fi
if [[ -n $CLANG_TIDY__EXIT_CODE_OUTPUT_FILE ]]; then
if [[ -n $CLANG_TIDY__VERBOSE ]]; then
echo "Exit Code -> ${CLANG_TIDY__EXIT_CODE_OUTPUT_FILE}"
fi
fi
if [[ -n $CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE ]]; then
out_file=$CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE
else
out_file=$(mktemp)
fi
# include stderr in output file; it contains some of the diagnostics
command="$clang_tidy $@ $file > $out_file 2>&1"
if [[ -n $CLANG_TIDY__VERBOSE ]]; then
echo "$@"
echo "cwd: " `pwd`
echo $command
fi
eval $command
exit_code=$?
if [[ -z $CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE ]]; then
cat $out_file
fi
# distinguish between compile (fatal) errors and warnings-as-errors errors
fatal_error=0
if [ $exit_code -ne 0 ] && [ -s $out_file ]; then
while read line
do
if [[ $line == *"clang-diagnostic-error"* ]]; then
fatal_error=1
break
fi
done < "$out_file"
fi
if [ $fatal_error -ne 0 ]; then
cat $out_file
rm $out_file
if [[ -n $CLANG_TIDY__VERBOSE ]]; then
echo "found clang-diagnostic-error (regarding as fatal)"
echo "exit $exit_code"
fi
exit $exit_code
fi
if [[ -z $CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE ]]; then
rm $out_file
fi
# if CLANG_TIDY__EXIT_CODE_FILE is set, write the max exit code to that file and return success
if [[ -n $CLANG_TIDY__EXIT_CODE_OUTPUT_FILE ]]; then
if [[ -n $CLANG_TIDY__VERBOSE ]]; then
echo "echo $exit_code > $CLANG_TIDY__EXIT_CODE_OUTPUT_FILE"
echo "exit 0"
fi
echo $exit_code > $CLANG_TIDY__EXIT_CODE_OUTPUT_FILE
exit 0
fi
if [[ -n $CLANG_TIDY__VERBOSE ]]; then
echo exit $exit_code
fi
exit $exit_code