1737973
1737973

Reputation: 118

__attribute_warn_unused_result__ vs __attribute__((warn_unused_result))

Been using __attribute__((warn_unused_result)) with joy for a while in some new project. I have been coding using Vim.

Now I'm starting to use KDevelop sometimes when developing that same project, and there is an autocomplete __attribute_warn_unused_result__ (among others) as well as __attribute__ which I was using.

Are those two __attribute__((warn_unused_result)) and __attribute_warn_unused_result__ the same? Is any of those supposed to supersede the other? I realized that, when editing headers (*.h), the autocomplete feature suggests both alternatives, but that, when editing sources (*.c), the feature only suggests __attribute_warn_unused_result__.

Upvotes: 2

Views: 1054

Answers (1)

ReAl
ReAl

Reputation: 1301

GCC supports this attribute since 3.4 so that __attribute_warn_unused_result__ defined as __attribute__((warn_unused_result)) or empty depend on GCC version (see sys/cdefs.h).

Search __attribute_warn_unused_result__ definition in your programming system.

Update:

Attribute warn_unused_result is a feature of the compiler. Any GCC compiler since 3.4 will recognize and use __attribute__((warn_unused_result)). But this feature may not be supported by other compilers or may be specified otherwise.

On the other hand, the __attribute_warn_unused_result__ macro defined in library header file. Purposes — the ability to remove an attribute for non-supported compilers; the hide implementation details and specify this property in another way.

But this macro depends on library implementation. For example, the macro not present at all in arm-none-eabi-gcc 4.9.3 and avr-gcc 4.9.2 packages, just as in mingw32-gcc 3.4.2 which I still use for one "ancient" project.

For gcc-linaro-7.2.1-2017.11-x86_64_arm-eabi similar purpose macros defined as

#define __result_use_check  __attribute__((__warn_unused_result__))

But in gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabihf package the definition (a whole sys/cdefs.h file) is the same as for "native" gcc in Ubuntu 16.04.

#define __attribute_warn_unused_result__ \
    __attribute__ ((__warn_unused_result__))

Hence, which one approach supercedes another one depends on the goals. On my opinion:

  • For code that supposed to be used with GCC (>= 3.4) across a couple of platforms it is better to use explicit __attribute__((__warn_unused_result__)).

  • For code that supposed to be compiled by a couple of compilers that may not support this feature or may support it in different way it is better to use some macro (may be even self-defined).

  • For already existing project it is better to use the approach mostly used in present code :-)

About autocompletion with different behavior for header and source files. I don't know. May be it is just originality of specific IDE (autocompletion).

Upvotes: 4

Related Questions