summaryrefslogtreecommitdiff
path: root/contrib/scripts/clang-scan-build
blob: c5f93acfa19f9c3aea96ae31dd17844e73f44c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
#
# clang-scan-build: configure and compile asterisk using the llvm static analyzer

# Options/Flags:
# -c|--compiler	: either [clang|gcc]
# --cflags	: cflags you would like to add to the default set
# --configure	: configure flags you would like to use instead off the default set
# --make	: make flags you would like to use instead off the default set
# --scanbuild	: scanbuild flags you would like to use instead of the default set
# --outputdir	: directory where scan-build should create the html files
# -h|--help	: this help

# Usage:
# contrib/scripts/clang-scan-build
# This script will use clang if available and no compiler has been specified
#
# Example usage:
#
#   contrib/scripts/clang-scan-build
#   contrib/scripts/clang-scan-build -c gcc
#   contrib/scripts/clang-scan-build --compiler clang --configure "--enable-dev-mode" --outputdir="/tmp/scan-build_output"
#   contrib/scripts/clang-scan-build --make "-j2"
#
# scan-build will generate html files during the make process, which will be stored in the specified outputdir or ./scan-build_output" by default

# Copyright (C) 2015 Diederik de Groot <dddegroot@users.sf.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA

COMPILER=clang
SCANBUILD="`which scan-build`"
CFLAGS=""
CONFIGURE_FLAGS="--enable-coverage --disable-xmldoc"
MAKE_FLAGS=""
SCANBUILD_FLAGS="-maxloop 10 -disable-checker deadcode.DeadStores -enable-checker alpha.core.BoolAssignment -enable-checker alpha.core.CallAndMessageUnInitRefArg -enable-checker alpha.core.CastSize -enable-checker alpha.core.CastToStruct -enable-checker alpha.core.IdenticalExpr -enable-checker alpha.core.PointerArithm -enable-checker alpha.core.PointerSub -enable-checker alpha.core.SizeofPtr -enable-checker alpha.core.TestAfterDivZero -enable-checker alpha.security.ArrayBound -enable-checker alpha.security.ArrayBoundV2 -enable-checker alpha.security.MallocOverflow -enable-checker alpha.security.ReturnPtrRange -enable-checker alpha.security.taint.TaintPropagation -enable-checker alpha.unix.MallocWithAnnotations -enable-checker alpha.unix.PthreadLock -enable-checker alpha.unix.SimpleStream -enable-checker alpha.unix.Stream -enable-checker alpha.unix.cstring.BufferOverlap -enable-checker alpha.unix.cstring.NotNullTerminated -enable-checker alpha.unix.cstring.OutOfBounds"
OUTPUTDIR="scan-build_output"

function print_usage {
cat << EOF
$0 Usage:

Options/Flags:
-c|--compiler	: either [clang|gcc]
--cflags	: cflags you would like to add to the default set:
		  "${CFLAGS}"

--configure	: configure flags you would like to use instead off the default set:
		  "${CONFIGURE_FLAGS}"

--make		: make flags you would like to use instead off the default set:
		  "${MAKE_FLAGS}"

--scanbuild	: scanbuild flags you would like to use instead of the default set:
		  "${SCANBUILD_FLAGS}"

--outputdir	: directory where scan-build should create the html files. default:
		  "`pwd`/${OUTPUTDIR}"

-h|--help	: this help
EOF
}

for i in "$@"
do
	case $i in
		-c=*|--compiler=*)
			COMPILER="${i#*=}"
			shift
		;;
		--cflags=*)
			CFLAGS="${i#*=}"
			shift
		;;
		--configure=*)
			CONFIGURE_FLAGS="${i#*=}"
			shift
		;;
		--make=*)
			MAKE_FLAGS="${i#*=}"
			shift
		;;
		--scanbuild=*)
			SCANBUILD_FLAGS="${i#*=}"
			shift
		;;
		--outputdir=*)
			OUTPUTDIR="${i#*=}"
			shift
		;;
		-h|--help)
			print_usage
			exit
		;;
	esac
done

if [ "${COMPILER}" == "clang" ] && [ ! -z "`which clang`" ]; then
	CCC_CC="`which`clang"
	CCC_CXX="`which clang++`"
	CFLAGS="-fblocks ${CFLAGS}"
elif [ "${COMPILER}" == "gcc" ] && [ ! -z "`which gcc`" ]; then
	CCC_CC="`which gcc`"
	CCC_CXX="`which g++`"
	CFLAGS="${CFLAGS}"
else
	echo "Unknown compiler: $2, needs to be either clang or gcc"
	exit
fi

if [ ! -f config.status ]; then
	echo "Running ./configure ${CONFIGURE_FLAGS} ..."
	${SCANBUILD} ${SCANBUILD_FLAGS} -o ${OUTPUTDIR} ./configure ${CONFIGURE_FLAGS}
	if [ $? != 0 ]; then
		echo "Configure error occurred, see output / config.log"
		exit
	fi
	make clean
fi
if [ -f config.status ]; then
	echo "Running scan-build make ${MAKE_FLAGS} ..."
	${SCANBUILD} ${SCANBUILD_FLAGS} -o ${OUTPUTDIR} make ${MAKE_FLAGS}
fi