2016-04-02 07:28:36 +08:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
#
|
|
|
|
# This is meant to be run by "make lint" or "make lint-all". It is not meant to
|
|
|
|
# be run directly from a shell prompt.
|
|
|
|
#
|
2019-02-04 23:58:38 +08:00
|
|
|
|
|
|
|
# We don't include "missingInclude" as that doesn't find our config.h.
|
|
|
|
# Missing includes will quickly be found by... compiling the thing anyway.
|
|
|
|
set cppchecks warning,performance,portability,information #,missingInclude
|
2016-04-02 07:28:36 +08:00
|
|
|
set cppcheck_args
|
|
|
|
set c_files
|
|
|
|
set all no
|
2016-04-21 14:00:54 +08:00
|
|
|
set kernel_name (uname -s)
|
|
|
|
set machine_type (uname -m)
|
2016-04-02 07:28:36 +08:00
|
|
|
|
2019-02-25 22:46:31 +08:00
|
|
|
argparse a/all -- $argv
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2016-04-02 07:28:36 +08:00
|
|
|
# We only want -D and -I options to be passed thru to cppcheck.
|
|
|
|
for arg in $argv
|
|
|
|
if string match -q -- '-D*' $arg
|
|
|
|
set cppcheck_args $cppcheck_args $arg
|
|
|
|
else if string match -q -- '-I*' $arg
|
|
|
|
set cppcheck_args $cppcheck_args $arg
|
2017-02-02 14:06:24 +08:00
|
|
|
else if string match -q -- '-iquote*' $arg
|
|
|
|
set cppcheck_args $cppcheck_args $arg
|
2016-04-02 07:28:36 +08:00
|
|
|
end
|
|
|
|
end
|
2016-11-12 12:48:34 +08:00
|
|
|
|
|
|
|
# Not sure when this became necessary but without these flags cppcheck no longer works on macOS.
|
2017-02-02 14:06:24 +08:00
|
|
|
# It complains that "Cppcheck cannot find all the include files." Adding these include paths should
|
|
|
|
# be harmless everywhere else.
|
|
|
|
set cppcheck_args $cppcheck_args -I /usr/include -I .
|
2016-11-12 12:48:34 +08:00
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
if test "$machine_type" = "x86_64"
|
2016-04-02 07:28:36 +08:00
|
|
|
set cppcheck_args -D__x86_64__ -D__LP64__ $cppcheck_args
|
|
|
|
end
|
|
|
|
|
2019-02-04 23:58:38 +08:00
|
|
|
if set -q _flag_all
|
2016-04-02 11:48:11 +08:00
|
|
|
set c_files src/*.cpp
|
2019-02-04 23:58:38 +08:00
|
|
|
set cppchecks "$cppchecks,unusedFunction"
|
2016-04-02 11:48:11 +08:00
|
|
|
else
|
2016-04-02 07:28:36 +08:00
|
|
|
# We haven't been asked to lint all the source. If there are uncommitted
|
|
|
|
# changes lint those, else lint the files in the most recent commit.
|
2016-05-19 07:00:30 +08:00
|
|
|
# Select (cached files) (modified but not cached, and untracked files)
|
2016-11-15 11:20:12 +08:00
|
|
|
set files (git diff-index --cached HEAD --name-only)
|
|
|
|
set files $files (git ls-files --exclude-standard --others --modified)
|
2016-04-13 09:32:20 +08:00
|
|
|
if not set -q files[1]
|
2016-04-02 07:28:36 +08:00
|
|
|
# No pending changes so lint the files in the most recent commit.
|
2016-05-19 07:00:30 +08:00
|
|
|
set files (git diff-tree --no-commit-id --name-only -r HEAD)
|
2016-04-02 07:28:36 +08:00
|
|
|
end
|
|
|
|
|
2016-05-09 03:08:23 +08:00
|
|
|
# Extract just the C/C++ files that exist.
|
|
|
|
set c_files
|
|
|
|
for file in (string match -r '.*\.c(?:pp)?$' -- $files)
|
|
|
|
test -f $file; and set c_files $c_files $file
|
|
|
|
end
|
2016-04-02 07:28:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# We now have a list of files to check so run the linters.
|
|
|
|
if set -q c_files[1]
|
2017-02-12 12:14:57 +08:00
|
|
|
if type -q include-what-you-use
|
2016-06-24 08:24:19 +08:00
|
|
|
echo
|
|
|
|
echo ========================================
|
|
|
|
echo Running IWYU
|
|
|
|
echo ========================================
|
2016-04-21 14:00:54 +08:00
|
|
|
for c_file in $c_files
|
|
|
|
switch $kernel_name
|
2017-02-13 10:05:01 +08:00
|
|
|
case Darwin FreeBSD
|
2016-11-15 11:20:12 +08:00
|
|
|
include-what-you-use -Xiwyu --no_default_mappings -Xiwyu \
|
|
|
|
--mapping_file=build_tools/iwyu.osx.imp --std=c++11 \
|
|
|
|
$cppcheck_args $c_file 2>&1
|
2016-04-21 14:00:54 +08:00
|
|
|
case Linux
|
2016-11-15 11:20:12 +08:00
|
|
|
include-what-you-use -Xiwyu --mapping_file=build_tools/iwyu.linux.imp \
|
|
|
|
$cppcheck_args $c_file 2>&1
|
2016-04-21 14:00:54 +08:00
|
|
|
case '*' # hope for the best
|
2017-02-13 10:05:01 +08:00
|
|
|
include-what-you-use --std=c++11 $cppcheck_args $c_file 2>&1
|
2016-04-21 14:00:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-02 07:28:36 +08:00
|
|
|
if type -q cppcheck
|
|
|
|
echo
|
|
|
|
echo ========================================
|
|
|
|
echo Running cppcheck
|
|
|
|
echo ========================================
|
2016-04-13 09:32:20 +08:00
|
|
|
# The stderr to stdout redirection is because cppcheck, incorrectly IMHO, writes its
|
|
|
|
# diagnostic messages to stderr. Anyone running this who wants to capture its output will
|
|
|
|
# expect those messages to be written to stdout.
|
2016-11-15 11:20:12 +08:00
|
|
|
set -l cn (set_color normal)
|
|
|
|
set -l cb (set_color --bold)
|
|
|
|
set -l cu (set_color --underline)
|
|
|
|
set -l cm (set_color magenta)
|
|
|
|
set -l cbrm (set_color brmagenta)
|
|
|
|
set -l template "[$cb$cu{file}$cn$cb:{line}$cn] $cbrm{severity}$cm ({id}):$cn\n {message}"
|
2016-11-20 17:42:03 +08:00
|
|
|
set cppcheck_args -q --verbose --std=c++11 --std=posix --language=c++ --template $template \
|
2016-11-15 11:20:12 +08:00
|
|
|
--suppress=missingIncludeSystem --inline-suppr --enable=$cppchecks \
|
|
|
|
--rule-file=.cppcheck.rules --suppressions-list=.cppcheck.suppressions $cppcheck_args
|
|
|
|
|
|
|
|
cppcheck $cppcheck_args $c_files 2>&1
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo ========================================
|
2017-02-02 14:06:24 +08:00
|
|
|
echo 'Running `cppcheck --check-config` to identify missing includes and similar problems.'
|
2016-11-15 13:31:51 +08:00
|
|
|
echo 'Ignore unmatchedSuppression warnings as they are probably false positives we'
|
|
|
|
echo 'cannot suppress.'
|
2016-11-15 11:20:12 +08:00
|
|
|
echo ========================================
|
|
|
|
cppcheck $cppcheck_args --check-config $c_files 2>&1
|
2016-04-02 07:28:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if type -q oclint
|
|
|
|
echo
|
|
|
|
echo ========================================
|
|
|
|
echo Running oclint
|
|
|
|
echo ========================================
|
2016-04-13 09:32:20 +08:00
|
|
|
# The stderr to stdout redirection is because oclint, incorrectly writes its final summary
|
|
|
|
# counts of the errors detected to stderr. Anyone running this who wants to capture its
|
|
|
|
# output will expect those messages to be written to stdout.
|
2019-01-27 06:58:22 +08:00
|
|
|
oclint $c_files -- $argv 2>&1
|
2016-04-02 07:28:36 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
echo
|
|
|
|
echo 'WARNING: No C/C++ files to check'
|
|
|
|
echo
|
|
|
|
end
|