clang-scan-build 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. #
  3. # clang-scan-build: configure and compile asterisk using the llvm static analyzer
  4. # Options/Flags:
  5. # -c|--compiler : either [clang|gcc]
  6. # --cflags : cflags you would like to add to the default set
  7. # --configure : configure flags you would like to use instead off the default set
  8. # --make : make flags you would like to use instead off the default set
  9. # --scanbuild : scanbuild flags you would like to use instead of the default set
  10. # --outputdir : directory where scan-build should create the html files
  11. # -h|--help : this help
  12. # Usage:
  13. # contrib/scripts/clang-scan-build
  14. # This script will use clang if available and no compiler has been specified
  15. #
  16. # Example usage:
  17. #
  18. # contrib/scripts/clang-scan-build
  19. # contrib/scripts/clang-scan-build -c gcc
  20. # contrib/scripts/clang-scan-build --compiler clang --configure "--enable-dev-mode" --outputdir="/tmp/scan-build_output"
  21. # contrib/scripts/clang-scan-build --make "-j2"
  22. #
  23. # scan-build will generate html files during the make process, which will be stored in the specified outputdir or ./scan-build_output" by default
  24. # Copyright (C) 2015 Diederik de Groot <dddegroot@users.sf.net>
  25. #
  26. # This program is free software; you can redistribute it and/or modify
  27. # it under the terms of the GNU General Public License as published by
  28. # the Free Software Foundation; either version 2 of the License, or
  29. # (at your option) any later version.
  30. #
  31. # This program is distributed in the hope that it will be useful,
  32. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. # GNU General Public License for more details.
  35. #
  36. # You should have received a copy of the GNU General Public License
  37. # along with this program; if not, write to the Free Software
  38. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  39. # USA
  40. COMPILER=clang
  41. SCANBUILD="`which scan-build`"
  42. CFLAGS=""
  43. CONFIGURE_FLAGS="--enable-coverage --disable-xmldoc"
  44. MAKE_FLAGS=""
  45. 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"
  46. OUTPUTDIR="scan-build_output"
  47. function print_usage {
  48. cat << EOF
  49. $0 Usage:
  50. Options/Flags:
  51. -c|--compiler : either [clang|gcc]
  52. --cflags : cflags you would like to add to the default set:
  53. "${CFLAGS}"
  54. --configure : configure flags you would like to use instead off the default set:
  55. "${CONFIGURE_FLAGS}"
  56. --make : make flags you would like to use instead off the default set:
  57. "${MAKE_FLAGS}"
  58. --scanbuild : scanbuild flags you would like to use instead of the default set:
  59. "${SCANBUILD_FLAGS}"
  60. --outputdir : directory where scan-build should create the html files. default:
  61. "`pwd`/${OUTPUTDIR}"
  62. -h|--help : this help
  63. EOF
  64. }
  65. for i in "$@"
  66. do
  67. case $i in
  68. -c=*|--compiler=*)
  69. COMPILER="${i#*=}"
  70. shift
  71. ;;
  72. --cflags=*)
  73. CFLAGS="${i#*=}"
  74. shift
  75. ;;
  76. --configure=*)
  77. CONFIGURE_FLAGS="${i#*=}"
  78. shift
  79. ;;
  80. --make=*)
  81. MAKE_FLAGS="${i#*=}"
  82. shift
  83. ;;
  84. --scanbuild=*)
  85. SCANBUILD_FLAGS="${i#*=}"
  86. shift
  87. ;;
  88. --outputdir=*)
  89. OUTPUTDIR="${i#*=}"
  90. shift
  91. ;;
  92. -h|--help)
  93. print_usage
  94. exit
  95. ;;
  96. esac
  97. done
  98. if [ "${COMPILER}" == "clang" ] && [ ! -z "`which clang`" ]; then
  99. CCC_CC="`which`clang"
  100. CCC_CXX="`which clang++`"
  101. CFLAGS="-fblocks ${CFLAGS}"
  102. elif [ "${COMPILER}" == "gcc" ] && [ ! -z "`which gcc`" ]; then
  103. CCC_CC="`which gcc`"
  104. CCC_CXX="`which g++`"
  105. CFLAGS="${CFLAGS}"
  106. else
  107. echo "Unknown compiler: $2, needs to be either clang or gcc"
  108. exit
  109. fi
  110. if [ ! -f config.status ]; then
  111. echo "Running ./configure ${CONFIGURE_FLAGS} ..."
  112. ${SCANBUILD} ${SCANBUILD_FLAGS} -o ${OUTPUTDIR} ./configure ${CONFIGURE_FLAGS}
  113. if [ $? != 0 ]; then
  114. echo "Configure error occurred, see output / config.log"
  115. exit
  116. fi
  117. make clean
  118. fi
  119. if [ -f config.status ]; then
  120. echo "Running scan-build make ${MAKE_FLAGS} ..."
  121. ${SCANBUILD} ${SCANBUILD_FLAGS} -o ${OUTPUTDIR} make ${MAKE_FLAGS}
  122. fi