views:

1474

answers:

5

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.

+2  A: 

I can compile the following just fine:

- (NSString *) test:(__unused NSString *)test {
    return nil;
}

Edit: Actually, that may not be strictly an arch thing:

Phoenix-VI:CouchPusher louis$ cc -c Pusher.m -Wall -Werror
Phoenix-VI:CouchPusher louis$ cc -c Pusher.m -Wall -Werror  -Wunused-parameter
cc1obj: warnings being treated as errors
Pusher.m:40: warning: unused parameter ‘test’
Phoenix-VI:CouchPusher louis$

So -Wall does not include not include -Wunused-parameter....

Louis Gerbarg
Yeah, I can compile that too, but try gcc -Wall -Wextra and see what happens :)
Jason Coco
No, -Wall doesn't, you need to use -Wextra for that... but I always use both -Wall and -Wextra on my code and I get annoyed with the unused warnings noise.
Jason Coco
I use -Werror and most warnings, but I have a few specific warnings I traditionally turn off -Wunused-parameters being one of them, since delegates tend to often have unused parameters that cannot be removed.
Louis Gerbarg
+4  A: 

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.

Jason Coco
+2  A: 

I think you can use the #pragma unused to mark arguments as unused. Untested, but you can try something like

- (NSString *)test:(NSString *)test {
#pragma unused test;
  return nil;
}
Kevin Ballard
You need to enclose the variable in parens for #pragma unused. So: #pragma unused(timer) does work but I don't really like the way that works. It turns out it's a bug anyway.
Jason Coco
+2  A: 

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.

Adam Rosenfield
Actually, in this case, with -Wall -Wextra you end up with a statement with no effect warning :)
Jason Coco
Fixed - casting to void gets rid of that warning
Adam Rosenfield
A: 

After fighting with the #pragma for a while, I discovered it's

+ (NSString*) runQuery:(id)query name:(NSString*)name options:(NSDictionary*)options
{
#pragma unused(name)
 ...

}
alecf
Yeah, I really wanted to put the unused keyword in the method line, though. For some reason, I feel like it "belongs" there. Anyway, according to Apple it is simply not supported on GCC before version 4.2, so that's that. Otherwise, if using 4.2 or above, it works as you would expect :)
Jason Coco