public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings
@ 2014-05-13  2:38 Ed Smith-Rowland
  2014-05-13 17:29 ` Joseph S. Myers
  2014-05-21  9:37 ` Andreas Schwab
  0 siblings, 2 replies; 6+ messages in thread
From: Ed Smith-Rowland @ 2014-05-13  2:38 UTC (permalink / raw)
  To: gcc-patches, Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 365 bytes --]

This patch is really a libcpp patch.  But UDLs are like that ;-)

Add string user-defined literals and char user-defined literals to the 
list of things to look out for while escaping strings in macro args.

I'm not sure how to test this really.  we want to write out *.ii files 
and verify that internal quotes are escaped.

Built and tested on x86_64-linux.
OK?


[-- Attachment #2: CL_pr61038 --]
[-- Type: text/plain, Size: 241 bytes --]


libcpp/

2014-05-12  Edward Smith-Rowland  <3dw4rd@verizon.net>

	PR C++/61038
	* macro.c (stringify_arg (cpp_reader *, macro_arg *)):
	Check for user-defined literal strings and user-defined literal chars
	to escape necessary characters.


[-- Attachment #3: CL_pr61038 --]
[-- Type: text/plain, Size: 120 bytes --]


gcc/testsuite/

2014-05-12  Edward Smith-Rowland  <3dw4rd@verizon.net>

	PR C++/61038
	* g++.dg/cpp0x/pr61038.C: New.


[-- Attachment #4: patch_pr61038 --]
[-- Type: text/plain, Size: 1065 bytes --]

Index: macro.c
===================================================================
--- macro.c	(revision 210315)
+++ macro.c	(working copy)
@@ -494,6 +494,9 @@
 		   || token->type == CPP_STRING16 || token->type == CPP_CHAR16
 		   || token->type == CPP_UTF8STRING);
 
+      escape_it = escape_it || cpp_userdef_string_p (token->type)
+			    || cpp_userdef_char_p (token->type);
+
       /* Room for each char being written in octal, initial space and
 	 final quote and NUL.  */
       len = cpp_token_len (token);
Index: ../gcc/testsuite/g++.dg/cpp0x/pr61038.C
===================================================================
--- ../gcc/testsuite/g++.dg/cpp0x/pr61038.C	(revision 0)
+++ ../gcc/testsuite/g++.dg/cpp0x/pr61038.C	(working copy)
@@ -0,0 +1,23 @@
+// PR c++/61038
+// { dg-do compile { target c++11 } }
+// { dg-options "-E" }
+
+void
+operator "" _s(const char *, unsigned long)
+{ }
+
+void
+operator "" _t(const char)
+{ }
+
+#define QUOTE(s) #s
+
+int
+main()
+{
+  QUOTE("hello"_s);
+
+  QUOTE('"'_t);
+  QUOTE('\''_t);
+  QUOTE('\\'_t);
+}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings
  2014-05-13  2:38 [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings Ed Smith-Rowland
@ 2014-05-13 17:29 ` Joseph S. Myers
  2014-05-14  0:59   ` Ed Smith-Rowland
  2014-05-21  9:37 ` Andreas Schwab
  1 sibling, 1 reply; 6+ messages in thread
From: Joseph S. Myers @ 2014-05-13 17:29 UTC (permalink / raw)
  To: Ed Smith-Rowland; +Cc: gcc-patches, Jason Merrill

On Mon, 12 May 2014, Ed Smith-Rowland wrote:

> This patch is really a libcpp patch.  But UDLs are like that ;-)
> 
> Add string user-defined literals and char user-defined literals to the list of
> things to look out for while escaping strings in macro args.
> 
> I'm not sure how to test this really.  we want to write out *.ii files and
> verify that internal quotes are escaped.

You should be able to check the results of stringizing twice, e.g.:

extern "C" int strcmp (const char *, const char *);
extern "C" void abort (void);
extern "C" void exit (int);

void operator "" _s(const char *, unsigned long)
{
}

#define QUOTE(s) #s
#define QQUOTE(s) QUOTE(s)

const char *s = QQUOTE(QUOTE("hello"_s));
const char *t = QUOTE("\"hello\"_s");

int main()
{
  if (strcmp(s, t) == 0)
    exit(0);
  else
    abort();
}

(at least, this fails for me with unmodified GCC, and I think it should 
pass).

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings
  2014-05-13 17:29 ` Joseph S. Myers
@ 2014-05-14  0:59   ` Ed Smith-Rowland
  2014-05-20 20:44     ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Ed Smith-Rowland @ 2014-05-14  0:59 UTC (permalink / raw)
  To: Joseph S. Myers; +Cc: gcc-patches, Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 1112 bytes --]

On 05/13/2014 01:29 PM, Joseph S. Myers wrote:
> On Mon, 12 May 2014, Ed Smith-Rowland wrote:
>
>> This patch is really a libcpp patch.  But UDLs are like that ;-)
>>
>> Add string user-defined literals and char user-defined literals to the list of
>> things to look out for while escaping strings in macro args.
>>
>> I'm not sure how to test this really.  we want to write out *.ii files and
>> verify that internal quotes are escaped.
> You should be able to check the results of stringizing twice, e.g.:
>
> extern "C" int strcmp (const char *, const char *);
> extern "C" void abort (void);
> extern "C" void exit (int);
>
> void operator "" _s(const char *, unsigned long)
> {
> }
>
> #define QUOTE(s) #s
> #define QQUOTE(s) QUOTE(s)
>
> const char *s = QQUOTE(QUOTE("hello"_s));
> const char *t = QUOTE("\"hello\"_s");
>
> int main()
> {
>    if (strcmp(s, t) == 0)
>      exit(0);
>    else
>      abort();
> }
>
> (at least, this fails for me with unmodified GCC, and I think it should
> pass).
>
Thank you Joe!

Here is a new patch with a proper test case.

Built and tested on x86_64-linux.

OK?

Ed


[-- Attachment #2: patch_pr61038 --]
[-- Type: text/plain, Size: 1523 bytes --]

Index: macro.c
===================================================================
--- macro.c	(revision 210315)
+++ macro.c	(working copy)
@@ -494,6 +494,9 @@
 		   || token->type == CPP_STRING16 || token->type == CPP_CHAR16
 		   || token->type == CPP_UTF8STRING);
 
+      escape_it = escape_it || cpp_userdef_string_p (token->type)
+			    || cpp_userdef_char_p (token->type);
+
       /* Room for each char being written in octal, initial space and
 	 final quote and NUL.  */
       len = cpp_token_len (token);
Index: ../gcc/testsuite/g++.dg/cpp0x/pr61038.C
===================================================================
--- ../gcc/testsuite/g++.dg/cpp0x/pr61038.C	(revision 0)
+++ ../gcc/testsuite/g++.dg/cpp0x/pr61038.C	(working copy)
@@ -0,0 +1,40 @@
+// PR c++/61038
+// { dg-do compile { target c++11 } }
+
+#include <cstring>
+#include <cstdlib>
+
+void
+operator "" _s(const char *, unsigned long)
+{ }
+
+void
+operator "" _t(const char)
+{ }
+
+#define QUOTE(s) #s
+#define QQUOTE(s) QUOTE(s)
+
+int
+main()
+{
+  const char *s = QQUOTE(QUOTE("hello"_s));
+  const char *t = QUOTE("\"hello\"_s");
+  if (strcmp(s, t) != 0)
+    abort();
+
+  const char *c = QQUOTE(QUOTE('"'_t));
+  const char *d = QUOTE("'\"'_t");
+  if (strcmp(c, d) != 0)
+    abort();
+
+  const char *e = QQUOTE(QUOTE('\''_t));
+  const char *f = QUOTE("'\\''_t");
+  if (strcmp(e, f) != 0)
+    abort();
+
+  const char *g = QQUOTE(QUOTE('\\'_t));
+  const char *h = QUOTE("'\\\\'_t");
+  if (strcmp(g, h) != 0)
+    abort();
+}

[-- Attachment #3: CL_pr61038 --]
[-- Type: text/plain, Size: 241 bytes --]


libcpp/

2014-05-12  Edward Smith-Rowland  <3dw4rd@verizon.net>

	PR C++/61038
	* macro.c (stringify_arg (cpp_reader *, macro_arg *)):
	Check for user-defined literal strings and user-defined literal chars
	to escape necessary characters.


[-- Attachment #4: CL_pr61038 --]
[-- Type: text/plain, Size: 120 bytes --]


gcc/testsuite/

2014-05-12  Edward Smith-Rowland  <3dw4rd@verizon.net>

	PR C++/61038
	* g++.dg/cpp0x/pr61038.C: New.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings
  2014-05-14  0:59   ` Ed Smith-Rowland
@ 2014-05-20 20:44     ` Jason Merrill
  2014-06-05 11:18       ` Ed Smith-Rowland
  0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2014-05-20 20:44 UTC (permalink / raw)
  To: Ed Smith-Rowland, Joseph S. Myers; +Cc: gcc-patches

On 05/13/2014 08:59 PM, Ed Smith-Rowland wrote:
> +      escape_it = escape_it || cpp_userdef_string_p (token->type)
> +			    || cpp_userdef_char_p (token->type);

Let's add the new cases to the previous statement instead of a new one. 
  OK with that change.

Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings
  2014-05-13  2:38 [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings Ed Smith-Rowland
  2014-05-13 17:29 ` Joseph S. Myers
@ 2014-05-21  9:37 ` Andreas Schwab
  1 sibling, 0 replies; 6+ messages in thread
From: Andreas Schwab @ 2014-05-21  9:37 UTC (permalink / raw)
  To: Ed Smith-Rowland; +Cc: gcc-patches, Jason Merrill

Ed Smith-Rowland <3dw4rd@verizon.net> writes:

> Index: ../gcc/testsuite/g++.dg/cpp0x/pr61038.C
> ===================================================================
> --- ../gcc/testsuite/g++.dg/cpp0x/pr61038.C	(revision 0)
> +++ ../gcc/testsuite/g++.dg/cpp0x/pr61038.C	(working copy)
> @@ -0,0 +1,23 @@
> +// PR c++/61038
> +// { dg-do compile { target c++11 } }
> +// { dg-options "-E" }
> +
> +void
> +operator "" _s(const char *, unsigned long)

../../gcc/gcc/testsuite/g++.dg/cpp0x/pr61038.C:8:43: error: ‘void operator""_s(const char*, long unsigned int)’ has invalid argument list

Andreas.

	* g++.dg/cpp0x/pr61038.C (operator "" _s): Use size_t.

Index: g++.dg/cpp0x/pr61038.C
===================================================================
--- g++.dg/cpp0x/pr61038.C	(revision 210686)
+++ g++.dg/cpp0x/pr61038.C	(working copy)
@@ -5,7 +5,7 @@
 #include <cstdlib>
 
 void
-operator "" _s(const char *, unsigned long)
+operator "" _s(const char *, size_t)
 { }
 
 void

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings
  2014-05-20 20:44     ` Jason Merrill
@ 2014-06-05 11:18       ` Ed Smith-Rowland
  0 siblings, 0 replies; 6+ messages in thread
From: Ed Smith-Rowland @ 2014-06-05 11:18 UTC (permalink / raw)
  To: Jason Merrill, Joseph S. Myers; +Cc: gcc-patches

On 05/20/2014 04:44 PM, Jason Merrill wrote:
> On 05/13/2014 08:59 PM, Ed Smith-Rowland wrote:
>> +      escape_it = escape_it || cpp_userdef_string_p (token->type)
>> +                || cpp_userdef_char_p (token->type);
>
> Let's add the new cases to the previous statement instead of a new 
> one.  OK with that change.
>
> Jason
>
>
PR c++/61038
I was asked to combine the escape logic for regular chars and strings
with the escape logic for user-defined literals chars and strings.
I just forgot the first time.

After rebuilding and testing committed as obvious.

ed@bad-horse:~/gcc_literal$ svn diff -rPREV  libcpp/ChangeLog 
libcpp/macro.c
Index: libcpp/ChangeLog
===================================================================
--- libcpp/ChangeLog    (revision 211266)
+++ libcpp/ChangeLog    (working copy)
@@ -1,3 +1,9 @@
+2014-06-04  Edward Smith-Rowland  <3dw4rd@verizon.net>
+
+    PR c++/61038
+    * macro.c (stringify_arg (cpp_reader *, macro_arg *)):
+    Combine user-defined escape logic with the other string and char logic.
+
  2014-05-26  Richard Biener  <rguenther@suse.de>

      * configure.ac: Remove long long and __int64 type checks,
Index: libcpp/macro.c
===================================================================
--- libcpp/macro.c    (revision 211265)
+++ libcpp/macro.c    (working copy)
@@ -492,11 +492,10 @@
             || token->type == CPP_WSTRING || token->type == CPP_WCHAR
             || token->type == CPP_STRING32 || token->type == CPP_CHAR32
             || token->type == CPP_STRING16 || token->type == CPP_CHAR16
-           || token->type == CPP_UTF8STRING);
+           || token->type == CPP_UTF8STRING
+           || cpp_userdef_string_p (token->type)
+           || cpp_userdef_char_p (token->type));

-      escape_it = escape_it || cpp_userdef_string_p (token->type)
-                || cpp_userdef_char_p (token->type);
-
        /* Room for each char being written in octal, initial space and
       final quote and NUL.  */
        len = cpp_token_len (token);

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-06-05 11:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-13  2:38 [PATCH, PR C++/61038] - g++ -E is unusable with UDL strings Ed Smith-Rowland
2014-05-13 17:29 ` Joseph S. Myers
2014-05-14  0:59   ` Ed Smith-Rowland
2014-05-20 20:44     ` Jason Merrill
2014-06-05 11:18       ` Ed Smith-Rowland
2014-05-21  9:37 ` Andreas Schwab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).