[cfe-dev] Force compile-time constant expressions evaluation
Ettore Speziale
speziale.ettore at gmail.com
Thu May 21 11:41:56 PDT 2015
Hello guys,
Let’s suppose I want to declare a function and some of their actual arguments must be constant expressions. I came out whit this solution:
#define MUST_BE_COMPILE_TIME_CONSTANT(V) \
__attribute__((enable_if(V || !V, "'" #V "' argument must be known at compile-time")))
constexpr int baz_body() {
return 0;
}
constexpr int baz_no_body();
int bar(int baz) MUST_BE_COMPILE_TIME_CONSTANT(baz);
int call_bar_fail(int baz) {
return bar(baz) + baz_no_body();
}
int call_bar_ok() {
return bar(0) + bar(1 + 2) + bar(baz_body());
}
The enable_if attribute allow me to identify bad call sites.
Now my problem is that I would like to force the evaluation of the constant expressions such as I would end up passing immediate values to all the call-sites of bar.
For instance if you try to compile only call_bar_ok with -O0 or -O1 you get different IR as with -O0 the call to baz_body is not inlined.
Is there in clang some attribute or flag to force compile-time evaluation of (some) constant expressions? I tried to look into the documentation and into the sources but I found nothing about that …
Thanks,
Ettore Speziale
More information about the cfe-dev
mailing list