aboutsummaryrefslogtreecommitdiff
path: root/scripts/pvscheck.sh
blob: 32d63646aa9cfef51dfaaa0b9fc1bb4d4f0f655d (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/sh
set -e

get_jobs_num() {
  local num="$(cat /proc/cpuinfo | grep -c "^processor")"
  num="$(echo $(( num + 1 )))"
  num="${num:-1}"
  echo $num
}

help() {
  echo 'Usage:'
  echo '  pvscheck.sh [target-directory [branch]]'
  echo '  pvscheck.sh [--recheck] [target-directory]'
  echo '  pvscheck.sh --patch [--only-build]'
  echo
  echo '    --patch: patch sources in the current directory.'
  echo '             Does not patch already patched files.'
  echo '             Does not run analysis.'
  echo
  echo '    --only-build: Only patch files in ./build directory.'
  echo
  echo '    --recheck: run analysis on a prepared target directory.'
  echo
  echo '    target-directory: Directory where build should occur.'
  echo '                      Default: ../neovim-pvs'
  echo
  echo '    branch: Branch to check.'
  echo '            Default: master.'
}

get_pvs_comment() {
  cat > pvs-comment << EOF
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

EOF
}

install_pvs() {
  mkdir pvs-studio
  cd pvs-studio

  curl -o pvs-studio.tar.gz "$PVS_URL"
  tar xzf pvs-studio.tar.gz
  rm pvs-studio.tar.gz
  local pvsdir="$(find . -maxdepth 1 -mindepth 1)"
  find "$pvsdir" -maxdepth 1 -mindepth 1 -exec mv '{}' . \;
  rmdir "$pvsdir"

  export PATH="$PWD/bin${PATH+:}${PATH}"

  cd ..
}

create_compile_commands() {
  mkdir build
  cd build
  env \
    CC=clang \
    CFLAGS=' -O0 ' \
    cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX="$PWD/root"
  make -j"$(get_jobs_num)"
  find src/nvim/auto -name '*.test-include.c' -delete

  cd ..
}

patch_sources() {
  get_pvs_comment

  local sh_script='
    pvs_comment="$(cat pvs-comment ; echo -n EOS)"
    filehead="$(head -c $(( ${#pvs_comment} - 3 )) "$1" ; echo -n EOS)"
    if test "x$filehead" != "x$pvs_comment" ; then
      cat pvs-comment "$1" > "$1.tmp"
      mv "$1.tmp" "$1"
    fi
  '

  if test "x$1" != "x--only-build" ; then
    find \
      src/nvim test/functional/fixtures test/unit/fixtures \
      -name '*.c' \
      -exec /bin/sh -c "$sh_script" - '{}' \;
  fi

  find \
    build/src/nvim/auto build/config \
    -name '*.c' -not -name '*.test-include.c' \
    -exec /bin/sh -c "$sh_script" - '{}' \;

  rm pvs-comment
}

run_analysis() {
  pvs-studio-analyzer \
    analyze \
      --threads "$(get_jobs_num)" \
      --output-file PVS-studio.log \
      --verbose \
      --file build/compile_commands.json \
      --sourcetree-root .

  plog-converter -t xml -o PVS-studio.xml PVS-studio.log
  plog-converter -t errorfile -o PVS-studio.err PVS-studio.log
  plog-converter -t tasklist -o PVS-studio.tsk PVS-studio.log
}

do_check() {
  local tgt="${1}"
  local branch="${2}"

  git clone --branch="$branch" . "$tgt"

  cd "$tgt"

  install_pvs

  create_compile_commands

  run_analysis
}

do_recheck() {
  local tgt="${1}"

  cd "$tgt"

  export PATH="$PWD/pvs-studio/bin${PATH+:}${PATH}"

  run_analysis
}

main() {
  local PVS_URL="http://files.viva64.com/pvs-studio-6.14.21446.1-x86_64.tgz"

  if test "x$1" = "x--help" ; then
    help
    return
  fi

  set -x

  if test "x$1" = "x--patch" ; then
    shift
    if test "x$1" = "x--only-build" ; then
      shift
      patch_sources --only-build
    else
      patch_sources
    fi
    exit $?
  fi

  local recheck=
  if test "x$1" = "x--recheck" ; then
    recheck=1
    shift
  fi

  local tgt="${1:-$PWD/../neovim-pvs}"
  local branch="${2:-master}"

  if test "x$recheck" = "x" ; then
    do_check "$tgt" "$branch"
  else
    do_recheck "$tgt"
  fi
}

main "$@"