Contact

Please feel free to email me:

fraser@speirs.org

Search
My Stuff
Navigation
Friday
Apr032009

Git pre-commit hooks and the Clang Static Analyzer

For the solo developer, it's easy to kid yourself into believing that you'll run those tests "when appropriate". It never really happens, does it?

I've always liked the Clang Static Analyzer as a tool for finding code oversights, but I've always found it a bit annoying to run. I always forgot the options, or something didn't quite work as expected. That's really my fault, rather than CSA's.

Anyway, it occurred to me that I could use my tools to keep myself more honest about regularly running the static analyzer. I use Git for source control and Git provides a pre-commit hook feature. The idea of pre-commit hooks is that you get to run a script before the commit happens. Depending on the result code of the script, the commit will either proceed or be aborted.

I wrote a wrapper around the scan-build tool, so that I could run the analyzer by hand with my preferred options at any time:


#!/bin/sh
~/Scripts/clang/latest/scan-build --status-bugs \
-warn-objc-missing-dealloc \
xcodebuild -sdk iphonesimulator2.2.1 -configuration Debug


The --status-bugs flag is the trick here: it makes scan-build return a non-zero status code if it detects bugs. That's what we want with Git pre-commit hooks: a non-zero status indicates a possible bug, and that causes the Git commit to be aborted.

Also, I think I'm right in saying that you can't run the static analyzer on ARM code, hence the use of the iPhone Simulator SDK.

The pre-commit hook is a script at .git/hooks/pre-commit in your repository. There's an example file in every .git/hooks that you can copy and edit. Because I wrote my wrapper script above, the pre-commit is trivial:


[(master)]$ cat .git/hooks/pre-commit
#!/bin/sh
./clang.sh


The working directory for the pre-commit script is the parent directory of your .git directory.

[Update: Peter Hosey tells you how to do this for Mercurial]

Reader Comments (21)

Nice idea. Do you find this slows commits down to unacceptable levels now though?

April 3, 2009 | Unregistered CommenterJonathan Wight

This does seem to be a reasonable way to keep you honest, but I could also see this negatively affecting your use of the repository. What happens when you are done for the day, you go to commit and the commit fails because of bugs in the code? You either sit down a fix them (not really an option if there was a reason you called it quits), remove the hook (a pain, and you just subverted your test), or you just don't commit (easiest, but also the most dangerous. Personally, I believe in very frequent commits, and I'm leery of anything that would break me of the habit. I'd also argue that the point when you go bug hunting is precisely the point you want a current copy in the repository in case you break something in the process and need to revert.

April 3, 2009 | Unregistered CommenterBlack

I'd offer the addition of '-k -V' to the scan-build options, so scan-view will automatically launch in your browser of choice if bugs are found.

In other words:

~/Scripts/clang/latest/scan-build --status-bugs \
-warn-objc-missing-dealloc \
-k -V \
xcodebuild -sdk iphonesimulator2.2.1 -configuration Debug

April 3, 2009 | Unregistered CommenterOwain Hunt

I agree with Black, there are plenty of times when you want to commit something you haven't finished work on. It's a bit like setting your tests to run when you build - a nice idea, but in practice, I always end up turning it off.

What would make more sense to me would be to automatically scan-build pre-push, as in, before you share your changes with your team / the world.

April 3, 2009 | Unregistered CommenterBen Copsey

Thanks for writing this up. I've been meaning to try Clang Static Analyzer, integrating it with git seems like a good idea.

April 5, 2009 | Unregistered CommenterMarc Charbonneau

I've used Clang Static Analyzer for quite some time, and it has saved me numerous memory leaks (and I regard myself as an experienced Cocoa programmer).

I've incorporated it into my project by making a Shell Script Target. This way I dont have to remember all the parameters, and I can run it at my leisure simply by right-clicking the target and select Build.

April 7, 2009 | Unregistered CommenterJoachim Bondo

The problem with adding -warn-objc-missing-dealloc seems to be that it turns off the other checks (running it with -h also says that). When I run it without that option, I get a bunch of warnings that I don't get with it. Is there a way to specify all checks without having to list them separately?

April 7, 2009 | Unregistered CommenterRobert Kosara

Another problem with -warn-objc-missing-dealloc is that it won't find releases that are performed outside dealloc, say if your dealloc contains a line [self releaseOutlets];. I'd expect a static analyzer to be able to follow a simple message call, but apparently not. Are the semantics of Objective-C such that this is impossible?

The solution is to use an ugly macro.

April 10, 2009 | Unregistered CommenterSteve Weller

Everyone has completely different tastes,michael kors wedges specially when you are their fashions.michael kors watches on sale It is make pe rfect sense how they would each prefer an unnaturally different sorts of watches michael kors handbags on sale. However, you have to features that almost most people enjoy to build to the wrist watch michael kors clothing. These features are true “must haves” that any watch designer or manufacturer requires to consider michael kors hamilton when generating any kind of distinctive line of watches for just a public.

June 27, 2011 | Unregistered Commenterqwaszx123

Very valuable information, I am very grateful

Great post. It appears that most of the steps are relying on the creativeness factor….

You managed to hit the nail upon the top as well asand alsoand defined out the whole thing without having side effectside-effects , people cancould take a signal. Will likelyprobably be back to get more. Thanks

July 19, 2011 | Unregistered Commentersupra tk society

Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some nice practices and we are looking to swap solutions with others, be sure to shoot me an email if interested.

July 19, 2011 | Unregistered Commentercheap jordan shoes

There are many online stores having Nike Air Max pas cher, i believe you gonna like it!
Do you have a pair of chaussure Nike Air Max now? if not, go to online store and have one, it is so amazing!!!

July 20, 2011 | Unregistered Commenterbuy nike air max

very great site you have created. I enjoyed reading this posting. I did want to issue a remark to tell you that the design of this site is very aesthetically pleasing. I used to be a graphic designer, now I am a copy editor for a marketing firm.

July 20, 2011 | Unregistered Commentercheap radii shoes

One of the best blogs I have ever come across. Thanks for putting this together and sharing with us.

Nice to be visiting your blog again, it has been months for me. Well this article that I’ve been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.

July 22, 2011 | Unregistered CommenterPrada handbags 2011

I am very happy to read your blog.Thank you for sharing this article.It is great! Thanks for sharing this nice post.I will keep your article in my idea.

July 23, 2011 | Unregistered Commenterdiscount gucci bags

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for?

July 23, 2011 | Unregistered Commentersac lv

jordan fusion new releases
jordan fusion wristbands
Air Jordan 6 Max Fusion
Air Jordan 11 Max Fusion
Air Jordan Fusion 4
louis Vuitton True Leather Handbags
jordan fusions new releases
jordan fusion ones
real louis vuitton handbags
jordan fusion obama
jordan fusion royal
Authentic Louis Vuitton
jordan fusion lifestyle
jordan fusion logo
air jordan fusion 12
jordan fusion june 14
Jordan Fusion 5
jordan fusion nike
jordan fusion two
air jordan fusion myspace layouts
air jordan fusion retro basketball shoes
new jordan fusion 09
jordan 12 fusion
jordan fusion nike shoes
Air Jordan Fusion 6
new jordan fusion 3
Jordan Fusion 3
nike air jordan fusion kids
jordan fusion retro 8
authentic louis vuitton watches
jordan fusions lime green
jordan fusion releases
jordan fusion sale
louis vuitton hats
jordan fusion v sneaker
jordan fusion premier
custom jordan fusion kicks
air jordan fusion one
jordan fusion max
jordans for sale
jordan fusion 15
jordan fusion v cactus
jordan fusion shoes cheap
jordan fusions womens
jordan fusion 3 premier
jordan fusion og jacket
jordan fusion 20 low
jordan fusion new release
jordan fusion wholesale
jordan fusion 25
jordan fusion viii
cheap jordan sneakers
cheap jordan fusion shoes
air jordan fusion websites
jordan fusion paypal
air jordan fusion xi
air jordan fusion wholesale
jordan fusion pe
Air Jordan Fusion 9
jordan fusion sneakers
air jordan fusion 3
jordan fusion list
jordan fusions premier
jordan fusion myspace layouts
nike jordan fusion two
Jordan Fusion 8
jordan fusion white
jordan fusions newest ones
official jordan fusion website
air jordan fusion new
jordan fusion vi
Jordan 2011 Max Fusion
air jordan fusion mid
jordan fusion v 5
jordan fusions white blue trim rainbow sole
jordan fusion xl
Louis Vuitton Men Low Shoes
brown jordan fusion outdoor furniture
air jordan fusion nine
air jordan fusion playoff
jordan fusion xxiii 23
air jordan fusion xiii
authentic LV Scarves
jordan fusion shirts
jordan fusion olive/orange
jordan fusion v wine
jordans fusion mid 13
Air Jordan Fusion 12
jordan fusion release dates 2011
jordan fusion 12 sneaker
jordan fusion youth
mens louis vuitton sunglasses
jordan fusion 20
jordan fusion three
jordan fusion 3 varsity
jordan fusion low top
jordan fusion
jordan fusion with air force ones
Jordan 12 Max Fusion
jordan fusion 07
jordan fusion release dates
jordan fusion layouts
Air Jordan 13 Max Fusion
jordan fusion rainbow
air jordan fusion jacket
air force jordan fusion new
jordan fusion xx for women
jordan fusion myspace
louis vuitton wallets
jordan fusion warehouse
jordan fusion titanium
air jordan fusion vi
louis vuitton tie clip
jordan 5 fusion
jordan fusions 06
jordan fusion pink
Air Jordan Fusion 20
wishesg 20 jordan fusion men shoes info
air jordan fusion t shirt
jordan fusion kicks
jordan fusion quarter sock
jordans fusion mid 3
jordan fusion shoes sneaker
Jordan 3 Max Fusion
jordan fusion real
jordan fusion xii
authentic jordan fusion v
mens louis vuitton sunglasses
jordan fusion prices
jordan fusion 10
jordan fusion mix
jordan fusion 11
air jordan fusion 3 shoes
Air Jordan 9 Max Fusion
jordan fusion tags
jordan fusion 2009
jordan fusion red
air jordan fusion release dates
jordan fusion 24
jordan fusion peel
louis vuitton tie clip
jordan fusion 22
air jordan fusion 23
jordan fusion low
jordan fusion retro 5
jordan fusion 25 & jordan 6 men shoes
jordan fusion 05
jordan fusion on sale
Jordan Fusion 13
jordan fusion release dates 2010
jordan fusion new
air jordan fusion youth
air jordan fusion kids
jordan fusion 09
jordan fusion plaid

July 26, 2011 | Unregistered Commenterjordans for sale
Editor Permission Required
You must have editing permission for this entry in order to post comments.