_Unused Flag Behavior/Usage (Gcc with Objective-C)

__unused Flag Behavior/Usage (GCC with Objective-C)

The __unused macro (which is in fact expanded to the __attribute__((unused)) GCC attribute) only tells the compiler "don't warn me if I don't use this variable".

unused: This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable. (Source: gnu.gcc.org doc)

So this GCC attribute is to avoid a warning when you don't use a variable, and NOT to trigger one when you use the variable you claimed unused.


As regard to putting the attribute before or after the variable name in your last example, both are accepted and equivalent in your case: the compiler is just lenient about that placement for compatibility purposes (quite as you can also write both const int i or int const i)

For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes (Source: gnu.gcc.org doc)

Using the GCC __unused attribute with Objective-C

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.

Are NSString * __unused aString and NSString __unused * aString equivalent?

There is a difference between the two.

When the attribute, __unused, appears before the star, it decorates the primary type of the entire declaration list. All variables will be "unused":


__unused NSString *foo, *bar; // OK. All variables are unused in the statement.
NSString __unused *foo, *bar; // OK

But when placed after the *, it will only apply to the first variable:


NSString * __unused foo, *bar; // Unused variable 'bar'

I prefer NSString * __unused foo; because it seems more clear to me and won't hide the rare case when I declare multiple variables in one statement.

The GCC Attribute Syntax reference mentions it in Section 6.31:

An attribute specifier list may appear immediately before a declarator
(other than the first) in a comma-separated list of declarators in a
declaration of more than one identifier using a single list of
specifiers and qualifiers. Such attribute specifiers apply only to the
identifier before whose declarator they appear. For example, in

 __attribute__((noreturn)) void d0 (void),
__attribute__((format(printf, 1, 2))) d1 (const char *, ...),
d2 (void)

the noreturn attribute applies to all the functions declared; the format attribute only applies to d1.

Where is the __unused macro defined?

unused 

is GCC specific 6.32.1 Common Variable Attributes

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable.

NTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x does not even mention unused.

Xcode development, can I place #pragma unused(x) via some #define rule

In the #else case, you can put the function call on the right side of the && operator with 0 on the left side. That will ensure that variables are "used" while also ensuring that the function doesn't actually get called and that the parameters are not evaluated.

#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr, "%s \n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
#else
#define NSLog(FORMAT, ...) (0 && fprintf(stderr, "%s \n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]))
#endif

Updating Objective-C syntax with Swift equivalent for unused static

That's just a function that returns a CGFloat:

func SLKPointSizeDifference(for category: UIContentSizeCategory) -> CGFloat {
typealias c = UIContentSizeCategory // for the sake of reducing boilerplate below

switch category {
case c.extraSmall: return -3
case c.small: return -2
case c.medium: return -1
case c.large: return 0
case c.extraLarge: return 2
case c.extraExtraLarge: return 4
case c.extraExtraExtraLarge: return 6
case c.accessibilityMedium: return 8
case c.accessibilityLarge: return 10
case c.accessibilityExtraLarge: return 11
case c.accessibilityExtraExtraLarge: return 12
case c.accessibilityExtraExtraExtraLarge: return 13
default: return 0
}
}
  • __unused is just a flag that tells the compiler not to warn if that function is never used. See more here.

  • static just limits the scope of that function's existence to the current file.

  • UIContentSizeCategory is imported into Swift as a struct, which is more strongly typed than just using raw strings.

[[maybe_unused]] on member variable, GCC warns (incorrectly?) that attribute is ignored

Any attribute can be "ignored by the compiler" for any reason, except where the standard says otherwise (such as using an attribute in a location where it is expressly forbidden).

GCC isn't saying you can't put one there; it's saying that putting one there won't do anything, because they probably don't warn about maybe-unused member variables.

How to remove unused C/C++ symbols with GCC and ld?

For GCC, this is accomplished in two stages:

First compile the data but tell the compiler to separate the code into separate sections within the translation unit. This will be done for functions, classes, and external variables by using the following two compiler flags:

-fdata-sections -ffunction-sections

Link the translation units together using the linker optimization flag (this causes the linker to discard unreferenced sections):

-Wl,--gc-sections

So if you had one file called test.cpp that had two functions declared in it, but one of them was unused, you could omit the unused one with the following command to gcc(g++):

gcc -Os -fdata-sections -ffunction-sections test.cpp -o test -Wl,--gc-sections

(Note that -Os is an additional compiler flag that tells GCC to optimize for size)

prevent gcc from removing an unused variable

You can use __attribute__((used)) gcc (also works in clang) specific (I see that the question is tagged gcc) attributes for this:

This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly.

From https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

Demo:

$ cat a.c
static const char srcvers[] __attribute__((used)) = "VERSION/foo.c/1.01/09.04.15";
$ gcc -O3 -c a.c
$ strings a.o
VERSION/foo.c/1.01/09.04.15

You can use some #ifs and #defines to make this terser and also compile on compilers which don't support this extension.

How do I get rid of the unused parameter warning in C with gcc 4.8.4 [-Wunused-parameter]

My comment was unclear:

You can write (void)argc inside main() in order to get rid of the compiler message without doing any harm to your program

int main(int argc, char *argv[])
{
char *prog = argv[0];
(void)argc;
return 0;
}


Related Topics



Leave a reply



Submit