[C++ Patch] PR 80955 (Macros expanded in definition of user-defined literals)

Paolo Carlini paolo.carlini@oracle.com
Mon Nov 6 10:30:00 GMT 2017


Hi,

On 04/11/2017 23:37, Mukesh Kapoor wrote:
> I had included <inttypes.h> to get the definition of macro PRId64. I 
> have now modified the test case to remove all includes. I have added 
> the definition of the macro in the test case and also added 
> declarations of functions sprintf() strcmp(). I have attached the 
> revised patch. Thanks.
Excellent. I'm now committing the patch with a couple of additional 
tweaks to the testcase: 1- Since you carefully constructed it to return 
zero at run time if we do the right thing, you want a 'dg-do run' not a 
'dg-do compile'; 2- You also want a long ia64 variable, otherwise the 
testscase triggers a -Wformat warning.

Thanks,
Paolo.

////////////////////
-------------- next part --------------
Index: gcc/testsuite/g++.dg/cpp0x/udlit-macros.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/udlit-macros.C	(nonexistent)
+++ gcc/testsuite/g++.dg/cpp0x/udlit-macros.C	(working copy)
@@ -0,0 +1,31 @@
+// PR c++/80955
+// { dg-do run { target c++11 } }
+
+extern "C" int sprintf (char *s, const char *format, ...);
+extern "C" int strcmp (const char *s1, const char *s2);
+
+#define __PRI64_PREFIX        "l"
+#define PRId64         __PRI64_PREFIX "d"
+
+using size_t = decltype(sizeof(0));
+#define _zero
+#define _ID _xx
+int operator""_zero(const char*, size_t) { return 0; }
+int operator""_ID(const char*, size_t) { return 0; }
+
+int main()
+{
+  long i64 = 123;
+  char buf[100];
+  sprintf(buf, "%"PRId64"abc", i64);  // { dg-warning "invalid suffix on literal" }
+  return strcmp(buf, "123abc")
+	 + ""_zero
+	 + "bob"_zero
+	 + R"#(raw
+	       string)#"_zero
+	 + "xx"_ID
+	 + ""_ID
+	 + R"AA(another
+		raw
+		string)AA"_ID;
+}
Index: libcpp/lex.c
===================================================================
--- libcpp/lex.c	(revision 254432)
+++ libcpp/lex.c	(working copy)
@@ -1871,8 +1871,9 @@ lex_raw_string (cpp_reader *pfile, cpp_token *toke
       /* If a string format macro, say from inttypes.h, is placed touching
 	 a string literal it could be parsed as a C++11 user-defined string
 	 literal thus breaking the program.
-	 Try to identify macros with is_macro. A warning is issued. */
-      if (is_macro (pfile, cur))
+	 Try to identify macros with is_macro. A warning is issued.
+	 The macro name should not start with '_' for this warning. */
+      if ((*cur != '_') && is_macro (pfile, cur))
 	{
 	  /* Raise a warning, but do not consume subsequent tokens.  */
 	  if (CPP_OPTION (pfile, warn_literal_suffix) && !pfile->state.skipping)
@@ -2001,8 +2002,9 @@ lex_string (cpp_reader *pfile, cpp_token *token, c
       /* If a string format macro, say from inttypes.h, is placed touching
 	 a string literal it could be parsed as a C++11 user-defined string
 	 literal thus breaking the program.
-	 Try to identify macros with is_macro. A warning is issued. */
-      if (is_macro (pfile, cur))
+	 Try to identify macros with is_macro. A warning is issued.
+	 The macro name should not start with '_' for this warning. */
+      if ((*cur != '_') && is_macro (pfile, cur))
 	{
 	  /* Raise a warning, but do not consume subsequent tokens.  */
 	  if (CPP_OPTION (pfile, warn_literal_suffix) && !pfile->state.skipping)


More information about the Gcc-patches mailing list