Add clang-tidy script
[gromacs.git] / admin / clang-tidy.sh
blob7e75a98fcea1ce2ec77a9e09fd0410d6aeb15d94
1 #!/bin/bash
3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
36 # This script runs clang tidy checks on modified files and
37 # reports/applies the necessary changes.
39 # See `clang-tidy.sh -h` for a brief usage, and docs/dev-manual/code-formatting.rst
40 # for more details.
42 # Parse command-line arguments
43 function usage() {
44 echo "usage: clang-tidy.sh [-f|--force] [--parallel=#Jobs] [--rev=REV]"
45 echo " [--format=(off|check)]"
46 echo " [--warnings=<file>] [<action>]"
47 echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
50 action="check-workdir"
51 declare -a diffargs
52 baserev="HEAD"
53 force=
54 tidy_mode=check
55 warning_file=
56 concurrency=2
57 for arg in "$@" ; do
58 if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
59 "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
60 "$arg" == "update-index" || "$arg" == "update-workdir" ]]
61 then
62 action=$arg
63 elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
64 action=$arg-workdir
65 elif [[ "$action" == diff-* ]] ; then
66 diffargs+=("$arg")
67 elif [[ "$arg" == --format=* ]] ; then
68 tidy_mode=${arg#--format=}
69 if [[ "$tidy_mode" != "off" && "$tidy_mode" != "check" ]] ; then
70 echo "Unknown option: $arg"
71 echo
72 usage
73 exit 2
75 elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
76 force=1
77 elif [[ "$arg" == "--parallel=*" ]] ; then
78 concurrency=${arg#--parallel=}
79 elif [[ "$arg" == --rev=* ]] ; then
80 baserev=${arg#--rev=}
81 elif [[ "$arg" == --warnings=* ]] ; then
82 warning_file=${arg#--warnings=}
83 elif [[ "$arg" == "-h" || "$arg" == "--help" ]] ; then
84 usage
85 exit 0
86 else
87 echo "Unknown option: $arg"
88 echo
89 usage
90 exit 2
92 done
94 # Check that format is present
95 if [[ "$tidy_mode" != "off" ]]
96 then
97 if [ -z "$RUN_CLANG_TIDY" ]
98 then
99 RUN_CLANG_TIDY=`git config hooks.run_clang_tidypath`
101 if [ -z "$RUN_CLANG_TIDY" ]
102 then
103 echo "Please set the path to run-clang-tidy using the git hook"
104 echo "git config hooks.run_clang_tidypath /path/to/run-clang-tidy"
105 echo "or by setting an environment variable, e.g."
106 echo "RUN_CLANG_TIDY=/path/to/run-clang-tidy"
107 exit 2
109 if ! which "$RUN_CLANG_TIDY" 1>/dev/null
110 then
111 echo "run-clang-tidy not found: $RUN_CLANG_TIDY"
112 exit 2
116 # Switch to the root of the source tree and check the config file
117 srcdir=`git rev-parse --show-toplevel`
118 pushd $srcdir >/dev/null
119 admin_dir=$srcdir/admin
121 # Actual processing starts: create a temporary directory
122 tmpdir=`mktemp -d -t gmxclangtidy.XXXXXX`
124 # Produce a list of changed files
125 # Only include files that have proper filter set in .gitattributes
126 internal_diff_args=
127 if [[ $action == *-index ]]
128 then
129 internal_diff_args="--cached"
131 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
132 cut -f2 <$tmpdir/difflist | \
133 git check-attr --stdin filter | \
134 sed -e 's/.*: filter: //' | \
135 paste $tmpdir/difflist - | \
136 grep -E '(complete_formatting)$' >$tmpdir/filtered
137 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
138 grep -E '(complete_formatting)$' <$tmpdir/filtered | \
139 cut -f2 >$tmpdir/filelist_clangtidy
140 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
142 # Extract changed files to a temporary directory
143 mkdir $tmpdir/org
144 if [[ $action == *-index ]] ; then
145 git checkout-index --prefix=$tmpdir/org/
146 else
147 rsync -a $srcdir/src/ $tmpdir/org/src/
149 # Need to have compilation database file available somewhere above where we are using it
150 rsync -a $srcdir/compile_commands.json $tmpdir/org
151 # Duplicate the original files to a separate directory, where all changes will
152 # be made.
153 cp -r $tmpdir/org $tmpdir/new
155 # Create output file for what was done (in case no messages get written)
156 touch $tmpdir/messages
158 # Run clang-tidy on the temporary directory
159 # Can only perform clang-tidy on a non-empty list of files
160 cd $tmpdir/new
161 if [[ $tidy_mode != "off" && -s $tmpdir/filelist_clangtidy ]] ; then
162 $RUN_CLANG_TIDY `cat $tmpdir/filelist_clangtidy` -- -header-filter=.* -j $concurrency -fix -fix-errors --cuda-host-only -nocudainc -quiet >$tmpdir/clang-tidy.out 2>&1
163 grep -v "clang-analyzer" $tmpdir/clang-tidy.out | grep -v "to display errors from all non" | grep -i "error|warning" - > $tmpdir/clang-tidy-errors.out
164 if [ -s $tmpdir/clang-tidy-errors.out ]; then
165 echo "Running code tidying failed. Check output below for errors:"
166 cat $tmpdir/clang-tidy-errors.out
167 rm -rf $tmpdir
168 exit 2
170 # Find the changed files if necessary
171 if [[ $action != diff-* ]] ; then
172 msg="needs formatting"
173 if [[ $action == update-* ]] ; then
174 msg="clang-tidy performed"
176 git diff --no-index --name-only ../org/ . | \
177 awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
179 # TODO: Consider checking whether rerunning clang-tidy causes additional changes
182 cd $tmpdir
184 # If a diff was requested, show it and we are done
185 if [[ $action == diff-* ]] ; then
186 git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
187 rm -rf $tmpdir
188 exit 0
191 # Find the changed files
192 git diff --no-index --name-only --exit-code org/ new/ | \
193 sed -e 's#new/##' > $tmpdir/changed
194 changes=
195 if [[ -s $tmpdir/changed ]]
196 then
197 changes=1
200 # Check if changed files have changed outside the index
201 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
202 then
203 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
204 >> $tmpdir/messages
205 if [[ ! $force && $action == update-* ]] ; then
206 echo "Modified files found in work tree, skipping update. Use -f to override."
207 echo "The following would have been done:"
208 sort $tmpdir/messages
209 rm -rf $tmpdir
210 exit 2
214 # Update the index/work tree if requested
215 if [[ $action == update-index ]] ; then
216 grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
217 cd $tmpdir/new
218 IFS='
220 for change in `cut -f2 $tmpdir/tohash | \
221 git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
222 paste - $tmpdir/tohash`
224 # NOTE: the patterns below contain literal tabs
225 sha1=${change%% *}
226 rest=${change#* }
227 mode=${rest:8:6}
228 path=${rest#* }
229 path=${path%% *}
230 # Contains a literal tab
231 echo "$mode $sha1 $path" >> $tmpdir/toindex
232 done
233 unset IFS
234 git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
235 elif [[ $action == update-workdir ]] ; then
236 rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
239 # Get back to the original directory
240 popd >/dev/null
242 # Report what was done
243 sort $tmpdir/messages | tee $warning_file
245 rm -rf $tmpdir
246 exit $changes