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
42 # Parse command-line arguments
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"
58 if [[ "$arg" == "check-index" ||
"$arg" == "check-workdir" || \
59 "$arg" == "diff-index" ||
"$arg" == "diff-workdir" || \
60 "$arg" == "update-index" ||
"$arg" == "update-workdir" ]]
63 elif [[ "$arg" == "check" ||
"$arg" == "diff" ||
"$arg" == "update" ]] ; then
65 elif [[ "$action" == diff-
* ]] ; then
67 elif [[ "$arg" == --format=* ]] ; then
68 tidy_mode
=${arg#--format=}
69 if [[ "$tidy_mode" != "off" && "$tidy_mode" != "check" ]] ; then
70 echo "Unknown option: $arg"
75 elif [[ "$arg" == "-f" ||
"$arg" == "--force" ]] ; then
77 elif [[ "$arg" == "--parallel=*" ]] ; then
78 concurrency
=${arg#--parallel=}
79 elif [[ "$arg" == --rev=* ]] ; then
81 elif [[ "$arg" == --warnings=* ]] ; then
82 warning_file
=${arg#--warnings=}
83 elif [[ "$arg" == "-h" ||
"$arg" == "--help" ]] ; then
87 echo "Unknown option: $arg"
94 # Check that format is present
95 if [[ "$tidy_mode" != "off" ]]
97 if [ -z "$RUN_CLANG_TIDY" ]
99 RUN_CLANG_TIDY
=`git config hooks.run_clang_tidypath`
101 if [ -z "$RUN_CLANG_TIDY" ]
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"
109 if ! which "$RUN_CLANG_TIDY" 1>/dev
/null
111 echo "run-clang-tidy not found: $RUN_CLANG_TIDY"
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
127 if [[ $action == *-index ]]
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
144 if [[ $action == *-index ]] ; then
145 git checkout-index
--prefix=$tmpdir/org
/
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
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
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
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
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
/
191 # Find the changed files
192 git
diff --no-index --name-only --exit-code org
/ new
/ | \
193 sed -e 's#new/##' > $tmpdir/changed
195 if [[ -s $tmpdir/changed
]]
200 # Check if changed files have changed outside the index
201 if grep -Ff $tmpdir/localmods
$tmpdir/changed
> $tmpdir/conflicts
203 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
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
214 # Update the index/work tree if requested
215 if [[ $action == update-index
]] ; then
216 grep -Ff $tmpdir/changed
$tmpdir/filtered
> $tmpdir/tohash
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
230 # Contains a literal tab
231 echo "$mode $sha1 $path" >> $tmpdir/toindex
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
242 # Report what was done
243 sort $tmpdir/messages |
tee $warning_file