This patch adds the diagnose_if attribute to clang.
diagnose_if is meant to be a less powerful version of enable_if: it doesn't interact with overload resolution at all, and if the condition in the diagnose_if attribute can't be evaluated, compilation proceeds as though said diagnose_if attribute was not present. It can be used to emit either errors or warnings, like so:
void foo(int a) __attribute__((diagnose_if(a > 10, "a is too large!", "error"))); void bar(int a) __attribute__((diagnose_if(a > 10, "this takes a long time if a is larger than 10.", "warning"))); void baz(int a) __attribute__((diagnose_if(1, "don't use me anymore", "warning"))); void run(int a) { foo(10); foo(11); // error: a is too large foo(a); bar(10); bar(11); // warning: this takes a long time if a is larger than 10. bar(a); void (*b)(int) = baz; // warning: don't use me anymore. }
Under the hood, we have two kinds of diagnose_if attributes: ones that are dependent on the value of arguments (which are handled a lot like enable_if), and ones that aren't (which are treated like unavailable/deprecated attributes). This split made this attribute *substantially* less complex to implement, since it lets us reuse existing code