Jun's Blog

Output, activities, memo and etc.

Static Analysis Tools for C language on Mac

Try to use static analysis tools for C language on Mac, such as Perl's Perl::Critic, PHP's phpcs.

Tried Clang Static Analyzer, and Splint.

Clang Static Analyzer

A lint tool for Mac, and UNIX platform.

Install

Download checker-NNN.tar.bz2 file to Mac.
Clang Static Analyzer

Only unpack the module, you can use it, if the OS is Mac.

$ tar xjvf checker-277.tar.bz2
$ mv checker-277 /usr/local/clang-static-analyzer-277
$ cd /usr/local
$ ln -s clang-static-analyzer-277 clang-static-analyzer

Usage

There are 2 commands scan-build and scan-view.
Set scan-build head of make command for your c language project.
No bugs found in below case.

$ make clean

$ /usr/local/clang-static-analyzer/scan-build make
scan-build: Using '/usr/local/clang-static-analyzer-277/bin/clang' for static analysis
/Library/Developer/CommandLineTools/usr/bin/make -C src all
/usr/local/clang-static-analyzer-277/libexec/ccc-analyzer -Werror -g -O0 -c thread_main.c
mkdir -p ../bin
/usr/local/clang-static-analyzer-277/libexec/ccc-analyzer -Werror -g -O0 -o ../bin/thread thread_main.o
/usr/local/clang-static-analyzer-277/libexec/ccc-analyzer -Werror -g -O0 -c multi_proc_main.c
mkdir -p ../bin
/usr/local/clang-static-analyzer-277/libexec/ccc-analyzer -Werror -g -O0 -o ../bin/multi_proc multi_proc_main.o
scan-build: Removing directory '/var/folders/vm/hz3dc6c17l10ltq50b9g8gvc0000gp/T/scan-build-2016-02-01-132157-81214-1' because it contains no reports.
scan-build: No bugs found.

Splint

Splint = Secure Programming Lint
A lint tool for Linux and UNIX platform.
http://www.splint.org

Install

Install Splint 3.1.2 on Mac.
Download latest source code below official web site.
Downloading Splint

In the case of Mac, need to compile from source code, and install.

$ tar xzvf splint-3.1.2.src.tgz
$ cd splint-3.1.2
$ ./configure --help
$ ./configure --prefix=/usr/local/splint-3.1.2

Got an error.

$ make
...
osd.c:519:3: error: unknown type name '__pid_t'; did you mean 'pid_t'?
  __pid_t pid = getpid ();
  ^~~~~~~
  pid_t
/usr/include/sys/_types/_pid_t.h:30:31: note: 'pid_t' declared here
typedef __darwin_pid_t        pid_t;
                              ^
$ cd src
$ cp -p osd.c osd.c.org

This error seems happened for except Windows or OS2.
Nice "did you mean" feature of gcc.

$ vi osd.c
 516 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
 517   int pid = _getpid ();
 518 # else
 519   __pid_t pid = getpid ();
 520 # endif

Edited the source file. But though I did not report to the community :

$ diff osd.c.org osd.c
519c519
<   __pid_t pid = getpid ();
---
>   pid_t pid = getpid ();

make command was succeed after updating the source code.

$ make clean
$ make
$ make install
$ cd /usr/local/
$ ln -s splint-3.1.2 splint

Add path: /usr/local/splint/bin/ to PATH.

$ vi ~/.bashrc
...
PATH="${PATH}:/usr/local/splint/bin"
export PATH
...
$ source ~/.bashrc

$ which splint
/usr/local/splint/bin/splint
$ splint -help
Splint 3.1.2 --- 01 Feb 2016
...

Usage

Strict mode.

$ splint *.c

Weak mode.

$ splint -weak *.c

Output sample that I tried to use it for my C source codes.

$ splint *.c
Splint 3.1.2 --- 01 Feb 2016

multi_proc_main.c:6: Include file <sys/wait.h> matches the name of a POSIX
    library, but the POSIX library is not being used.  Consider using +posixlib
    or +posixstrictlib to select the POSIX library, or -warnposix to suppress
    this message.
  Header name matches a POSIX header, but the POSIX library is not selected.
  (Use -warnposixheaders to inhibit warning)
multi_proc_main.c:7: Include file <unistd.h> matches the name of a POSIX
    library, but the POSIX library is not being used.  Consider using +posixlib
    or +posixstrictlib to select the POSIX library, or -warnposix to suppress
    this message.
thread_main.c:5: Include file <unistd.h> matches the name of a POSIX library,
    but the POSIX library is not being used.  Consider using +posixlib or
    +posixstrictlib to select the POSIX library, or -warnposix to suppress this
    message.
/usr/include/sys/_types.h:94:36: Parse Error:
    Suspect missing struct or union keyword: __int64_t :
    int. (For help on parse errors, see splint -help parseerrors.)
*** Cannot continue.

See manual page for detail: http://www.splint.org/manual/

That's all.