Message ID | 1434143128-13785-1-git-send-email-andres.tiraboschi@tallertechnologies.com |
---|---|
State | New |
Headers | show |
On Fri, 12 Jun 2015, Andres Tiraboschi wrote: > 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. All options need documenting in invoke.texi. All diagnostics need testcases added to the testsuite. C++-specific options go in c.opt and should be listed as C++ ObjC++, not Common. All new diagnostics should use warning_at etc. with explicit locations passed, unless there is some strong reason it's hard to get the relevant location when the warning is given.
Hi, thanks for your answer. I was trying with warning_at this way instead that I was doing before: + 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_at (DECL_SOURCE_LINE(decl), OPT_Wconstexpr, "function %q+F cannot be evaluated at compile time", fn); + where "decl" is the declaration that is being initializated. For some reason this isn't working. Checking with gdb I realised that warning_at in this case is returnin false. I don't know if I am missing something. 2015-06-12 18:35 GMT-03:00 Joseph Myers <joseph@codesourcery.com>: > On Fri, 12 Jun 2015, Andres Tiraboschi wrote: > >> 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. > > All options need documenting in invoke.texi. All diagnostics need > testcases added to the testsuite. C++-specific options go in c.opt and > should be listed as C++ ObjC++, not Common. All new diagnostics should > use warning_at etc. with explicit locations passed, unless there is some > strong reason it's hard to get the relevant location when the warning is > given. > > -- > Joseph S. Myers > joseph@codesourcery.com
2015-06-12 18:35 GMT-03:00 Joseph Myers <joseph@codesourcery.com>: > On Fri, 12 Jun 2015, Andres Tiraboschi wrote: > >> 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. > > All options need documenting in invoke.texi. All diagnostics need > testcases added to the testsuite. C++-specific options go in c.opt and > should be listed as C++ ObjC++, not Common. All new diagnostics should > use warning_at etc. with explicit locations passed, unless there is some > strong reason it's hard to get the relevant location when the warning is > given. > > -- > Joseph S. Myers > joseph@codesourcery.com Hi, do you know where is the .exp file for the tests in .../gcc/testsuite/g++.dg/warn? I can't find it. Thanks again
On Jun 15, 2015, at 12:55 PM, Andres Tiraboschi <andres.tiraboschi@tallertechnologies.com> wrote: > > Hi, do you know where is the .exp file for the tests in > .../gcc/testsuite/g++.dg/warn? > I can't find it. find srcdir -name \*.exp -print will show you all of them. You’ll discover that a .exp file can run the entire tree under it. In this case, look in the parent directory.
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. */