Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the GCC __unused attribute with Objective-C

Is it possible to use the __unused attribute macro on Objective-C object method parameters? I've tried placing it in various positions around the parameter declaration but it either causes a compilation error or seems to be ignored (i.e., the compiler still generates unused parameter warnings when compiling with -Wall -Wextra).

Has anyone been able to do use this? Is it just unsupported with Objective-C? For reference, I'm currently using Apple's build of GCC 4.0.1.

like image 603
Jason Coco Avatar asked Nov 12 '08 22:11

Jason Coco


People also ask

What is __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What is __ Attribute__ unused ))?

__attribute__((unused)) variable attribute Normally, the compiler warns if a variable is declared but is never referenced. This attribute informs the compiler that you expect a variable to be unused and tells it not issue a warning if it is not used.

What is werror in GCC?

-Werror= Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors.

How do I turn off GCC warning?

If the value of y is always 1, 2 or 3, then x is always initialized, but GCC doesn't know this. To suppress the warning, you need to provide a default case with assert(0) or similar code. This option also warns when a non-volatile automatic variable might be changed by a call to longjmp.


2 Answers

Okay, I found the answer... it appears to be a bug with the implementation of Apple's gcc 4.0. Using gcc 4.2 it works as expected and the proper placement is the following:

-(void)someMethod:(id) __unused someParam;

It's documented in the Objective-C release notes if anyone is interested: http://developer.apple.com/releasenotes/Cocoa/RN-ObjectiveC/index.html#//apple_ref/doc/uid/TP40004309-DontLinkElementID_6

As a note, your answer will compile, Louis, but as I stated in my question it won't actually do anything or suppress the unused warning issued by the compiler.

EDIT: I filed a bug report with apple for this rdar://6366051.

like image 157
Jason Coco Avatar answered Nov 08 '22 14:11

Jason Coco


A common idiom is to use the following:

#define UNUSED(x) (void)x
void SomeFunction(int param1, int param2)
{
  UNUSED(param2);
  // do stuff with param1
}

The UNUSED(param2) statement doesn't generate any object code, eliminates warnings about unused variables, and clearly documents the code as not using the variable.

like image 36
Adam Rosenfield Avatar answered Nov 08 '22 15:11

Adam Rosenfield