Xcode 4: Generating Code Coverage Files with LLVM

With Apple transitioning from GCC to LLVM, one thing that was lost was support for code coverage, which measures how much of your code executed when running. But I discovered in a thread on Apple paid developer forums that LLVM now has code coverage support. Setting up Xcode to generate code coverage files is easier than it was in GCC.

Code coverage file generation from LLVM is currently available only for Mac applications.

Setting up Xcode for Code Coverage

To generate code coverage files, you must set the following Xcode build settings to Yes: Generate Test Coverage Files and Instrument Program Flow. These build settings are in the Code Generation collection. You must also generate debug symbols, which you can ensure by running with the Debug build configuration.

Creating and Finding the Code Coverage Files

Build the project and run your application to generate the code coverage files. Finding the code coverage files is tricky because they’re buried inside your build folder. You can find them in the following location:

Where DerivedData is the location of your derived data folder, Debug is the build configuration, and x86_64 is the architecture. You fill find two code coverage files for each of your source files: one with extension .gcda and one with extension .gcno.

Viewing Code Coverage Statistics

The last step is to view the code coverage statistics. I recommend using CoverStory. Choose File > Open in CoverStory. Select the folder containing your code coverage files and click the Open button. Next to each line of code is the number of times it executed. Lines of code that were not executed are highlighted in red.