2c5a6a1046601a069985c171afebc4bdc4d71355
[alexxy/gromacs.git] / admin / clang-tidy.sh
1 #!/bin/bash
2 #
3 # This file is part of the GROMACS molecular simulation package.
4 #
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.
9 #
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.
14 #
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.
19 #
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.
24 #
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.
32 #
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.
35
36 # This script runs clang tidy checks on modified files and
37 # reports/applies the necessary changes.
38 #
39 # See `clang-tidy.sh -h` for a brief usage, and docs/dev-manual/code-formatting.rst
40 # for more details.
41
42 # Parse command-line arguments
43 function usage() {
44     echo "usage: clang-tidy.sh [-f|--force] [--parallel=#Jobs] [--rev=REV]"
45     echo "           [--tidy=(off|check)]"
46     echo "           [--warnings=<file>] [<action>]"
47     echo "           [-B=<builddir>]"
48     echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
49 }
50
51 action="check-workdir"
52 declare -a diffargs
53 baserev="HEAD"
54 force=
55 tidy_mode=check
56 warning_file=
57 builddir=
58 concurrency=2
59 for arg in "$@" ; do
60     if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
61           "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
62           "$arg" == "update-index" || "$arg" == "update-workdir" ]]
63     then
64         action=$arg
65     elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
66         action=$arg-workdir
67     elif [[ "$action" == diff-* ]] ; then
68         diffargs+=("$arg")
69     elif [[ "$arg" == --tidy=* ]] ; then
70         tidy_mode=${arg#--tidy=}
71         if [[ "$tidy_mode" != "off" && "$tidy_mode" != "check" ]] ; then
72             echo "Unknown option: $arg"
73             echo
74             usage
75             exit 2
76         fi
77     elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
78         force=1
79     elif [[ "$arg" == --parallel=* ]] ; then
80         concurrency=${arg#--parallel=}
81     elif [[ "$arg" == --rev=* ]] ; then
82         baserev=${arg#--rev=}
83     elif [[ "$arg" == --warnings=* ]] ; then
84         warning_file=${arg#--warnings=}
85     elif [[ "$arg" == -B=* ]] ; then
86         builddir=${arg#-B=}
87     elif [[ "$arg" == "-h" || "$arg" == "--help" ]] ; then
88         usage
89         exit 0
90     else
91         echo "Unknown option: $arg"
92         echo
93         usage
94         exit 2
95     fi
96 done
97
98 # Check that format is present
99 if [[ "$tidy_mode" != "off" ]]
100 then
101     if [ -z "$RUN_CLANG_TIDY" ]
102     then
103         RUN_CLANG_TIDY=`git config hooks.runclangtidypath`
104     fi
105     if [ -z "$RUN_CLANG_TIDY" ]
106     then
107         echo "Please set the path to run-clang-tidy using the git hook"
108         echo "git config hooks.runclangtidypath /path/to/run-clang-tidy-9.py"
109         echo "or by setting an environment variable, e.g."
110         echo "RUN_CLANG_TIDY=/path/to/run-clang-tidy-9.py"
111         exit 2
112     fi
113     if ! which "$RUN_CLANG_TIDY" 1>/dev/null
114     then
115         echo "run-clang-tidy-9.py not found: $RUN_CLANG_TIDY"
116         exit 2
117     fi
118 fi
119
120 # Switch to the root of the source tree and check the config file
121 srcdir=`git rev-parse --show-toplevel`
122 pushd $srcdir >/dev/null || exit
123
124 # Actual processing starts: create a temporary directory
125 tmpdir=`mktemp -d -t gmxclangtidy.XXXXXX`
126
127 # Produce a list of changed files
128 # Only include files that have proper filter set in .gitattributes
129 internal_diff_args=
130 if [[ $action == *-index ]]
131 then
132     internal_diff_args="--cached"
133 fi
134 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
135 cut -f2 <$tmpdir/difflist | \
136     git check-attr --stdin filter | \
137     sed -e 's/.*: filter: //' | \
138     paste $tmpdir/difflist - | \
139     grep -E '(complete_formatting|clangformat|copyright|includesort)$' >$tmpdir/filtered
140 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
141 grep -E '(complete_formatting|clangformat)$' <$tmpdir/filtered | \
142     cut -f2 >$tmpdir/filelist_clangtidy
143 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
144
145 # Extract changed files to a temporary directory
146 mkdir $tmpdir/org
147 if [[ $action == *-index ]] ; then
148     git checkout-index --prefix=$tmpdir/org/
149 else
150     rsync -a $srcdir/src/ $tmpdir/org/src/
151 fi
152 # check for the existence of the compile_commands.json file and abort
153 # if it is not present. If we don't have a build directory, try the
154 # current source directory.
155 if [ -z $builddir ] ; then
156     builddir=$srcdir
157 fi
158 if [[ ! -f $builddir/compile_commands.json ]] ; then
159     echo "Could not find compile_commands.json in builddir=$builddir"
160     echo "Make sure you gave a correct build tree and that it contains the file!"
161 else
162     # Need to have compilation database file available somewhere above where we are using it
163     rsync -a $builddir/compile_commands.json $tmpdir/org
164 fi
165 # Duplicate the original files to a separate directory, where all changes will
166 # be made.
167 cp -r $tmpdir/org $tmpdir/new
168
169 # Create output file for what was done (in case no messages get written)
170 touch $tmpdir/messages
171
172 # Run clang-tidy on the temporary directory
173 # Can only perform clang-tidy on a non-empty list of files
174 cd $tmpdir/new
175 if [[ $tidy_mode != "off" &&  -s $tmpdir/filelist_clangtidy ]] ; then
176     $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
177     awk '/warning/,/clang-tidy|^$/' $tmpdir/clang-tidy.out | grep -v "warnings generated." | grep -v "Suppressed .* warnings" | grep -v "clang-analyzer"  | grep -v "to display errors from all non" | sed '/^\s*$/d' > $tmpdir/clang-tidy-errors.out
178     cp $tmpdir/clang-tidy.out $tmpdir/clang-tidy-errors.out $srcdir
179     if [ -s $tmpdir/clang-tidy-errors.out ]; then
180         echo "Running code tidying failed. Check output below for errors:"
181         cat $tmpdir/clang-tidy-errors.out
182         rm -rf $tmpdir
183         exit 2
184     fi
185     # Find the changed files if necessary
186     if [[ $action != diff-* ]] ; then
187         msg="found code issues"
188         if [[ $action == update-* ]] ; then
189             msg="clang-tidy performed"
190         fi
191         git diff --no-index --name-only ../org/ . | \
192             awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
193     fi
194     # TODO: Consider checking whether rerunning clang-tidy causes additional changes
195 fi
196
197 cd $tmpdir
198
199 # If a diff was requested, show it and we are done
200 if [[ $action == diff-* ]] ; then
201     git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
202     rm -rf $tmpdir
203     exit 0
204 fi
205
206 # Find the changed files
207 git diff --no-index --name-only --exit-code org/ new/ | \
208     sed -e 's#new/##' > $tmpdir/changed
209 changes=
210 if [[ -s $tmpdir/changed ]]
211 then
212     changes=1
213 fi
214
215 # Check if changed files have changed outside the index
216 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
217 then
218     awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
219         >> $tmpdir/messages
220     if [[ ! $force && $action == update-* ]] ; then
221         echo "Modified files found in work tree, skipping update. Use -f to override."
222         echo "The following would have been done:"
223         sort $tmpdir/messages
224         rm -rf $tmpdir
225         exit 2
226     fi
227 fi
228
229 # Update the index/work tree if requested
230 if [[ $action == update-index ]] ; then
231     grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
232     cd $tmpdir/new
233     IFS='
234 '
235     for change in `cut -f2 $tmpdir/tohash | \
236                    git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
237                    paste - $tmpdir/tohash`
238     do
239         # NOTE: the patterns below contain literal tabs
240         sha1=${change%% *}
241         rest=${change#* }
242         mode=${rest:8:6}
243         path=${rest#*   }
244         path=${path%%   *}
245         # Contains a literal tab
246         echo "$mode $sha1       $path" >> $tmpdir/toindex
247     done
248     unset IFS
249     git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
250 elif [[ $action == update-workdir ]] ; then
251     rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
252 fi
253
254 # Get back to the original directory
255 popd >/dev/null
256
257 # Report what was done
258 sort $tmpdir/messages | tee $warning_file
259
260 rm -rf $tmpdir
261 exit $changes