views:

739

answers:

3

I am just starting out with xcode and learning Objective-C & Cocoa, would it be helpful to swap the compiler in Xcode 3.2 to Clang for the enhanced error checking and static analyser. I am just curious if the extras given would be helpful or would my learning be better served sticking to the default settings?

many thanks

gary

+2  A: 

If you "Build and Analyze" or turn on the "Run Static Analyzer" preference in XCode, you get Clang's static analysis.

If you're not doing C++ development, there doesn't appear to be much downside to switching from gcc to Clang. You just don't need to do so to get the benefits of Clang's static analysis.

Terry Wilcox
Are you sure you get nicer error messages using "Build and Analyze" under GCC 4.2, I don't see that here. Also for iPhone, you need to be using GCC?
fuzzygoat
You're right, you still get gcc's obtuse error messages. I changed my answer accordingly. And while I'd swear I saw an official doc that said no Clang-LLVM for iPhone, I can't find it, so try it out. I've switched to CClang-LLVM for my Mac code.
Terry Wilcox
+1  A: 

You can use the static analyser without using the actual Clang compiler. Generally you wouldn't use the analyser as your default build option as it slows build times a fair bit.

However, the Clang compiler is a good option even without considering static analysis. It has much better error reporting as you point out, but the main advantage is that it's very fast, in fact in my testing it's almost twice as fast as GCC.

Rob Keniger
Just for reference, there would be no difference in the code or the project with regard which compiler you have set. If you find somethings not working out I would assume its just a case of defaulting it back and recompiling?
fuzzygoat
@fuzzygoat: That is correct. You just choose a different compiler and rebuild the project. So which one you choose as your default isn't very important.
Amuck
+2  A: 

There are a few options for using CLANG:

  1. Run build and analyze, to see clang results.

  2. In project settings for a configuration, you can check the "run static analyzer" box and CLANG will be run each time you compile.

  3. You can also change the compiler from GCC 4.x to CLANG/LLVM. That displays more errors and CLANG warnings (one example is that NSLog(@"A value is %@:%@", value) will warn you you are not passing in enough parameters).

I would recommend option 2 - if you run the static analyzer with each build, it doesn't take much more time and you'll find out if you are doing something odd right away, rather than fixing a bunch of errors later. The clear messages it give you actually provide a lot of educational value, so it makes even more sense to turn it on if you are just learning!

Option 3 is good to run from time to time because of the greater error checking. However, currently XCode has no CLANG/LLVM compiler for the iPhone device, and so you can only use that setting when compiling for the simulator. The best approach then is to create a copy of the debug configuration (in the project settings configurations tab) named "Debug CLANG", and set that configuration to use the CLANG/LLVM compiler and run the static analyser. Note that after you create a new configuration you have to back out of settings, select it as the active configuration (via the top right dropdown) and then go back in to project settings to edit the values.

Then in day to day use you simply use the "Debug" setting to compile using the static analyzer, and switch to "Debug CLANG" perhaps once a week to see if any deeper problems exist.

Kendall Helmstetter Gelner