Bug 79459 - Please add enable_if and diagnose_if attributes (from clang)
Summary: Please add enable_if and diagnose_if attributes (from clang)
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-02-10 20:53 UTC by felix-gcc
Modified: 2024-01-30 08:06 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description felix-gcc 2017-02-10 20:53:42 UTC
clang supports several advanced __attribute__ cases that gcc does not, and two strike me as particularly useful: enable_if and diagnose_if. You can find the documentation on those here:

https://clang.llvm.org/docs/AttributeReference.html#diagnose-if
https://clang.llvm.org/docs/AttributeReference.html#enable-if

Basically, diagnose_if allows to add custom warning messages, think of it as a superset of the nonnull attribute. This could be a great tool to improve code quality, if applied to an API from a library.

enable_if is much more complex and probably a lot harder to implement. It allows to have a special case version of a library function and use the attribute to tell the compiler to call it if the compiler knows the special case is true. For example, one could have a special memset version that does not need to do alignment handling if the compiler can tell that the destination buffer is 16 byte aligned.
Comment 1 Martin Sebor 2017-07-11 03:39:22 UTC
Confirmed.  I think a feature along these lines could be useful as a generalization of some existing warnings (such as -Walloc-zero, -Walloc-size-larger-than, or -Wnonnull) and future ones like those.

An aspect of the attribute that might be worth handling differently in GCC than in Clang is whether the checking should be restricted to just arguments that are constant expressions or also applied to arguments that are the result of constant or value range propagation (or perhaps even other forms of optimization.  I think the latter would make it more powerful in GCC, but I wonder if there might also be cases where it could lead to excessive false positives.  If there were such cases, generalizing the attribute to specify what kinds of expressions the diagnostic should consider might be worthwhile (i.e., constant expressions only, or even those where the expression is known to have a value or be in some range as a result of various optimizations).
Comment 2 corentinjabot 2024-01-29 20:49:50 UTC
This could be used to implement SFINAE friendly, warning free <compare> operators
See https://github.com/llvm/llvm-project/pull/79465
https://godbolt.org/z/1xr9Wx38T