blob: 57407102843b701138262c713018ef5ca87d1f4e [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001#!/bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18function _log()
19{
20 echo -e "$*" >&2
21}
22
23function _eval()
24{
25 local label="$1"
26 local cmd="$2"
27 local red="\e[31m"
28 local green="\e[32m"
29 local reset="\e[0m"
30
31 _log "${green}[ RUN ]${reset} ${label}"
32 local output="$(eval "$cmd")"
33 if [[ -z "${output}" ]]; then
34 _log "${green}[ OK ]${reset} ${label}"
35 return 0
36 else
37 echo "${output}"
38 _log "${red}[ FAILED ]${reset} ${label}"
39 errors=$((errors + 1))
40 return 1
41 fi
42}
43
44function _clang_format()
45{
46 local path
47 local errors=0
48
49 for path in $cpp_files; do
50 local output="$(clang-format -style=file "$path" | diff $path -)"
51 if [[ "$output" ]]; then
52 echo "$path"
53 echo "$output"
54 errors=1
55 fi
56 done
57 return $errors
58}
59
60function _bpfmt()
61{
62 local output="$(bpfmt -s -d $bp_files)"
63 if [[ "$output" ]]; then
64 echo "$output"
65 return 1
66 fi
67 return 0
68}
69
70function _cpplint()
71{
72 local cpplint="${ANDROID_BUILD_TOP}/tools/repohooks/tools/cpplint.py"
Ryan Mitchellebc0b6a2018-12-18 17:54:36 -080073 local output="$($cpplint --quiet $cpp_files 2>&1 >/dev/null | grep -v \
74 -e 'Found C system header after C++ system header.' \
75 -e 'Unknown NOLINT error category: misc-non-private-member-variables-in-classes' \
Mårten Kongstad1e99b172019-01-28 08:49:12 +010076 -e 'Unknown NOLINT error category: performance-unnecessary-copy-initialization' \
Ryan Mitchellebc0b6a2018-12-18 17:54:36 -080077 )"
78 if [[ "$output" ]]; then
79 echo "$output"
80 return 1
81 fi
82 return 0
Mårten Kongstad02751232018-04-27 13:16:32 +020083}
84
85function _parse_args()
86{
87 local opts
88
89 opts="$(getopt -o cfh --long check,fix,help -- "$@")"
90 if [[ $? -ne 0 ]]; then
91 exit 1
92 fi
93 eval set -- "$opts"
94 while true; do
95 case "$1" in
96 -c|--check) opt_mode="check"; shift ;;
97 -f|--fix) opt_mode="fix"; shift ;;
98 -h|--help) opt_mode="help"; shift ;;
99 *) break ;;
100 esac
101 done
102}
103
104errors=0
105script="$(readlink -f "$BASH_SOURCE")"
106prefix="$(dirname "$script")"
107cpp_files="$(find "$prefix" -name '*.cpp' -or -name '*.h')"
108bp_files="$(find "$prefix" -name 'Android.bp')"
109opt_mode="check"
110
111_parse_args "$@"
112if [[ $opt_mode == "check" ]]; then
113 _eval "clang-format" "_clang_format"
114 _eval "bpfmt" "_bpfmt"
115 _eval "cpplint" "_cpplint"
116 exit $errors
117elif [[ $opt_mode == "fix" ]]; then
118 clang-format -style=file -i $cpp_files
119 bpfmt -s -w $bp_files
120 exit 0
121elif [[ $opt_mode == "help" ]]; then
122 echo "Run static analysis tools such as clang-format and cpplint on the idmap2"
123 echo "module. Optionally fix some of the issues found (--fix). Intended to be run"
124 echo "before merging any changes."
125 echo
126 echo "usage: $(basename $script) [--check|--fix|--help]"
127 exit 0
128else
129 exit 1
130fi