[PATCH] D27424: Add the diagnose_if attribute to clang.
Eric Fiselier via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 5 20:14:55 PST 2016
EricWF added a comment.
Awesome, this is exactly what I was hoping for! Thanks for working on this!
I'm a bit surprised that the attribute fails to evaluate the condition as a constant expression during the evaluation of constexpr functions.
Ex:
#define DIAG(...) __attribute__((diagnose_if(__VA_ARGS__, "message", "error")))
constexpr bool foo(int x) DIAG(x == 0) // fails to evaluate this
{
// assert(x != 0); <-- ironically a simple assert would catch this
return x == 0;
}
constexpr bool bar(int x) { return foo(x); }
static_assert(bar(0), "");
Clearly the compiler can evaluate `x == 0` as a constant expression, but the attribute fails to see this. If the condition could be evaluated as-if it were part of the function body (during constexpr evaluation) that would be incredibly powerful. Especially within the STL where everything is `constexpr`.
The second thing that would be amazing if this attribute was late parsed so it the ability to reference `this` when applied to member functions.
For example:
struct Foo {
bool is_good;
constexpr bool good() const { return is_good; }
constexpr bool foo() const _diagnose_if(!good(), "message", "error")
{return true; }
};
constexpr Foo f{false};
static_assert(f.foo(), "");
Thanks again for working on this! I'm very excited to get to use it!
https://reviews.llvm.org/D27424
More information about the cfe-commits
mailing list