From eae63d784c503ad7954ac8ae28232aec635ac7fc Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 11 Jan 2017 21:10:36 +0100 Subject: [PATCH] re PR c++/71537 (GCC rejects consetxpr boolean conversions and comparisons on the result of pointer arithmetic.) PR c++/71537 * fold-const.c (maybe_nonzero_address): Return 1 for function local objects. (tree_single_nonzero_warnv_p): Don't handle function local objects here. * g++.dg/cpp1y/constexpr-71537.C: New test. From-SVN: r244333 --- gcc/ChangeLog | 6 ++++++ gcc/fold-const.c | 17 +++++++++-------- gcc/testsuite/ChangeLog | 3 +++ gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index e46392c2953..82847b903ab 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,11 @@ 2017-01-11 Jakub Jelinek + PR c++/71537 + * fold-const.c (maybe_nonzero_address): Return 1 for function + local objects. + (tree_single_nonzero_warnv_p): Don't handle function local objects + here. + PR c++/72813 * gcc.c (default_compilers): Don't add -o %g.s for -S -save-temps of c-header. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 73ebd76d1e2..cfd270ce83c 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8171,7 +8171,8 @@ pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos) /* Return a positive integer when the symbol DECL is known to have a nonzero address, zero when it's known not to (e.g., it's a weak symbol), and a negative integer when the symbol is not yet in the - symbol table and so whether or not its address is zero is unknown. */ + symbol table and so whether or not its address is zero is unknown. + For function local objects always return positive integer. */ static int maybe_nonzero_address (tree decl) { @@ -8179,6 +8180,13 @@ maybe_nonzero_address (tree decl) if (struct symtab_node *symbol = symtab_node::get_create (decl)) return symbol->nonzero_address (); + /* Function local objects are never NULL. */ + if (DECL_P (decl) + && (DECL_CONTEXT (decl) + && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL + && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))) + return 1; + return -1; } @@ -13276,13 +13284,6 @@ tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) if (nonzero_addr >= 0) return nonzero_addr; - /* Function local objects are never NULL. */ - if (DECL_P (base) - && (DECL_CONTEXT (base) - && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL - && auto_var_in_fn_p (base, DECL_CONTEXT (base)))) - return true; - /* Constants are never weak. */ if (CONSTANT_CLASS_P (base)) return true; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b22c2e3213b..b8cd53a9bff 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2017-01-11 Jakub Jelinek + PR c++/71537 + * g++.dg/cpp1y/constexpr-71537.C: New test. + * gcc.dg/tree-ssa/flatten-3.c: Add quotation marks around dg-options argument. diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C new file mode 100644 index 00000000000..5d468d7c2e7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C @@ -0,0 +1,14 @@ +// PR c++/71537 +// { dg-do compile { target c++14 } } + +constexpr bool +foo () +{ + constexpr int n[42] = { 1 }; + constexpr int o = n ? 1 : 0; + constexpr int p = n + 1 ? 1 : 0; + constexpr int q = "abc" + 1 ? 1 : 0; + return p + p + q == 3; +} + +static_assert (foo (), ""); -- 2.30.2