1#!/bin/sh
2
3set -eu
4
5SCRIPT_DIR=$(dirname "$0")
6case $SCRIPT_DIR in
7    "/"*)
8        ;;
9    ".")
10        SCRIPT_DIR=$(pwd)
11        ;;
12    *)
13        SCRIPT_DIR=$(pwd)/$(dirname "$0")
14        ;;
15esac
16
17LOG_FILE=/tmp/cppcheck_qgis.txt
18
19rm -f ${LOG_FILE}
20echo "Checking ${SCRIPT_DIR}/../src ..."
21
22cppcheck --library=qt.cfg --inline-suppr \
23         --template='{file}:{line},{severity},{id},{message}' \
24         --enable=all --inconclusive --std=c++11 \
25         -DPROJ_VERSION_MAJOR=6 \
26         -USIP_RUN \
27         -DSIP_TRANSFER= \
28         -DSIP_TRANSFERTHIS= \
29         -DSIP_INOUT= \
30         -DSIP_OUT= \
31         -DSIP_FACTORY= \
32         -DSIP_THROW= \
33         -DCMAKE_SOURCE_DIR="/foo/bar" \
34         -DQ_NOWARN_DEPRECATED_PUSH= \
35         -DQ_NOWARN_DEPRECATED_POP= \
36         -DQ_DECLARE_OPAQUE_POINTER= \
37         -j $(nproc) \
38         ${SCRIPT_DIR}/../src \
39         >>${LOG_FILE} 2>&1 &
40
41PID=$!
42while kill -0 $PID 2>/dev/null; do
43    printf "."
44    sleep 1
45done
46echo " done"
47if ! wait $PID; then
48    echo "cppcheck failed"
49    exit 1
50fi
51
52ret_code=0
53
54cat ${LOG_FILE} | grep -v -e "syntaxError," -e "cppcheckError," > ${LOG_FILE}.tmp
55mv ${LOG_FILE}.tmp ${LOG_FILE}
56
57for category in "style" "performance" "portability"; do
58    if grep "${category}," ${LOG_FILE} >/dev/null; then
59        echo "INFO: Issues in '${category}' category found, but not considered as making script to fail:"
60        grep "${category}," ${LOG_FILE} | grep -v -e "clarifyCalculation," -e "duplicateExpressionTernary," -e "redundantCondition," -e "unusedPrivateFunction," -e "postfixOperator,"
61        echo ""
62    fi
63done
64
65# unusedPrivateFunction not reliable enough in cppcheck 1.72 of Ubuntu 16.04
66if test "$(cppcheck --version)" = "Cppcheck 1.72"; then
67    UNUSED_PRIVATE_FUNCTION=""
68else
69    UNUSED_PRIVATE_FUNCTION="unusedPrivateFunction"
70fi
71
72for category in "error" "warning" "clarifyCalculation" "duplicateExpressionTernary" "redundantCondition" "postfixOperator" "${UNUSED_PRIVATE_FUNCTION}"; do
73    if test "${category}" != ""; then
74        if grep "${category}," ${LOG_FILE}  >/dev/null; then
75            echo "ERROR: Issues in '${category}' category found:"
76            grep "${category}," ${LOG_FILE}
77            echo ""
78            echo "${category} check failed !"
79            ret_code=1
80        fi
81    fi
82done
83
84if [ ${ret_code} = 0 ]; then
85    echo "cppcheck succeeded"
86fi
87
88exit ${ret_code}
89