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.

- (BOOL)getSomeValue:(int)x
  BOOL positiveFlag;
  if (x < 0)   
   positiveFlag = NO;   
  else if (x > 0)
    positiveFlag = YES;
  return positiveFlag;

If you look in the Build Results window, you’ll see more information:

  • Variable ‘positiveflag’ declared without an initial value
  • Uninitialized or undefined value returned to caller

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:


  1. 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?

    (typo btw: positiveFlag => positiveflag)

  2. Thanks for the intro! I’ll implement static analysis into my builds from now on.

  3. 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!

  4. 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?

  5. wic,

    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).


    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).

    Hope that helps.


  6. John,

    Yes I ran your sample code as copied from above. Weird, it works in one of my projects, but not the one I first tested. Must be some setting that differs between them I guess.

  7. wic,

    if you get some time, it would be interesting to know if you can figure out what the difference is in the projects, as I’m sure this will come up again.


  8. John,

    I think I figured it out.

    In my other project, I’m mixing in some C++ code. That is, some of the files contains C++ and are named *.mm. If I try to analyze them I get no response, but it works correctly for all *.m-files.

    From the Build Results output window, it seems like Xcode simply skips the analyze step for all *.mm files.

    Apparently there is no support for clang with C++ in Xcode. :-(

  9. How do you set the default compiler to LLVM-Clang? I know how to do it per project, but what if I want to do it across the board?


  10. Cheers John, one of the best articles I’ve come across in months. Clang is top of my toolbox.

  11. The tool is an excellent tool. However, I believe that I found an instance where it gave an erroneous warning.

    -(unsigned long long) computeEntryValue
    NSUInteger length= [self.entryString length];
    NSUInteger digit;
    NSUInteger i;
    // declared and initialized here
    unsigned long long evalue=0;
    for(i=0;i= ‘0’ && c = ‘A’ && c <= 'F')
    digit = 10+c-'A';
    evalue = evalue * base + digit;
    return evalue;

    The analyzer says that evalue is undefined. However, digit is uninitialized this should have been detected.

  12. Something doesn’t seem right – there is only one opening { and two closing } . Did you copy the code directly from a code block that compiles?

  13. Hi John,

    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, and keep up the great work.

    • Casey, you may already know this, however….you can touch all the files in a project by choosing the option ‘Clean’ in the ‘Build’ menu. I do this often, clean and rebuild.

      Anyone else have any insight on Clang LVM ??


  14. Hi John,

    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.


  15. I have written a simple code to check xcode static analyzer.

    char b[15];

    char a[50]=”aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”;

    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?

  16. Great Article… I simply cannot believe I didn’t know about this before it’s insanely faster than GCC.

  17. It’s a shame, CLANG can’t find the memory leak in this code:

    NSMutableString *theString = [[NSMutableString alloc] init];

    for (int i=0; i<100; i++)
    [theString appendFormat:@"%d ",i] ;

    NSLog(@"theString is: %@", theString);

    Not really sure why. I never release theString. This is caught easily by the Instruments Leaks tool, but CLANG doesn't catch it.

  18. 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.

Comments are closed.