diff mbox

PR C++/58708 - string literal operator templates broken

Message ID 526A7618.5030601@verizon.net
State New
Headers show

Commit Message

Ed Smith-Rowland Oct. 25, 2013, 1:46 p.m. UTC
On 10/25/2013 07:54 AM, Paolo Carlini wrote:
> On 10/25/2013 01:38 PM, Ed Smith-Rowland wrote:
>> So, you also want a library testcase?
> Up to you: if you feel it would test something that the c++ front-end 
> testcase doesn't, why not.
>
> Paolo.
>
I think this patch should be sufficient - no containers, it just follows 
the pattern of the other tests in the cpp1y directory.
I builds and tests of x86_64-linux.

OK?

Later we can think of library tools that could help authors of literals 
operators.
The bits/bits/parse_numbers.h is a start on this.

Ed

gcc/cp:

2013-10-25  Edward Smith-Rowland  <3dw4rd@verizon.net>

        PR c++/58708
	* parser.c (make_string_pack): Discover non-const type and size
	of character and build parm pack with correct type and chars.

gcc/testsuite:

2013-10-25  Edward Smith-Rowland  <3dw4rd@verizon.net>

        PR c++/58708
	*g++.dg/cpp1y/pr58708.C : New.

Comments

Paolo Carlini Oct. 25, 2013, 2:01 p.m. UTC | #1
On 10/25/2013 03:46 PM, Ed Smith-Rowland wrote:
> On 10/25/2013 07:54 AM, Paolo Carlini wrote:
>> On 10/25/2013 01:38 PM, Ed Smith-Rowland wrote:
>>> So, you also want a library testcase?
>> Up to you: if you feel it would test something that the c++ front-end 
>> testcase doesn't, why not.
>>
>> Paolo.
>>
> I think this patch should be sufficient - no containers, it just 
> follows the pattern of the other tests in the cpp1y directory.
Note, however, that <type_traits> isn't small at all, and apparently you 
are only using std::is_same which is just a one line template + a one 
line specialization (just grep in testsuite/g++.dg)

Paolo.
Ed Smith-Rowland Oct. 25, 2013, 2:36 p.m. UTC | #2
On 10/25/2013 10:01 AM, Paolo Carlini wrote:
> On 10/25/2013 03:46 PM, Ed Smith-Rowland wrote:
>> On 10/25/2013 07:54 AM, Paolo Carlini wrote:
>>> On 10/25/2013 01:38 PM, Ed Smith-Rowland wrote:
>>>> So, you also want a library testcase?
>>> Up to you: if you feel it would test something that the c++ 
>>> front-end testcase doesn't, why not.
>>>
>>> Paolo.
>>>
>> I think this patch should be sufficient - no containers, it just 
>> follows the pattern of the other tests in the cpp1y directory.
> Note, however, that <type_traits> isn't small at all, and apparently 
> you are only using std::is_same which is just a one line template + a 
> one line specialization (just grep in testsuite/g++.dg)
>
> Paolo.
>
Hold up.  something didn't work.
I'll be back later.
Ed
diff mbox

Patch

Index: cp/parser.c
===================================================================
--- cp/parser.c	(revision 203997)
+++ cp/parser.c	(working copy)
@@ -3793,22 +3793,39 @@ 
   tree charvec;
   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
   const char *str = TREE_STRING_POINTER (value);
-  int i, len = TREE_STRING_LENGTH (value) - 1;
+  int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
+  int len = TREE_STRING_LENGTH (value) / sz - 1;
   tree argvec = make_tree_vec (2);
 
-  tree string_char_type_node = TREE_TYPE (TREE_TYPE (value));
+  tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
+  str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
 
   /* First template parm is character type.  */
-  TREE_VEC_ELT (argvec, 0) = string_char_type_node;
+  TREE_VEC_ELT (argvec, 0) = str_char_type_node;
 
   /* Fill in CHARVEC with all of the parameters.  */
   charvec = make_tree_vec (len);
-  for (i = 0; i < len; ++i)
-    TREE_VEC_ELT (charvec, i) = build_int_cst (string_char_type_node, str[i]);
+  if (sz == 1)
+    {
+      for (int i = 0; i < len; ++i)
+	TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, str[i]);
+    }
+  else if (sz == 2)
+    {
+      const uint16_t *num = (const uint16_t *)str;
+      for (int i = 0; i < len; ++i)
+	TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
+    }
+  else if (sz == 4)
+    {
+      const uint32_t *num = (const uint32_t *)str;
+      for (int i = 0; i < len; ++i)
+	TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
+    }
 
   /* Build the argument packs.  */
   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
-  TREE_TYPE (argpack) = string_char_type_node;
+  TREE_TYPE (argpack) = str_char_type_node;
 
   TREE_VEC_ELT (argvec, 1) = argpack;
 
Index: testsuite/g++.dg/cpp1y/pr58708.C
===================================================================
--- testsuite/g++.dg/cpp1y/pr58708.C	(revision 0)
+++ testsuite/g++.dg/cpp1y/pr58708.C	(working copy)
@@ -0,0 +1,68 @@ 
+// { dg-options -std=c++1y }
+// { dg-do run }
+
+#include <type_traits>
+
+struct Foo
+{
+  bool type[4];
+  bool const_type[4];
+  int chars[10];
+};
+
+template<typename CharT, CharT... str>
+Foo
+operator""_foo()
+{
+  CharT arr[]{str...};
+
+  Foo foo;
+
+  foo.type[0] = std::is_same<CharT, char>::value;
+  foo.type[1] = std::is_same<CharT, wchar_t>::value;
+  foo.type[2] = std::is_same<CharT, char16_t>::value;
+  foo.type[3] = std::is_same<CharT, char32_t>::value;
+  foo.const_type[0] = std::is_same<CharT, const char>::value;
+  foo.const_type[1] = std::is_same<CharT, const wchar_t>::value;
+  foo.const_type[2] = std::is_same<CharT, const char16_t>::value;
+  foo.const_type[3] = std::is_same<CharT, const char32_t>::value;
+
+  for(int i; i < sizeof(arr)/sizeof(CharT) - 1; ++i)
+    foo.chars[i] = (int)arr[i];
+
+  return foo;
+}
+
+int
+main()
+{
+  Foo foo;
+
+  foo = U"\x10000\x10001\x10002"_foo;
+  if (foo.type[3] != true) __builtin_abort();
+  if (sizeof(foo.chars)/sizeof(char32_t)-1 != 3) __builtin_abort();
+  if (foo.chars[0] != 65536) __builtin_abort();
+  if (foo.chars[1] != 65537) __builtin_abort();
+  if (foo.chars[2] != 65538) __builtin_abort();
+
+  foo = "\x61\x62\x63"_foo;
+  if (foo.type[0] != true) __builtin_abort();
+  if (sizeof(foo.chars)/sizeof(char)-1 != 3) __builtin_abort();
+  if (foo.chars[0] != 97) __builtin_abort();
+  if (foo.chars[1] != 98) __builtin_abort();
+  if (foo.chars[2] != 99) __builtin_abort();
+
+  foo = L"\x01020304\x05060708"_foo;
+  if (foo.type[1] != true) __builtin_abort();
+  if (sizeof(foo.chars)/sizeof(wchar_t)-1 != 2) __builtin_abort();
+  if (foo.chars[0] != 16909060) __builtin_abort();
+  if (foo.chars[1] != 84281096) __builtin_abort();
+
+  foo = u"\x0102\x0304\x0506\x0708"_foo;
+  if (foo.type[2] != true) __builtin_abort();
+  if (sizeof(foo.chars)/sizeof(char16_t)-1 != 4) __builtin_abort();
+  if (foo.chars[0] != 258) __builtin_abort();
+  if (foo.chars[1] != 772) __builtin_abort();
+  if (foo.chars[2] != 1286) __builtin_abort();
+  if (foo.chars[3] != 1800) __builtin_abort();
+}