Wednesday, December 30, 2009

Clang Static Analyzer for iPhone Programming


Scan Analyzer : Clang Analyzer LLVM Compiler


Overview:


While we may be familiar with using tools likes Instruments to find and fix memory leaks in our application, the Clang Static Analyzer takes a different approach to memory leak detection by compiling our Xcode project and scanning each method, class, loop and logic block for potential leaks.

We may heard of Clang Static Analyzer referred to by the name of command line tool used to run the analyzer:scan-build.


Requirements


Download the Clang/LLVM Static Analyzer from http://clang-analyzer.llvm.org/ and click the link for checker-NNN.tar.bz2 (Latest build (Universal binary, 10.5+): checker-230.tar.bz2 (built December 8, 2009) ).



Installation


1) We are going to install the check-NNN binary in Mac /usr/local/bin folder. Run the following command to verify that this folder exists:

sudo mkdir -p /usr/local/bin


2) Open Terminal.app from the Application folders, and move the contents of checker-NNN to the usr/local/bin folder(here NNN is 230 version no.)

sudo mv ~/Downloads/checker-230/* /usr/local/bin



Basic Usage


scan- build tests our code by compiling our Xcode project and studying it for defects during the build process. Initially, we are required to change some of the settings in our XcodeProject.

  1. Select the xcodeproject group, and click on Get Info.

  2. In General tabs, select “Base SDK” - “iPhone Simulator 3.0”

  3. In Build tab, select “Base SDK” - iPhonesimulator3.0” & Valid Architectures – i386.

  4. Make Code Signing Identity - “Don't Code Sign”

  5. Select “Any iPhone OS Device” - “Don't Code Sign”

    And then

To check our code, we just invoke scan-build from the command line at the top level of any one of your project directories.

  1. Still in Terminal.app, change into one of your project directories

  2. Run scan-build of your Xcode project.

scan-build -k -V xcodebuild -configuration Debug -s iphonesimulator3.0

Customizing your Output Report

After running scan-build for sometimes, the first thing you might want to do is tell scan-build to put its report in a different place. To do that, simply specify the output folder on the command line like so:

scan-build -o /custompath/for/report xcodebuild -configuration Debug -s iphonesimulator3.0


  • If you are using iphone SDK 3.0 on Snow Leapord, then Clang Static Analyzer is coming in-built in the IDE.

It will be great to use it for better programming analysis to judge the memory management.

Thanks

No comments:

Post a Comment