Thursday, February 12, 2009

CLANG Static Analyzer

If you aren't using the LLVM/Clang Static Analyzer, you really should be. The Clang Project is an attempt to write a front end for the LLVM Compiler for the C language family. But the static analyzer tool is a standalone product you can run on any project, and it creates a nifty HTML report showing a bunch of possible bugs and/or problems with your code.

Here's a screen capture showing what the generated report looks like:

I forgot to release my formatter, and Clang realized it. This probably would have shown up in Instruments before I shipped, but it's nice to have all these little leaks and mistakes pointed out in one report. If you do nothing more than run the static analyzer over your code before shipping your next iPhone application, you will ship a better application.

Let's walk you through getting it installed and using it, so you have no excuse for not using it. First, go download the latest build. The website claims it's a bzip2 file, but Safari thinks it's a plain old tar file, and the following command expanded it for me:
tar -xvf checker-0.155.tar.bz2.tar

You may have to change the filename based on the latest version. If that doesn't work, then try using the bunzip2 command.

Now, you need to put this somewhere and add it to your PATH. You could put the files into /user/local/bin if you want. Personally, I have a folder in my home directory called bin that's part of my path. You can add a folder to your PATH variable by adding the folder to the invisible configuration file in your home directory for the Unix shell you use. Terminal now defaults to BASH, it used to default to TCSH. If you don't know which one you're using, go to Terminal's preferences and look at your startup options. The shell you use will be shown there - you can see from this screenshot that I'm a TCSH user.



If you're a TCSH user, like me, you add directories to your path variable by editing an invisible file in your home directory called .cshrc. If you use TextMate, you can edit this file by opening a terminal window and typing
mate .cshrc
or you can edit it right in the terminal using vi, emacs, or pico. Here's what my .cshrc file looks like - it's the first line you're interested in. If this file doesn't exist, go ahead and create a new file with the name.
set path= ( /usr/local/ImageMagick-6.4.3/bin /usr/X11/bin /opt/local /opt/local/bin ~/bin /usr/local/bin /usr/bin /bin /usr/X11R6/include/GL /System/Library/Frameworks/OpenGL.framework/Headers /usr/x11R6/lib /usr/local/sbin /usr/sbin /sbin /Users/jeff/Applications/apache-ant-1.6.1/bin /Users/jeff/bin /Developer/Tools /opt/local/bin /usr/local/BerkeleyDB.4.2/bin /usr/local/pgsql/bin /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ /Users/jeff/bin/checker-0.155)

setenv TERM vt100
setenv CVSROOT "/usr/local/cvsrep"
setenv CVSEDITOR "mate -w"
setenv SVN_EDITOR "mate -w"
setenv JAVA_HOME "/Library/Java/Home"
setenv PGUSER "pgsql"
setenv PGDATABASE "postgres"
setenv PYTHONPATH "/Users/jeff/bin/python:/usr/local/pgsql/lib/libpq.a"
setenv PGSQLDIR "/usr/local/pgsql/include/libpq"
setenv EDITOR "mate -w"
setenv MAGICK_HOME "/usr/local/ImageMagick-6.4.3"
setenv YLD_LIBRARY_PATH "$MAGICK_HOME/lib"

setenv MANPATH "${HOME}/man:/usr/local/share/man:/usr/share/man"
alias gwhois "whois -h geektools.com"
stty erase

To add directories to your path, just add them to that first line that says set path=. In this file, spaces separate the individual, which means you'll have to escape or put quotes around directories with spaces in them.

If you're a BASH person, the process is almost the same, except you need to edit a file called .bash_profile, and the syntax is a little different. Here's an example .bash_profile file. In this example, it's the last line you're interested in and, again, if the file doesn't exist, go ahead and create a new one.

[ -f /etc/profile ] && . /etc/profile
[ -f ~/.bashrc ] && . ~/.bashrc

export PATH="/usr/local/ImageMagick-6.2.6/bin:/opt/local:/opt/local/bin/:Users/jeff/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/Users/jeff/Applications/apache-ant-1.6.1/bin:/Users/jeff/bin:/Developer/Tools:/opt/local/bin:/usr/local/BerkeleyDB.4.2/bin:/usr/local/pgsql/bin:/Users/jeff/bin/checker-0.155"


In the BASH file, you separate the directories with a colon.

I'm going to assume that you've now put the static analyzer files somewhere in your path. Open a new terminal window. It has to be a new terminal window because any ones that are open won't have the changes you made to your path. Now, use the cd command to navigate to the folder of the Xcode project you want to analyze, for example:
cd dev/My Project Folder

Now, launch the analyzer by typing
scan-build xcodebuild

xcodebuild is a command line tool that does the same thing as pressing the build option in Xcode. By passing that command to scan-build, we're telling it to analyze the project that would get compiled if we typed xcodebuild in this directory. It will run for a few seconds, and then it will tell you how many bugs you have and tell you a command you can run to view the report. It'll look something like this:
** BUILD SUCCEEDED **
scan-build: 6 bugs found.
scan-build: Run 'scan-view /var/folders/w6/w6u1ni7mEjW7oDaih6Dw9U+++TI/-Tmp-//scan-build-2009-02-12-2' to examine bug reports.


Now, we can just copy that command and paste it back in the terminal:
scan-view /var/folders/w6/w6u1ni7mEjW7oDaih6Dw9U+++TI/-Tmp-//scan-build-2009-02-12-2

and our report will launch in a browser. It's a fully hyperlinked report that's pretty pretty easy to understand.

No comments:

Post a Comment