From: Jakub Jelinek Date: Fri, 8 Mar 2019 07:43:58 +0000 (+0100) Subject: re PR c++/89599 (C-style function-pointer-to-void* cast is handled inconsistently) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ed3ea9f271aecfdea1650c87f835efa70a6f1bac;p=gcc.git re PR c++/89599 (C-style function-pointer-to-void* cast is handled inconsistently) PR c++/89599 * constexpr.c (potential_constant_expression_1): Reject REINTERPRET_CAST_P NOP_EXPRs. * g++.dg/ubsan/vptr-4.C: Adjust expected diagnostics. * g++.dg/parse/array-size2.C: Likewise. * g++.dg/cpp0x/constexpr-89599.C: New test. From-SVN: r269482 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9bdb4936053..c6e023ff663 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2019-03-08 Jakub Jelinek + PR c++/89599 + * constexpr.c (potential_constant_expression_1): Reject + REINTERPRET_CAST_P NOP_EXPRs. + PR c++/89622 * call.c (joust): Call print_z_candidate only if pedwarn returned true. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 1c3c7252807..783d1fcb496 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5961,6 +5961,13 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, return true; case NOP_EXPR: + if (REINTERPRET_CAST_P (t)) + { + if (flags & tf_error) + error_at (loc, "a reinterpret_cast is not a constant expression"); + return false; + } + /* FALLTHRU */ case CONVERT_EXPR: case VIEW_CONVERT_EXPR: /* -- a reinterpret_cast. FIXME not implemented, and this rule diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 777c1b29937..5db4e375b27 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,10 @@ 2019-03-08 Jakub Jelinek + PR c++/89599 + * g++.dg/ubsan/vptr-4.C: Adjust expected diagnostics. + * g++.dg/parse/array-size2.C: Likewise. + * g++.dg/cpp0x/constexpr-89599.C: New test. + PR c++/89622 * g++.dg/warn/pr89622.C: New test. diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-89599.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-89599.C new file mode 100644 index 00000000000..07760a302a7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-89599.C @@ -0,0 +1,6 @@ +// PR c++/89599 +// { dg-do compile { target c++11 } } + +void foo (int x) {} +constexpr void *arr[2] = { (void*) &foo, (void *) foo };// { dg-error "a reinterpret_cast is not a constant expression" } +constexpr void *ptr = (void *) &foo; // { dg-error "a reinterpret_cast is not a constant expression" } diff --git a/gcc/testsuite/g++.dg/parse/array-size2.C b/gcc/testsuite/g++.dg/parse/array-size2.C index e58fe266e77..c4a69df3b01 100644 --- a/gcc/testsuite/g++.dg/parse/array-size2.C +++ b/gcc/testsuite/g++.dg/parse/array-size2.C @@ -15,6 +15,8 @@ void foo (void) { char g[(char *) &((struct S *) 0)->b - (char *) 0]; // { dg-error "40:size of array .g. is not an integral constant-expression" } + // { dg-error "narrowing conversion" "" { target c++11 } .-1 } + // { dg-message "expression has a constant value but is not a C.. constant-expression" "" { target c++11 } .-2 } char h[(__SIZE_TYPE__) &((struct S *) 8)->b]; // { dg-error "10:size of array .h. is not an integral constant-expression" } bar (g, h); } diff --git a/gcc/testsuite/g++.dg/ubsan/vptr-4.C b/gcc/testsuite/g++.dg/ubsan/vptr-4.C index cf638e9b35f..764f5999501 100644 --- a/gcc/testsuite/g++.dg/ubsan/vptr-4.C +++ b/gcc/testsuite/g++.dg/ubsan/vptr-4.C @@ -19,7 +19,7 @@ struct T : S { }; constexpr T t; -constexpr const T *p = t.foo (); // { dg-message "expansion of" } +constexpr const T *p = t.foo (); // { dg-error "called in a constant expression" } template struct V { @@ -39,17 +39,16 @@ struct W : V { }; constexpr W w; -constexpr const W *s = w.foo (); // { dg-error "is not a constant expression" } -// { dg-message "expansion of" "" { target *-*-* } .-1 } +constexpr const W *s = w.foo (); // { dg-error "called in a constant expression" } template int foo (void) { static constexpr T t; - static constexpr const T *p = t.foo (); // { dg-message "expansion of" } + static constexpr const T *p = t.foo (); // { dg-error "called in a constant expression" } static constexpr W w; - static constexpr const W *s = w.foo (); // { dg-error "is not a constant expression" } - return t.b + w.b; // { dg-message "expansion of" "" { target *-*-* } .-1 } + static constexpr const W *s = w.foo (); // { dg-error "called in a constant expression" } + return t.b + w.b; } int x = foo ();