9

Using the -Wunused-parameter flag, you can enforce __unused for unused parameters, as a compiler optimization. The following code causes two warnings:

#include <stdio.h>
int main(int argc, char **argv) {
  printf("hello world\n");
  return 0;
}

These warnings are fixed by adding __unused the unused arguments.

#include <stdio.h>
int main(int __unused argc, char __unused **argv) {
  printf("hello world\n");
  return 0;
}

When you use a parameter marked __unused, clang 4.1 does not warn or error.

#include <stdio.h>
int main(int __unused argc, char __unused **argv) {
  printf("hello world. there are %d args\n", argc);
  return 0;
}

The same behavior is exhibited using __attribute__((unused)).

int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv) {

Is there a way to warn or error on __unused? What would happen should you accidentally leave an __unused on a used parameter? In the above example argc appears to have the correct value, although it could be the compiler not taking advantage of the hint, and I wouldn't rely on this behavior without more understanding.

wjl
  • 7,143
  • 1
  • 30
  • 49
  • I've substantially edited the question to use C sample code instead of Objective-C to demonstrate that this is a C language question. Also, noted that the flag is -Wextra instead of -Wunused. – wjl Jan 24 '13 at 23:29
  • 1
    It's actually the `-Wunused-parameter` flag that's relevant here. The `-Wextra` flag enables `-Wunused-parameter` if `-Wunused` is specified, which is also implied by `-Wall`, but `-Wextra` is not enough by itself. – Dietrich Epp Jan 25 '13 at 01:49
  • It sounds like you want to use `__attribute__((deprecated))`, which will warn when something is used. – Dietrich Epp Jan 25 '13 at 01:50

3 Answers3

12

The __unused attribute is intended to prevent complaints when arguments to functions/methods or functions/methods are unused, not to enforce their lack of use.

The term used in the GCC manual is:

This attribute, attached to a function, means that the function is meant to be possibly unused

And for variables:

This attribute, attached to a variable, means that the variable is meant to be possibly unused.

The most common use is for development against an interface - e.g. callbacks, where you may be forced to accept several parameters but don't make use of all of them.

I use it a bit when I'm doing test driven development - my initial routine takes some parameters and does nothing, so all the parameters take __attribute__((unused)). As I develop it I make use of the parameters. At the end of development I remove them from the method, and see what shakes out.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • My understanding is that it was a compiler hint, that you don't necessarily need the value to be available, which should cause issues if you're using it. I get the same behavior with `__attribute__((unused))`. – wjl Jan 25 '13 at 10:11
  • 2
    It's not an optimization hint - calling conventions would require the parameter to be present. In the same way as declaring a `register` variable should not result in an error if you don't have any registers left to use. – Anya Shenanigans Jan 25 '13 at 10:37
3

Another way to skin this cat is to remove (or comment out) the name of the parameter.

int main ( int argc, char ** /* argv */ ) {
    printf("hello world. there are %d args\n", argc);
    return 0;
}

Now the compiler won't warn about argv being unused, and you can't use it, because it has no name.

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • This, for me, is definitely the way to go: the compiler doesn't need to know nor care; and you physically cannot use the parameters you want to leave unused! Be sure to note this convention down somewhere though so that it's not mistaken for sloppy programming. – fatuhoku Aug 16 '13 at 10:41
  • 2
    Clang/LLVM doesn't like this --- I get a "Parameter name omitted" error and I can't find any compiler flag that might disable this. Petesh's answer works well, though. – big_m Nov 15 '13 at 15:17
  • @big_m: I suspect there's something else going on here, because I use code just like that all the time. Can you post some code? – Marshall Clow Nov 15 '13 at 22:01
  • @Marshall, I created a new command-line project in Xcode (4.6.3), pasted in your code above, and I got a "Parameter name omitted" error, same as I get in my existing projects. Are you using Clang? I'm sure I've seen this work with other compilers in the past; I was just giving my experience with Clang/Xcode. – big_m Nov 16 '13 at 16:14
  • 1
    @big_m: Sorry - I see what's going on. I was building with C++ enabled. If I build with C, then I get the "parameter name omitted". Sorry for the confusion. – Marshall Clow Nov 17 '13 at 22:56
  • 1
    That's an interesting find, @Marshall. An error for C (and functions in Objective-C), but not for C++. Odd. Perhaps it's part of the C++ language definition. In any case, I think it's great to have all the options documented here. – big_m Nov 18 '13 at 01:13
  • This is apparently a gcc/clang extension, not part of standard C. Oddly it is not included in the documentation of gcc's extensions; they are usually pretty good about that. But compiling with `-pedantic` gives a warning with both gcc and clang. – Nate Eldredge May 08 '22 at 20:57
2

You can use deprecated and unused together to achieve the desired behavior (thanks to Dietrich Epp for mentioning deprecated in his comment):

#define is_unused __attribute__((deprecated, unused))

When the entity is actually unused, this will silence the warning. When an entity is flagged with this macro but still used, you'll get a deprecation warning.