aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-03-19 18:36:56 +0100
committerJakub Jelinek <jakub@redhat.com>2021-03-19 18:36:56 +0100
commit82bb66730bc42b8694fdebef607ea6e49e8496bf (patch)
tree7c7282833d2a07704bea93c4f10887d073d4f498 /gcc
parent02f305440f29c68b7368c9af9ae689cce6d26d6d (diff)
downloadgcc-82bb66730bc42b8694fdebef607ea6e49e8496bf.zip
gcc-82bb66730bc42b8694fdebef607ea6e49e8496bf.tar.gz
gcc-82bb66730bc42b8694fdebef607ea6e49e8496bf.tar.bz2
c++: Only reject reinterpret casts from pointers to integers for manifestly_const_eval evaluation [PR99456]
My PR82304/PR95307 fix moved reinterpret cast from pointer to integer diagnostics from cxx_eval_outermost_constant_expr where it caught invalid code only at the outermost level down into cxx_eval_constant_expression. Unfortunately, it regressed following testcase, we emit worse code including dynamic initialization of some vars. While the initializers are not constant expressions due to the reinterpret_cast in there, there is no reason not to fold them as an optimization. I've tried to make this dependent on !ctx->quiet, but that regressed two further tests, and on ctx->strict, which regressed other tests, so this patch bases that on manifestly_const_eval. The new testcase is now optimized as much as it used to be in GCC 10 and the only regression it causes is an extra -Wnarrowing warning on vla22.C test on invalid code (which the patch adjusts). 2021-03-19 Jakub Jelinek <jakub@redhat.com> PR c++/99456 * constexpr.c (cxx_eval_constant_expression): For CONVERT_EXPR from INDIRECT_TYPE_P to ARITHMETIC_TYPE_P, when !ctx->manifestly_const_eval don't diagnose it, set *non_constant_p nor return t. * g++.dg/opt/pr99456.C: New test. * g++.dg/ext/vla22.C: Expect a -Wnarrowing warning for c++11 and later.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/constexpr.c3
-rw-r--r--gcc/testsuite/g++.dg/ext/vla22.C2
-rw-r--r--gcc/testsuite/g++.dg/opt/pr99456.C33
3 files changed, 36 insertions, 2 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index c946744..42d00ec 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -6656,7 +6656,8 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
if (TREE_CODE (t) == CONVERT_EXPR
&& ARITHMETIC_TYPE_P (type)
- && INDIRECT_TYPE_P (TREE_TYPE (op)))
+ && INDIRECT_TYPE_P (TREE_TYPE (op))
+ && ctx->manifestly_const_eval)
{
if (!ctx->quiet)
error_at (loc,
diff --git a/gcc/testsuite/g++.dg/ext/vla22.C b/gcc/testsuite/g++.dg/ext/vla22.C
index 2308ee7..967adb9 100644
--- a/gcc/testsuite/g++.dg/ext/vla22.C
+++ b/gcc/testsuite/g++.dg/ext/vla22.C
@@ -6,4 +6,4 @@ void
f ()
{
const int tbl[(long) "h"] = { 12 }; // { dg-error "size of array .tbl. is not an integral constant-expression" }
-}
+} // { dg-warning "narrowing conversion" "" { target c++11 } .-1 }
diff --git a/gcc/testsuite/g++.dg/opt/pr99456.C b/gcc/testsuite/g++.dg/opt/pr99456.C
new file mode 100644
index 0000000..66f19c8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/pr99456.C
@@ -0,0 +1,33 @@
+// PR c++/99456
+// { dg-do compile { target c++17 } }
+// { dg-options "-g0" }
+// { dg-final { scan-assembler-not "PR99456Var0\[1234]" } }
+// { dg-final { scan-assembler-not "__static_initialization_and_destruction" } }
+// { dg-final { scan-assembler-not "_GLOBAL__sub_I" } }
+// { dg-final { scan-assembler-not "_ZGV12PR99456Var1\[1234]" } }
+
+typedef __UINTPTR_TYPE__ uintptr_t;
+
+class Container
+{
+public:
+ uintptr_t m;
+};
+
+extern unsigned desc;
+static constexpr unsigned &descRef = desc;
+
+inline Container PR99456Var01 {reinterpret_cast<uintptr_t> (&descRef)};
+inline Container PR99456Var02 {reinterpret_cast<uintptr_t> (&desc)};
+inline uintptr_t PR99456Var03 {reinterpret_cast<uintptr_t> (&descRef)};
+inline uintptr_t PR99456Var04 {reinterpret_cast<uintptr_t> (&desc)};
+
+inline Container PR99456Var11 {reinterpret_cast<uintptr_t> (&descRef)};
+inline Container PR99456Var12 {reinterpret_cast<uintptr_t> (&desc)};
+inline uintptr_t PR99456Var13 {reinterpret_cast<uintptr_t> (&descRef)};
+inline uintptr_t PR99456Var14 {reinterpret_cast<uintptr_t> (&desc)};
+
+auto *PR99456Ref11 = &PR99456Var11;
+auto *PR99456Ref12 = &PR99456Var12;
+auto *PR99456Ref13 = &PR99456Var13;
+auto *PR99456Ref14 = &PR99456Var14;