A few weeks ago, I discovered that the Build and Analyze in Xcode didn’t find as many problems as scan-build did directly when you turn all options on.
On the Clang analyzer site, they have instructions for setting up the latest analyzer so that Xcode would use it. Unfortunately, the settings that find some very common problems are off by default. Here are instructions for getting the Clang analyzer to run with those options on.
To make sure you are doing it right, make your project completely succeed a Build and Analyze, then comment out a release that you are doing in any dealloc. Rerun Build and Analyze — it should not find this problem.
Then,
1. If you don’t have it, download and untar the latest checker
2. Make a script file called full-analyze-clang and put it in the checker’s bin directory, with this code
#!/bin/bash CLANG="`dirname $0`/clang" CLANG_CMD="\"$CLANG\" -Xanalyzer -analyzer-check-objc-missing-dealloc -Xanalyzer -analyzer-check-objc-missing-dealloc -Xanalyzer -analyzer-experimental-internal-checks $*" eval $CLANG_CMD
3. From the Terminal, run this command to make the script executable
chmod +x full-analyze-clang
4. In the checker root directory, there is a script called set-xcode-analyzer. Run it like this:
sudo ./set-xcode-analyzer --use-checker-build=FULL_PATH_TO_CHECKER/bin/full-analyze-clang
replacing FULL_PATH_TO_CHECKER with the path to the checker directory where you untarred it in step 1.
5. To test, Run the Build and Analyze on the project you set up with the bad dealloc. If it finds 0 errors, this could be because something is wrong with the way the script is set up.
Important parts if this stops working with a future clang:
The set-xcode-analyzer expects a directory or a full-path to clang. It checks to see it has a full-path by looking for a path ending with “clang”, which works for us because our script’s name ends in “clang”. If they change this script in the future, this might not work
Build and Analyze just reports a successful build if our script reports errors — it’s just looking for the existence of result files. You might need to add logging lines to the script to figure out what’s going on. Log to a full-path because the current directory is inside Xcode’s path somewhere when the script is run.
Use something like this as your last line to debug:
eval $CLANG_CMD > `dirname $0`/cl.out 2> `dirname $0`/cl.err
Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.