Running Custom Clang Analyzer Builds

I don’t remember where I first saw this mentioned so I cannot give proper credit but this is an interesting tip to try if you have five minutes.

Open Source Builds of the Clang Analyzer

The Clang Static Analyzer has long been integrated with Xcode and provides powerful source code analysis to detect bugs in C, C++ and Objective-C code. The analyzer is fully open source and part of the larger Clang project. This means that you do not need to wait for Apple to release a new version of Xcode to get the latest updates to the analyzer.

You can find full details on downloading the latest build from the Clang Static Analyzer site. The download is a pre-built binary so installation only requires you expand the package and copy it to your preferred destination.

At time of writing the latest build is checker-276.tar.bz2 (built February 19, 2014). The release notes provide details on what is new and can make interesting reading. The downloaded file expands into a directory named checker-276. You can place this file any where you choose and since it is self-contained deleting the directory is sufficient to uninstall it. For the purposes of this post I am going to place the analyzer files in /usr/local/checker-276.

Using the build with Xcode

The Clang Analyzer site has full details on running the analyzer within Xcode. The build directory contains the set-xcode-analyzer utility that allows you to set which version of the analyzer Xcode should use. It needs to be run under sudo as it modifies the Xcode application bundle. Also make sure you have exited from Xcode before running set-xcode-analyzer.

Set Xcode to use the custom analyzer build

$ cd /usr/local/checker-276
$ sudo ./set-xcode-analyzer --use-checker-build=/usr/local/checker-276
(+) Using Clang bundled with checker build: /usr/local/checker-276
(+) Searching for xcspec file in:  /Applications/Xcode.app/Contents
(+) processing: /Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/Clang LLVM 1.0.xcplugin/Contents/Resources/Clang LLVM 1.0.xcspec

Reset Xcode back to using the default Clang Analyzer

$ cd /usr/local/checker-276
$ sudo ./set-xcode-analyzer --use-xcode-clang
(+) Using the Clang bundled with Xcode
(+) Searching for xcspec file in:  /Applications/Xcode.app/Contents
(+) processing: /Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/Clang LLVM 1.0.xcplugin/Contents/Resources/Clang LLVM 1.0.xcspec

Why Do It?

The open source build is updated frequently with bug fixes and extra checks. This makes it more likely to spot an error in your code that would go undetected with the Xcode bundled version. On the other hand some of the extra checks may be still be very experimental leading to a lot of false warnings.

As an example I tried running the downloaded build against a sample project (Product > Analyze ⇧⌘B) and it produced the following warning:

warning: The 'viewWillAppear:' instance method in UIViewController
subclass 'UYLViewController' is missing a [super viewWillAppear:] call

Missing the call to super from one of the view(Did/Will)(Appear/Disappear) methods is a common mistake that is not currently detected by the analyzer bundled with Xcode (I am running Xcode 5.1.1). So given that it only takes two minutes to download and configure Xcode and painless to switch back I would say it is definitely worth trying on your own code.