Hi, this patch is for adding a warning when a constexpr cannot be evaluated at compile time. This is a single case: type var = fun(args...), with fun declared as a constexpr.
diff --git a/gcc/common.opt b/gcc/common.opt index b49ac46..88374b1 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -581,6 +581,10 @@ Winline Common Var(warn_inline) Warning Warn when an inlined function cannot be inlined +Wconstexpr +Common Var(warn_constexpr) Warning +Warn when a constexpr function is not evaluated at compile time + Winvalid-memory-model Common Var(warn_invalid_memory_model) Init(1) Warning Warn when an atomic memory model parameter is known to be outside the valid range. diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 80a6939..e25c240 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -842,6 +842,15 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); } + else if (TREE_CODE(init) == CALL_EXPR) + { + tree fn = TREE_OPERAND(CALL_EXPR_FN(init), 0); + if (DECL_DECLARED_CONSTEXPR_P(fn) && warn_constexpr) + { + warning (OPT_Wconstexpr, "function %q+F cannot be evaluated at compile time", fn); + warning (OPT_Wconstexpr, "called from here"); + } + } if (cxx_dialect >= cxx14) /* Handle aggregate NSDMI in non-constant initializers, too. */