Thursday, May 14, 2009

Clang Checker: Making your development and debugging life easier.

In one of the more recent iTunes U - iPhone Application Programming lectures offered from Stanford (blog entry), Evan mentioned Clang, a cross-platform compiler written in C++ designed for the C family of programming languages including C, C++ and Objective-C and the Clang Checker, a static code analysis tool built on Clang. At Able Pear Software Don is the technical lead in the software side of things and it's easy for him to navigate around and squash bugs, plug leaks, and solve issues that are more often than not introduced by me. But I would like to pull my own weight around in helping with this process.

As someone who is relatively new to Objective-C and to hard-core OOP in general, I find that Xcode's debug tools are more than a little confusing to me. With vague or rather non-informative hints as to where a problem is or exactly what the issue pertains to, it does not help me fix the issues at hand and I find myself asking Don for more pair-programming time - or in this case pair-debugging.

As I have very much enjoyed Evan and Alan's lectures thus far and when Evan mentioned Clang I thought I should at least take a look at it and try to understand what it is and if it can help us with developing and debugging our suite of iPhone applications.

Clang is designed to be compatible with code that can be compiled today with GCC. That's very exciting for the open source software community, because it means that Clang and LLVM could some day be used as a complete replacement for the entire GCC stack.
-- Ryan Paul, Ars Technica, Things that go Clang in the night (Article)

So what does all this mean and how can I use it were my initial thought followed by and how hard will it be for me to install and implement such a tool? The answers I found were easier than I thought. But to provide you with a snapshot of our current state of things I should walk you thought the steps in which brought me to this current point.

Download and Installation
The first thing I had to do was go to the LLVM website and find the correct files to download. As the link was in Evan’s presentation I simply clicked myself to the page; I will provide the link for you here. There are additional ways to download the current build of Clang Checker via outlined here; as the project is under heavy and constant development you may find new updates posted daily.

Once I downloaded the tar ball I moved it to it’s new home, in my case Documents>Xcode, where I then expanded the file and renamed the folder to ClangChecker.

Creating the Path Shortcut
Now that I have the file I could follow the instructions and run it with the long command line instruction in Terminal, but since I have to use Terminal I would like the experience to be easy as possible. Evan commented on adding a “shortcut” into the .bash_profile and demonstrated his version using emacs. That was fine but I prefer pico, and you may prefer vi or any other text editor.  Here is what I added to the .bash_profile file:

export PATH=~/Documents/Xcode/ClangChecker/:$PATH

Here I’m telling the terminal to look for the script to run in the directory where I unpacked the tar ball I uninstalled and renamed. This allows you to avoid typing in the path each time you’d like to run Clang Checker. Great, but to use Clang Checker I still have to “clean” the Xcode project I’m working on and navigate to the root folder in Terminal, then type the command... At this point I showed Don what I had done.

Running Clang Checker
I showed Don Clang Checker, ran the script and we immediately watched Clang Checker spew output to the Terminal window. Moments later the compiling and checking was done and Clang Checker launched Safari with our report: four errors, two ‘retain/release’ issues and two leaks.  We click on the view link and it opened a formatted version of the source with annotations of the issues in the file. Don started to walk me through the report but I asked him to allow me to walk him through it as this was a test to see how I could use it.

Debugging Code
After walking thought the colored messages for a few minutes I had solved the issues at hand, opened Xcode and fixed the problematic code then progressed to the next issue.  Within six minutes I had corrected the code and ran Clang Checker again. This time no Safari, no report, and no information. What did I do wrong I started to wonder.  I looked at Clang Checker’s output in Terminal and saw that no reports were generate because there were no errors in the code. I did it -- I had debugged code all by myself with the help of Clang Checker. A feeling of accomplishment washed over me for being able to download, install, run, debug and compile without error.  Thank you Clang Checker... Now about that long command...

Simplifying the Running of the Script
I asked Don if there was something I could do to reduce the footprint of the command that I had to enter as it’s was the same command and I was not using all of Clang Checker’s switches. I wanted a alias, much like the shortcut to the path were the compiler now lives. Don suggested creating a shell script.  This did not sound good, easy or fun. But it was simple. I opened pico again and typed the following:


scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator2.2

I then saved the file as “clang” in the current directory.  To run clang I just need to have that file in the current build directory, have a clean project, open Terminal to that folder and type “./clang” to run the build script.

Final Thoughts
I’ve only been using it for the past few days but I’ve already developed a fondness for it. Remember that Clang Checker is in it’s infancy and is still being developed and as such is prone to erroneous message such as false positives. I have only heard this and have not experienced this but I assume that this in working on complex code implementations of the language your working on or having a very large code base for it to process.

I hope you found this information as informative as I have and that you try Clang Checker. I’d be happy to hear your thoughts or experiences on this matter so please, comment bellow. Cheers!

No comments: