• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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    local output
31
32    _log "${green}[ RUN      ]${reset} ${label}"
33    output="$(eval "$cmd" 2>&1)"
34    if [[ $? -eq 0 ]]; then
35        _log "${green}[       OK ]${reset} ${label}"
36        return 0
37    else
38        echo "${output}"
39        _log "${red}[  FAILED  ]${reset} ${label}"
40        errors=$((errors + 1))
41        return 1
42    fi
43}
44
45function _clang_format()
46{
47    local path
48    local errors=0
49
50    for path in $cpp_files; do
51        local output="$(clang-format -style=file "$path" | diff $path -)"
52        if [[ "$output" ]]; then
53            echo "$path"
54            echo "$output"
55            errors=1
56        fi
57    done
58    return $errors
59}
60
61function _bpfmt()
62{
63    local output="$(bpfmt -d $bp_files)"
64    if [[ "$output" ]]; then
65        echo "$output"
66        return 1
67    fi
68    return 0
69}
70
71function _cpplint()
72{
73    local cpplint="${ANDROID_BUILD_TOP}/tools/repohooks/tools/cpplint.py"
74    local output="$($cpplint --quiet $cpp_files 2>&1 >/dev/null | grep -v \
75        -e 'Found C system header after C++ system header.' \
76        -e 'Unknown NOLINT error category: cert-dcl50-cpp' \
77        -e 'Unknown NOLINT error category: misc-non-private-member-variables-in-classes' \
78        -e 'Unknown NOLINT error category: performance-unnecessary-copy-initialization' \
79    )"
80    if [[ "$output" ]]; then
81        echo "$output"
82        return 1
83    fi
84    return 0
85}
86
87function _parse_args()
88{
89    local opts
90
91    opts="$(getopt -o cfh --long check,fix,help -- "$@")"
92    if [[ $? -ne 0 ]]; then
93        exit 1
94    fi
95    eval set -- "$opts"
96    while true; do
97        case "$1" in
98            -c|--check) opt_mode="check"; shift ;;
99            -f|--fix) opt_mode="fix"; shift ;;
100            -h|--help) opt_mode="help"; shift ;;
101            *) break ;;
102        esac
103    done
104}
105
106errors=0
107script="$(readlink -f "$BASH_SOURCE")"
108prefix="$(dirname "$script")"
109cpp_files="$(find "$prefix" -name '*.cpp' -or -name '*.h')"
110bp_files="$(find "$prefix" -name 'Android.bp')"
111opt_mode="check"
112
113_parse_args "$@"
114if [[ $opt_mode == "check" ]]; then
115    _eval "clang-format" "_clang_format"
116    _eval "bpfmt" "_bpfmt"
117    _eval "cpplint" "_cpplint"
118    exit $errors
119elif [[ $opt_mode == "fix" ]]; then
120    clang-format -style=file -i $cpp_files
121    bpfmt -w $bp_files
122    exit 0
123elif [[ $opt_mode == "help" ]]; then
124    echo "Run static analysis tools such as clang-format and cpplint on the idmap2"
125    echo "module. Optionally fix some of the issues found (--fix). Intended to be run"
126    echo "before merging any changes."
127    echo
128    echo "usage: $(basename $script) [--check|--fix|--help]"
129    exit 0
130else
131    exit 1
132fi
133