1#! /bin/sh
2
3# usage:
4#
5# make \
6#	REAL_CC=gcc-mine \
7#	CC=extra/scripts/cppcheck.sh \
8#	CPPCHECK_FLAGS="--enable=style,performance,portability,information,missingInclude --max-configs=256 -j $(($(getconf _NPROCESSORS_ONLN)-1))" \
9#	CPPCHECK_LIMIT="yes"
10
11# CPPCHECK_FLAGS are optional and are not set per default.
12# CPPCHECK_LIMIT limits cppcheck to the -D and -U that would be passed to CC.
13# Setting CPPCHECK_LIMIT greatly improves the check-time but obviously
14# just checks a small subset of the defines found in a file.
15
16: ${REAL_CC:=gcc}
17${REAL_CC} $@
18args=""
19limits=""
20next_arg=0
21next_limit=0
22
23for i in $@
24do
25  if [ $next_arg -eq 1 ] ; then
26	next_arg=0
27	case "/$i" in
28	/-*) exit 0 ;;
29	esac
30    [ "x$args" = "x" ] && args="$i" || args="$args $i"
31	continue
32  fi
33  if [ $next_limit -eq 1 ] ; then
34	next_limit=0
35    [ "x$limits" = "x" ] && limits="$i" || limits="$limits $i"
36	continue
37  fi
38  case "/$i" in
39  /-c) next_arg=1 ;;
40  /-isystem)
41		next_arg=1;
42		[ "x$args" = "x" ] && args="-I" || args="$args -I" ;;
43  /-I)
44		next_arg=1;
45		[ "x$args" = "x" ] && args="$i" || args="$args $i" ;;
46  /-I*) [ "x$args" = "x" ] && args="$i" || args="$args $i" ;;
47  /-D|/-U)
48		next_limit=1;
49		[ "x$limit" = "x" ] && limit="$i" || limit="$limit $i" ;;
50  /-D*) [ "x$limits" = "x" ] && limits="$i" || limits="$limits $i" ;;
51  /-s|/-S|/-dump*|/--print*|/-print*) exit 0 ;;
52  *) ;;
53  esac
54done
55[ -z "${CPPCHECK_LIMIT}" ] && limits=""
56[ -z "${args}" ] || exec cppcheck ${CPPCHECK_FLAGS} ${args} ${limits}
57