From 7d19c460ed95b5e0988f1ba37ce614ddd8d27aa8 Mon Sep 17 00:00:00 2001 From: Mukesh Kapoor Date: Mon, 6 Nov 2017 10:33:41 +0000 Subject: [PATCH] re PR c++/80955 (Macros expanded in definition of user-defined literals) /libcpp 2017-11-06 Mukesh Kapoor PR c++/80955 * lex.c (lex_string): When checking for a valid macro for the warning related to -Wliteral-suffix (CPP_W_LITERAL_SUFFIX), check that the macro name does not start with an underscore before calling is_macro(). /gcc/testsuite 2017-11-06 Mukesh Kapoor PR c++/80955 * g++.dg/cpp0x/udlit-macros.C: New. From-SVN: r254443 --- gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/g++.dg/cpp0x/udlit-macros.C | 31 +++++++++++++++++++++++ libcpp/ChangeLog | 8 ++++++ libcpp/lex.c | 10 +++++--- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/udlit-macros.C diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8232b4a4add..b56ace44321 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-11-06 Mukesh Kapoor + + PR c++/80955 + * g++.dg/cpp0x/udlit-macros.C: New. + 2017-11-06 Paul Thomas PR fortran/69739 diff --git a/gcc/testsuite/g++.dg/cpp0x/udlit-macros.C b/gcc/testsuite/g++.dg/cpp0x/udlit-macros.C new file mode 100644 index 00000000000..fb518281811 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/udlit-macros.C @@ -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; +} diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 285f4143d5f..86c42affded 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,11 @@ +2017-11-06 Mukesh Kapoor + + PR c++/80955 + * lex.c (lex_string): When checking for a valid macro for the + warning related to -Wliteral-suffix (CPP_W_LITERAL_SUFFIX), + check that the macro name does not start with an underscore + before calling is_macro(). + 2017-11-05 Tom de Vries PR other/82784 diff --git a/libcpp/lex.c b/libcpp/lex.c index 9164a0745b2..8af09e50295 100644 --- a/libcpp/lex.c +++ b/libcpp/lex.c @@ -1871,8 +1871,9 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base, /* 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, const uchar *base) /* 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) -- 2.30.2