Static Code Analysis (Clang) and Xcode 3.2

With the release of Xcode 3.2 (available with Mac OX 10.6 – Snow Leopard) you can easily run a static analysis check on your code, thanks to integration of Clang. If you’ve worked with Clang prior to its integration of Xcode, you already know just how cool this is for . If you are new to Clang, you owe it to yourself to check it out.

Static analysis does a deeper parsing of your code than a traditional compilation. It looks through each potential code path and can find potential problems that are not obvious to the compiler (and may be missed by you as well).

How it Works

The code below compiles just fine, no warnings. However, Xcode 3.2 will flag a few potential problems when running an analysis of the code. To run the analyzer, from the Build menu, choose Build and Analyze.

The problem occurs in this code if the value of x == 0, given the variable positiveflag has no default value set, it may not return what you expect.

Showing Control Flow

Here’s an interesting option you have within the integration of static analysis in Xcode – click on one of the entries in the Build Results window (as shown below):

and Xcode will highlight the flow of the code, and help you pinpoint the potential problem:

Run Static Analysis with Each Build

By default, you request a static analysis of your code by selecting Build and Analyze in the Build menu. If you would like a static analysis run whenever you build a project with the debug configuration, you can check the Run Static Analyzer in the Build Options as shown in the image below:

22 comments

Thanks for the tip, it should be useful. But I tried it as you suggested with “Build and Analyze”, but got no analyzer results at all. Is there anything else one needed to get it running, except of course latest Xcode?

what do you have to enable to get the arrows? – ihave xcode 3.2 on SL, i have set the compiler version to CLang LLVM and copied your example code into the project i’m working on but i never see any arrows – no matter how many times i click on build an analyse!

What is the command-line equivalent of using the checkbox? For those of us using systems like Jam (because of cross-platform development). Is it a flag to the compiler, or a separate executable to be run?

Do you have sample code such as what I listed that has something the analyzer would detect? (if there are no problems, I believe no messages will be returned).

bg,

I didn’t have to set the compiler version – with Xcode 3.2 you should be able to select Build and Analyze and any message appear in the Build Results window. You have to then click on the little arrow to expand the results, and then select one of the errors (see the figure above).

Thanks for the article. Clang appears to be a great tool and I would love to add it to my kit, but I’ve run into a few problems:

When I try to set the C/C++ Compiler Version in my Project Settings to Clang LLVM 1.0, I get the error “Library not found for -lgcc” when I attempt to build in Xcode. When I use the system default compiler (GCC 4.2), I can get static analysis to work on a per-file basis when I Build & Analyze. I would like to rebuild and analyze all files with Clang, but I’m guessing that I need to Touch each file to have Xcode include it in the build process.

Are there benefits to using Clang LLVM 1.0 as the compiler, or is using GCC and leaving “Run Static Analyzer” checked in the Project Settings enough? Any advice would be appreciated.

Thanks for the tip! I suspected that Clean had that effect, but I’m glad to hear it confirmed. Unfortunately, after running Clean I no longer get any static analysis results in the Build Results window when I think I should (ex: synthesized properties not released in dealloc). I made sure that “Run Static Analyzer” is enabled in the Project Settings, and this seems to happen regardless of which compiler version I’m using. I’m not sure how I could have broken it, but you never know. Thoughts? I hope I can get this working; the more insight I have into my code, the better.

Surprisingly the xcode unable to detect simple buffer overflow issue. I tried with GCC 4.2, LLVM GCC 4.2 and clang LLVM 1.0. No issue detected. Is these any bench mark available for xcode analysis. How much effective is this?

For anyone having problems getting Build and Analyze to work correctly in XCode, try setting it build for Device rather than Simulator. Apparently there’s a bug for versions of XCode > 3.2.3 meaning that this only works for Device.

You don’t need a device connected, just make sure the drop down says Device.