This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] preprocessor stringizing raw strings


This patch fixes PR 82506, where we fail to properly stringize a raw string literal, which can contain a raw LF character.

When we're not just preprocessing, there isn't a problem. The string literal gets correctly escaped into the assembly file. This is just a problem with preprocessing.

Applying to trunk.

nathan
--
Nathan Sidwell
2017-10-10  Nathan Sidwell  <nathan@acm.org>

	libcpp/
	PR preprocessor/82506
	* macro.c (cpp_quote_string): Escape raw LFs.

	gcc/testsuite/
	PR preprocessor/82506
	* g++.dg/cpp/string-3.C: New.

Index: gcc/testsuite/g++.dg/cpp/string-3.C
===================================================================
--- gcc/testsuite/g++.dg/cpp/string-3.C	(revision 0)
+++ gcc/testsuite/g++.dg/cpp/string-3.C	(working copy)
@@ -0,0 +1,9 @@
+// PR c++/82506
+// { dg-do preprocess { target c++11 } }
+
+#define STRINGIZE(A) #A
+
+BEGIN STRINGIZE(R"(
+)") END
+
+// { dg-final { scan-file string-3.ii "BEGIN \"R\\\"(\\n)\\\"\"\n END" } }
Index: libcpp/macro.c
===================================================================
--- libcpp/macro.c	(revision 253587)
+++ libcpp/macro.c	(working copy)
@@ -502,13 +502,21 @@ cpp_quote_string (uchar *dest, const uch
     {
       uchar c = *src++;
 
-      if (c == '\\' || c == '"')
+      switch (c)
 	{
+	case '\n':
+	  /* Naked LF can appear in raw string literals  */
+	  c = 'n';
+	  /* FALLTHROUGH */
+
+	case '\\':
+	case '"':
 	  *dest++ = '\\';
+	  /* FALLTHROUGH */
+
+	default:
 	  *dest++ = c;
 	}
-      else
-	  *dest++ = c;
     }
 
   return dest;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]