Ipphones

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 12 February 2009

CLANG Static Analyzer

Posted on 06:04 by Unknown
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.
Email ThisBlogThis!Share to XShare to Facebook
Posted in Clang Static Analyzer, Performance | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Making OpenGL ES Screenshot
    The Bit-101 Blog has an entry that shows how to take a screenshot when using OpenGL ES . I tested this in my much-delayed particle-generato...
  • Adding CLANG to Your Build Process
    Frasier Spiers has a nifty piece this morning on using Git pre-commit hooks to automatically run the CLANG Static Analyzer. I'm not a G...
  • 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...
  • A Little Help
    I'm having a problem with OpenGL ES, and it's keeping me from finishing my particle engine post. I was hoping someone here could see...
  • WWDC Accommodations
    Staying downtown in San Francisco is very expensive in the summertime. Bu, if you're going to WWDC, you really want to stay downtown. Yo...
  • Xcode File Templates and a Mystery
    One of the things that confuses many newcomers to Xcode is how to set it up so that your company name gets automatically filled in when you ...
  • Brain Surgery?
    Craig Hockenberry has an interesting post on his blog today about the iPhone background processing issue. Craig speaks from personal experi...
  • Book's Almost Done
    I just finished Chapter 16. I'll give it another read-over in the morning then it will go off to my writing partner for his review, then...
  • iPhone Alley
    Looks like Dave and I are going to make an appearance on the iPhone Alley Podcast next week. We're recording on Sunday night, so I woul...
  • Shuffling Arrays
    Ever want to randomize an array of items? It's a task that, for some reason, I've had to do a lot in recent programs. So, I wrote a ...

Categories

  • 3D Models
  • Ad Hoc Distribution
  • ADC
  • Address Book
  • Amazon
  • Anaglyphs
  • App Store
  • Apple
  • Apple DTS
  • Apple Store
  • Application Store
  • articles
  • Award
  • Background Processing
  • Barcodes
  • Beta
  • Blog
  • Blogger
  • Blogging
  • Blogs
  • Blogspot
  • Book project
  • Bug Reporting
  • Captain Obvious
  • Categories
  • Censorship
  • CFFoundation
  • CGAffineTransform
  • Clang Static Analyzer
  • Cocoa
  • Cocoa Touch
  • Code Reuse
  • Code Signing
  • Computer
  • conferences
  • Controller Classes
  • Core Animation
  • Daring Fireball
  • Database
  • Debugging
  • Defect
  • Delegates
  • Design Awards
  • Developer Certifications
  • Discussion Forums
  • Edit Mode
  • employment opportunities
  • Encryption
  • Enterprise
  • Errata
  • free code
  • Free software
  • Full Screen
  • Game Programming
  • Gestures
  • Getting Started
  • goof
  • Google Code
  • Google Maps
  • Gotcha
  • Help
  • HIG
  • HTTP PUT
  • Idiots
  • Idle Timer
  • Images
  • Instruments
  • Interface Builder
  • iPHone
  • iPhone Applications
  • iPhone Dev Center
  • iPhone Developers
  • iPhone OS 3.0
  • iPhone SDK
  • iPhone SDK PNG
  • iPhone Simulator
  • iPhoneSDK
  • iPod
  • Job Opportunities.
  • k
  • Key Value Observing
  • Keynote
  • KVO
  • Landscape Mode
  • Learn Cocoa
  • Learn Cocoa on the Mac
  • libxml
  • Licensing
  • Mac Developers
  • Mac OS X
  • Macworld Expo
  • Microsoft
  • NDA
  • NeHe
  • New Category
  • New Release
  • NSFileHandle
  • NSMutableArray
  • NSMutableURLRequest
  • NSXML
  • Object-Oriented Design
  • Objective-C
  • Open Source
  • OpenGL ES
  • Optimizations
  • Other blogs
  • Paired Arrays
  • Parsing
  • Particle Engine
  • Party
  • PeopleSoft
  • Performance
  • Persistence
  • Pink Screen of Death
  • Piracy
  • Pixar
  • Podcasts
  • Press Release WTF
  • Press Releases WTF
  • private APIs Google
  • Project Template
  • Properties
  • Random Numbers
  • Rant
  • Rejected
  • Resources
  • Responder Chain
  • REST
  • Reverse Engineering
  • Rumors
  • Runtime
  • Sample Code
  • Screencast
  • screenshot
  • Scroll Views
  • snippet
  • Snow Leopard.
  • SOAP
  • Sockets
  • Source
  • Splash Screen
  • SQLite
  • SQLitePersistentObjects
  • Steve Jobs
  • Steve-Note
  • Strings
  • Stupidity
  • Subversion
  • Table Views
  • Taps
  • Template
  • Tip
  • Tips
  • Tririga
  • tutorials
  • Twitter
  • UIAlertView
  • UIColor
  • UIImage
  • UIPickerView
  • UIScrollView
  • UITextField
  • UIView
  • UIWebView
  • Update
  • Utilities
  • UUID
  • Vacation
  • Version Control
  • Web Services
  • Writing
  • WTF
  • WWDC
  • Xcode
  • XML

Blog Archive

  • ▼  2009 (141)
    • ►  May (14)
    • ►  April (30)
    • ►  March (48)
    • ▼  February (26)
      • 360 iDev Conference, T-Minus 18 hours
      • Mapping Directions from your App
      • Reusable Code in Google Code
      • Alert View with Prompt
      • Editable Select List
      • BWToolkit
      • Asynchronous Downloading of Images in a Table View
      • Rogue Amoeba is Hiring
      • Cross Development Hint
      • Making OpenGL ES Screenshot
      • Programmatically Drawing Gloss Gradients
      • All Generic Controllers to Date (Zip File)
      • Generic Selection List Controller
      • Computer Books & the Economy
      • What Are All the Cool Kids Doing These Days?
      • 360 iDev Conferences
      • Strong Encryption for Cocoa / Cocoa Touch
      • CLANG Static Analyzer
      • Long Text Generic Editing Controller
      • Same but Different
      • Accessorizer
      • Gameplay Videos
      • KVO and the iPhone SDK
      • Learn Cocoa
      • SQLitePersistentObjects Update
      • Longer Spinning & Blurring v2.0
    • ►  January (23)
  • ►  2008 (163)
    • ►  December (46)
    • ►  November (25)
    • ►  October (44)
    • ►  September (2)
    • ►  August (5)
    • ►  July (2)
    • ►  June (9)
    • ►  May (2)
    • ►  April (11)
    • ►  March (17)
Powered by Blogger.

About Me

Unknown
View my complete profile