I like to eliminate all compiler warnings before checking code into source control. I find when using the LLVM based compiler with Xcode that warnings generated by the Clang front end are pretty good and that most times I really do need to fix the code. Even when the warning may be considered questionable I still like to eliminate it so that I do not start to ignore compiler warnings.
Having said that there can be times (when playing with beta versions of a framework for example…) when you cannot easily modify code to eliminate a warning. In those situations I find it better to explicity disable the warning for the relevant section of the source code.
Clang Compiler Diagnostics
If you are using the LLVM-GCC or Apple LLVM Compiler build option there are a large number of possible compiler warnings that you can enable/disable. The Clang front end also supports the GCC diagnostic warnings) for backwards compatibility.
At the time of writing I am using Xcode 4.1 which exposes some of these compiler settings in the Build Settings tab of the project/target:
One quick tip in passing if you have the Xcode Utilities tab open and showing the quick help it will display a detailed description of the compiler option when you click on the setting:
You can globally decide to disable a compiler warning by adding/removing a compiler flag in the build settings for the project or target. However I think the better approach is to selectively disable the warning only for the relevant section of code. To illustrate how to do that consider the warning you get if you have an unused variable in a section of code (this is only an example, in practise you should probably just delete an unused variable):
If you look at the detailed compiler log you can see the command-line option that is responsible for generating the warning:
RootViewController.m:50:9: warning: unused variable 'a' [-Wunused-variable,10] int a; ^ 1 warning generated.
Of course you can disable the unused-variable option in the build settings but to disable it in just this section you can surround it with some clang compiler pragmas as follows:
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" int a; #pragma clang diagnostic pop
The pragma clang diagnostic push and pop lines save and restore the current compiler diagnostic settings. This ensures that the compiler will continue with our usual compiler options when compiling the rest of this source file. The clang diagnostic ignored statement takes the compiler command-line flag that you want to ignore.
You can find a lot more details on controlling the Clang compiler diagnostics via the command line or with pragmas in the Clang User Manual.