Статичний аналіз C/C++ програм за допомогою CLang через CMake

Все просто!

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=ccc-analyzer -DCMAKE_CXX_COMPILER=ccc-analyzer ..
scan-build make
 

Приклад використання:

$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=ccc-analyzer -DCMAKE_CXX_COMPILER=ccc-analyzer ..
-- The C compiler identification is GNU 6.4.0
-- The CXX compiler identification is GNU 6.4.0
-- Check for working C compiler: /usr/lib/llvm/5/libexec/ccc-analyzer
-- Check for working C compiler: /usr/lib/llvm/5/libexec/ccc-analyzer -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib/llvm/5/libexec/ccc-analyzer
-- Check for working CXX compiler: /usr/lib/llvm/5/libexec/ccc-analyzer -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenSSL: /usr/lib64/libcrypto.so (found version "1.0.2n")
-- Found MySQL: /usr/lib/libmysqlclient.so
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2")
-- Checking for module 'liburiparser'
--   Found liburiparser, version 0.8.4
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.13") found components:  doxygen dot
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kaa/projects/libtools/build
kaa@pc ~/projects/libtools/build (master) $ scan-build make
scan-build: Using '/usr/lib64/llvm/5/bin/clang-5.0' for static analysis
Scanning dependencies of target libtools_objects
[  3%] Building C object CMakeFiles/libtools_objects.dir/source/closeall.c.o
[  6%] Building C object CMakeFiles/libtools_objects.dir/source/daemonize.c.o
[  9%] Building C object CMakeFiles/libtools_objects.dir/source/directory.c.o
[ 12%] Building C object CMakeFiles/libtools_objects.dir/source/dir_remove.c.o
[ 15%] Building C object CMakeFiles/libtools_objects.dir/source/dump.c.o
[ 18%] Building C object CMakeFiles/libtools_objects.dir/source/env.c.o
[ 21%] Building C object CMakeFiles/libtools_objects.dir/source/executefile.c.o
[ 25%] Building C object CMakeFiles/libtools_objects.dir/source/file.c.o
[ 28%] Building C object CMakeFiles/libtools_objects.dir/source/flock.c.o
[ 31%] Building C object CMakeFiles/libtools_objects.dir/source/gethostaddr.c.o
[ 34%] Building C object CMakeFiles/libtools_objects.dir/source/hex.c.o
/home/kaa/projects/libtools/source/hex.c:72:2: warning: Potential leak of memory pointed to by 'out'
        return(ret);
        ^~~~~~~~~~~
1 warning generated.
[ 37%] Building C object CMakeFiles/libtools_objects.dir/source/http.c.o
[ 40%] Building C object CMakeFiles/libtools_objects.dir/source/iface.c.o
[ 43%] Building C object CMakeFiles/libtools_objects.dir/source/input.c.o
[ 46%] Building C object CMakeFiles/libtools_objects.dir/source/mailbox.c.o
[ 50%] Building C object CMakeFiles/libtools_objects.dir/source/mkpasswd.c.o
[ 53%] Building C object CMakeFiles/libtools_objects.dir/source/movefile.c.o
[ 56%] Building C object CMakeFiles/libtools_objects.dir/source/popen2.c.o
[ 59%] Building C object CMakeFiles/libtools_objects.dir/source/re.c.o
[ 62%] Building C object CMakeFiles/libtools_objects.dir/source/recvall.c.o
[ 65%] Building C object CMakeFiles/libtools_objects.dir/source/recvfile.c.o
[ 68%] Building C object CMakeFiles/libtools_objects.dir/source/recvline.c.o
[ 71%] Building C object CMakeFiles/libtools_objects.dir/source/sendfile.c.o
[ 75%] Building C object CMakeFiles/libtools_objects.dir/source/sha1.c.o
[ 78%] Building C object CMakeFiles/libtools_objects.dir/source/strexp.c.o
[ 81%] Building C object CMakeFiles/libtools_objects.dir/source/string.c.o
[ 84%] Building C object CMakeFiles/libtools_objects.dir/source/trim.c.o
[ 87%] Building C object CMakeFiles/libtools_objects.dir/source/mysql.c.o
[ 90%] Building C object CMakeFiles/libtools_objects.dir/source/url.c.o
[ 90%] Built target libtools_objects
Scanning dependencies of target libtools
[ 93%] Linking C shared library libtools.so
[ 93%] Built target libtools
Scanning dependencies of target libtools_doc
[ 96%] Generating API documentation with Doxygen
[ 96%] Built target libtools_doc
Scanning dependencies of target libtools_static
[100%] Linking C static library libtools.a
[100%] Built target libtools_static
scan-build: 1 bug found.
scan-build: Run 'scan-view /tmp/scan-build-2018-03-11-114953-11003-1' to examine bug reports.
 

Запускаємо переглядач результатів:

$ scan-view /tmp/scan-build-2018-03-11-114953-11003-1
Starting scan-view at: http://127.0.0.1:8181
  Use Ctrl-C to exit.
 

scan-build

Клікаємо на файл, аналізуємо проблему: File