From cfc935327585254eabd5c18f298b1b4bc28a2b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20L=C3=B3pez-Ib=C3=A1=C3=B1ez?= Date: Wed, 22 Apr 2009 15:32:18 +0000 Subject: [PATCH] re PR c++/14875 (When using 'or' keyword, the error message speaks of a '||' token) 2009-04-22 Manuel Lopez-Ibanez PR c++/14875 * c-common.c (c_parse_error): Take a token_flags parameter. Use token_type for the token type instead. Pass token_flags to cpp_type2name. * c-common.h (c_parse_error): Update declaration. * c-parser.c (c_parser_error): Pass 0 as token flags. libcpp/ * lex.c (cpp_type2name): Take a flags parameter. Call cpp_named_operator2name for named operators and cpp_digraph2name for digraphs. (cpp_digraph2name): New. (cpp_spell_token): Use it. (cpp_output_token): Likewise. * include/cpplib.h (cpp_type2name): Update declaration. * init.c (cpp_named_operator2name): New. * internal.h (cpp_named_operator2name): Declare. cp/ * parser.c (cp_parser_error): Pass token->flags to c_parse_error. testsuite/ * g++.dg/parse/parser-pr14875.C: New. * g++.dg/parse/parser-pr14875-2.C: New. * g++.dg/parse/error6.C: Update match string. From-SVN: r146589 --- gcc/ChangeLog | 9 ++++++ gcc/c-common.c | 31 +++++++++++-------- gcc/c-common.h | 2 +- gcc/c-parser.c | 6 +++- gcc/cp/ChangeLog | 5 +++ gcc/cp/parser.c | 2 +- gcc/testsuite/ChangeLog | 7 +++++ gcc/testsuite/g++.dg/parse/error6.C | 2 +- gcc/testsuite/g++.dg/parse/parser-pr14875-2.C | 31 +++++++++++++++++++ gcc/testsuite/g++.dg/parse/parser-pr14875.C | 20 ++++++++++++ libcpp/ChangeLog | 13 ++++++++ libcpp/include/cpplib.h | 2 +- libcpp/init.c | 18 +++++++++++ libcpp/internal.h | 1 + libcpp/lex.c | 25 ++++++++++----- 15 files changed, 149 insertions(+), 25 deletions(-) create mode 100644 gcc/testsuite/g++.dg/parse/parser-pr14875-2.C create mode 100644 gcc/testsuite/g++.dg/parse/parser-pr14875.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1745a365e862..f037097d36e8 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2009-04-22 Manuel Lopez-Ibanez + + PR c++/14875 + * c-common.c (c_parse_error): Take a token_flags parameter. + Use token_type for the token type instead. + Pass token_flags to cpp_type2name. + * c-common.h (c_parse_error): Update declaration. + * c-parser.c (c_parser_error): Pass 0 as token flags. + 2009-04-22 Andrey Belevantsev PR rtl-optimization/39580 diff --git a/gcc/c-common.c b/gcc/c-common.c index 3595e552966f..67fe8ad8ebd9 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -8167,21 +8167,24 @@ catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) TOKEN, which had the associated VALUE. */ void -c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value) +c_parse_error (const char *gmsgid, enum cpp_ttype token_type, + tree value, unsigned char token_flags) { #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) char *message = NULL; - if (token == CPP_EOF) + if (token_type == CPP_EOF) message = catenate_messages (gmsgid, " at end of input"); - else if (token == CPP_CHAR || token == CPP_WCHAR || token == CPP_CHAR16 - || token == CPP_CHAR32) + else if (token_type == CPP_CHAR + || token_type == CPP_WCHAR + || token_type == CPP_CHAR16 + || token_type == CPP_CHAR32) { unsigned int val = TREE_INT_CST_LOW (value); const char *prefix; - switch (token) + switch (token_type) { default: prefix = ""; @@ -8206,26 +8209,28 @@ c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value) free (message); message = NULL; } - else if (token == CPP_STRING || token == CPP_WSTRING || token == CPP_STRING16 - || token == CPP_STRING32) + else if (token_type == CPP_STRING + || token_type == CPP_WSTRING + || token_type == CPP_STRING16 + || token_type == CPP_STRING32) message = catenate_messages (gmsgid, " before string constant"); - else if (token == CPP_NUMBER) + else if (token_type == CPP_NUMBER) message = catenate_messages (gmsgid, " before numeric constant"); - else if (token == CPP_NAME) + else if (token_type == CPP_NAME) { message = catenate_messages (gmsgid, " before %qE"); error (message, value); free (message); message = NULL; } - else if (token == CPP_PRAGMA) + else if (token_type == CPP_PRAGMA) message = catenate_messages (gmsgid, " before %<#pragma%>"); - else if (token == CPP_PRAGMA_EOL) + else if (token_type == CPP_PRAGMA_EOL) message = catenate_messages (gmsgid, " before end of line"); - else if (token < N_TTYPES) + else if (token_type < N_TTYPES) { message = catenate_messages (gmsgid, " before %qs token"); - error (message, cpp_type2name (token)); + error (message, cpp_type2name (token_type, token_flags)); free (message); message = NULL; } diff --git a/gcc/c-common.h b/gcc/c-common.h index a46da6bcb7d0..ec47a023bfc1 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -1029,7 +1029,7 @@ extern void builtin_define_std (const char *macro); extern void builtin_define_with_value (const char *, const char *, int); extern void c_stddef_cpp_builtins (void); extern void fe_file_change (const struct line_map *); -extern void c_parse_error (const char *, enum cpp_ttype, tree); +extern void c_parse_error (const char *, enum cpp_ttype, tree, unsigned char); /* Objective-C / Objective-C++ entry points. */ diff --git a/gcc/c-parser.c b/gcc/c-parser.c index 676c709cdf2f..ed77098b4d39 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -573,7 +573,11 @@ c_parser_error (c_parser *parser, const char *gmsgid) CPP_KEYWORD, keywords are treated like identifiers. */ (token->type == CPP_KEYWORD ? CPP_NAME : token->type), - token->value); + /* ??? The C parser does not save the cpp flags of a + token, we need to pass 0 here and we will not get + the source spelling of some tokens but rather the + canonical spelling. */ + token->value, /*flags=*/0); } /* If the next token is of the indicated TYPE, consume it. Otherwise, diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 99c78261654d..1b67ad879442 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2009-04-22 Manuel Lopez-Ibanez + + PR c++/14875 + * parser.c (cp_parser_error): Pass token->flags to c_parse_error. + 2009-04-21 Manuel Lopez-Ibanez PR c++/35711 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index b6b8bf598334..fbf211bfb9f3 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -2091,7 +2091,7 @@ cp_parser_error (cp_parser* parser, const char* message) CPP_KEYWORD, keywords are treated like identifiers. */ (token->type == CPP_KEYWORD ? CPP_NAME : token->type), - token->u.value); + token->u.value, token->flags); } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6fa530d460cb..386ce8cae694 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2009-04-22 Manuel Lopez-Ibanez + + PR c++/14875 + * g++.dg/parse/parser-pr14875.C: New. + * g++.dg/parse/parser-pr14875-2.C: New. + * g++.dg/parse/error6.C: Update match string. + 2009-04-22 Richard Guenther PR tree-optimization/39845 diff --git a/gcc/testsuite/g++.dg/parse/error6.C b/gcc/testsuite/g++.dg/parse/error6.C index 81e04e21722e..8b8424fe30ac 100644 --- a/gcc/testsuite/g++.dg/parse/error6.C +++ b/gcc/testsuite/g++.dg/parse/error6.C @@ -5,7 +5,7 @@ int f(int not) { return 1-not; } -// { dg-error "11:expected ',' or '...' before '!' token" "" { target *-*-* } 4 } +// { dg-error "11:expected ',' or '...' before 'not' token" "" { target *-*-* } 4 } // { dg-error "15:expected primary\\-expression before ';' token" "" { target *-*-* } 5 } diff --git a/gcc/testsuite/g++.dg/parse/parser-pr14875-2.C b/gcc/testsuite/g++.dg/parse/parser-pr14875-2.C new file mode 100644 index 000000000000..3510aac908ff --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/parser-pr14875-2.C @@ -0,0 +1,31 @@ +// PR 14875: When using 'or' keyword, the error message speaks of a '||' token +// { dg-do compile } +// { dg-options "" } +#define CHECK(x) void ::x + CHECK (and); // { dg-error "before .and. token" } + CHECK (and_eq); // { dg-error "before .and_eq. token" } + CHECK (bitand); // { dg-error "before .bitand. token" } + CHECK (bitor); // { dg-error "before .bitor. token" } + CHECK (compl); // { dg-error "before .compl. token" } + CHECK (not); // { dg-error "before .not. token" } + CHECK (not_eq); // { dg-error "before .not_eq. token" } + CHECK (or); // { dg-error "before .or. token" } + CHECK (or_eq); // { dg-error "before .or_eq. token" } + CHECK (xor); // { dg-error "before .xor. token" } + CHECK (xor_eq); // { dg-error "before .xor_eq. token" } +#undef CHECK +#define CHECK(x) int x + CHECK (<:); // { dg-error "before .<:. token" } + CHECK (:>); // { dg-error "before .:>. token" } +#undef CHECK +#define CHECK(x) x + CHECK (<%); // { dg-error "before .<%. token" } +#undef CHECK +#define CHECK(x) x x + CHECK (%>); // { dg-error "before .%>. token" } +#undef CHECK +#define CHECK(x) x + CHECK (%:); // { dg-error "stray .%:. " } + CHECK (%:%:); // { dg-error "stray .%:%:. " } + + diff --git a/gcc/testsuite/g++.dg/parse/parser-pr14875.C b/gcc/testsuite/g++.dg/parse/parser-pr14875.C new file mode 100644 index 000000000000..7a6f0618681e --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/parser-pr14875.C @@ -0,0 +1,20 @@ +// PR 14875: When using 'or' keyword, the error message speaks of a '||' token +// { dg-do compile } +// { dg-options "" } +using namespace std; + +class Sample +{ + +public: + Sample(); + void or(long Digital); // { dg-error "before .or. token" } +}; + +Sample::Sample() +{ +} + +void Sample::or(long Digital) // { dg-error "before .or. token" } +{ +} diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 7fdc737239f0..ff720612a567 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,16 @@ +2009-04-22 Manuel Lopez-Ibanez + + PR c++/14875 + * lex.c (cpp_type2name): Take a flags parameter. Call + cpp_named_operator2name for named operators and cpp_digraph2name + for digraphs. + (cpp_digraph2name): New. + (cpp_spell_token): Use it. + (cpp_output_token): Likewise. + * include/cpplib.h (cpp_type2name): Update declaration. + * init.c (cpp_named_operator2name): New. + * internal.h (cpp_named_operator2name): Declare. + 2009-04-21 Manuel Lopez-Ibanez PR c++/13358 diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 3aeb035f48f4..b38d9f43ef5e 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -841,7 +841,7 @@ extern void cpp_output_line (cpp_reader *, FILE *); extern unsigned char *cpp_output_line_to_string (cpp_reader *, const unsigned char *); extern void cpp_output_token (const cpp_token *, FILE *); -extern const char *cpp_type2name (enum cpp_ttype); +extern const char *cpp_type2name (enum cpp_ttype, unsigned char flags); /* Returns the value of an escape sequence, truncated to the correct target precision. PSTR points to the input pointer, which is just after the backslash. LIMIT is how much text we have. WIDE is true diff --git a/libcpp/init.c b/libcpp/init.c index c21121707d38..aef39981792e 100644 --- a/libcpp/init.c +++ b/libcpp/init.c @@ -381,6 +381,24 @@ mark_named_operators (cpp_reader *pfile) } } +/* Helper function of cpp_type2name. Return the string associated with + named operator TYPE. */ +const char * +cpp_named_operator2name (enum cpp_ttype type) +{ + const struct builtin_operator *b; + + for (b = operator_array; + b < (operator_array + ARRAY_SIZE (operator_array)); + b++) + { + if (type == b->value) + return (const char *) b->name; + } + + return NULL; +} + void cpp_init_special_builtins (cpp_reader *pfile) { diff --git a/libcpp/internal.h b/libcpp/internal.h index 4368ebbe998f..22e6d238906a 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -570,6 +570,7 @@ extern void _cpp_init_tokenrun (tokenrun *, unsigned int); /* In init.c. */ extern void _cpp_maybe_push_include_file (cpp_reader *); +extern const char *cpp_named_operator2name (enum cpp_ttype type); /* In directives.c */ extern int _cpp_test_assertion (cpp_reader *, unsigned int *); diff --git a/libcpp/lex.c b/libcpp/lex.c index 63e291c64c0e..af5c06a7abcf 100644 --- a/libcpp/lex.c +++ b/libcpp/lex.c @@ -1416,6 +1416,13 @@ utf8_to_ucn (unsigned char *buffer, const unsigned char *name) return ucn_len; } +/* Given a token TYPE corresponding to a digraph, return a pointer to + the spelling of the digraph. */ +static const unsigned char * +cpp_digraph2name (enum cpp_ttype type) +{ + return digraph_spellings[(int) type - (int) CPP_FIRST_DIGRAPH]; +} /* Write the spelling of a token TOKEN to BUFFER. The buffer must already contain the enough space to hold the token's spelling. @@ -1435,8 +1442,7 @@ cpp_spell_token (cpp_reader *pfile, const cpp_token *token, unsigned char c; if (token->flags & DIGRAPH) - spelling - = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH]; + spelling = cpp_digraph2name (token->type); else if (token->flags & NAMED_OP) goto spell_ident; else @@ -1499,11 +1505,17 @@ cpp_token_as_text (cpp_reader *pfile, const cpp_token *token) return start; } -/* Used by C front ends, which really should move to using - cpp_token_as_text. */ +/* Returns a pointer to a string which spells the token defined by + TYPE and FLAGS. Used by C front ends, which really should move to + using cpp_token_as_text. */ const char * -cpp_type2name (enum cpp_ttype type) +cpp_type2name (enum cpp_ttype type, unsigned char flags) { + if (flags & DIGRAPH) + return (const char *) cpp_digraph2name (type); + else if (flags & NAMED_OP) + return cpp_named_operator2name (type); + return (const char *) token_spellings[type].name; } @@ -1521,8 +1533,7 @@ cpp_output_token (const cpp_token *token, FILE *fp) int c; if (token->flags & DIGRAPH) - spelling - = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH]; + spelling = cpp_digraph2name (token->type); else if (token->flags & NAMED_OP) goto spell_ident; else