A Note About the Clang Static Analyzer

/ 23 November 2009 / Alexander Celeste

One of the key new features in the latest release of Xcode (v. 3.2.1) is the built-in Clang Static Analyzer. For those that don’t know, the Clang Static Analyzer has been around for some time as an extra tool developers can install. The extra layer it provides on top of the normal errors and warnings of Xcode is that it will look at your code, without executing it, and go down every possible path to find memory leaks or any other kind of logic error that may be in your code and easily missed when reading the code as a human. In my personal opinion every developer should regularly use the Clang Static Analyzer, which is why it was so nice when Apple included it as part of Xcode a little while back (I’d used the standalone version of the analyzer once). Only today did I finally get the built-in analyzer to properly function.

It took me so long to figure it out that I thought I should explain it here: With the default settings the “Build and Analyze” menu item in the “Build” menu won’t work (I kept trying this for weeks with no results). Nor will setting the active compiler in project settings to Clang LLVM 1.0 and using the menu item. Instead you can keep the compiler at the default GCC 4.2 and just check the Run Static Analyzer checkbox in project settings. Now each time you build your code (no need to build and go) it will be analyzed and the output embedded in the source code windows the same way that the errors and warnings are.