aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2020-09-01 11:44:16 -0400
committerMarek Polacek <polacek@redhat.com>2020-09-01 17:49:20 -0400
commitb1c59b31ef7adc832405209e9e2a77212284abd7 (patch)
tree42f54803b37d39309d4637b18e600ded4247e36b /gcc
parent10f51543bb81cc953792270b40a9c812049e8b4c (diff)
downloadgcc-b1c59b31ef7adc832405209e9e2a77212284abd7.zip
gcc-b1c59b31ef7adc832405209e9e2a77212284abd7.tar.gz
gcc-b1c59b31ef7adc832405209e9e2a77212284abd7.tar.bz2
c++: Allow new char[4]{"foo"} [PR77841]
Currently, we allow new char[]{"foo"}, but not new char[4]{"foo"}. We should accept the latter too: [dcl.init.list]p3.3 says to treat this as [dcl.init.string]. We were rejecting this code because we never called reshape_init before the digest_init in build_new_1. reshape_init handles [dcl.init.string] by unwrapping the STRING_CST from its enclosing { }, and digest_init assumes that reshape_init has been called for aggregates anyway, and an array is an aggregate. gcc/cp/ChangeLog: PR c++/77841 * init.c (build_new_1): Call reshape_init. gcc/testsuite/ChangeLog: PR c++/77841 * g++.dg/cpp0x/initlist-new4.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/init.c6
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/initlist-new4.C6
2 files changed, 12 insertions, 0 deletions
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 360ab8c..d4540db 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -3575,6 +3575,12 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
/* We'll check the length at runtime. */
domain = NULL_TREE;
arraytype = build_cplus_array_type (type, domain);
+ /* If we have new char[4]{"foo"}, we have to reshape
+ so that the STRING_CST isn't wrapped in { }. */
+ vecinit = reshape_init (arraytype, vecinit, complain);
+ /* The middle end doesn't cope with the location wrapper
+ around a STRING_CST. */
+ STRIP_ANY_LOCATION_WRAPPER (vecinit);
vecinit = digest_init (arraytype, vecinit, complain);
}
}
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-new4.C b/gcc/testsuite/g++.dg/cpp0x/initlist-new4.C
new file mode 100644
index 0000000..d7b4184
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist-new4.C
@@ -0,0 +1,6 @@
+// PR c++/77841
+// { dg-do compile { target c++11 } }
+
+char *p1 = new char[4]{"foo"};
+char *p2 = new char[5]{"foo"};
+char *p3 = new char[3]{"foo"}; // { dg-error "too long" }