From 6394a341d6c4a69d61b29618bf8837002eca580f Mon Sep 17 00:00:00 2001 From: Matthias Kretz Date: Wed, 6 Nov 2019 16:06:08 +0000 Subject: [PATCH] Fix parser to recognize operator?: This change lets grok_op_properties print its useful "ISO C++ prohibits overloading operator ?:" message instead of the cryptic error message about a missing type-specifier before '?' token. 2019-11-06 Matthias Kretz * parser.c (cp_parser_operator): Parse operator?: as an attempt to overload the conditional operator. From-SVN: r277887 --- gcc/cp/ChangeLog | 5 +++++ gcc/cp/parser.c | 9 +++++++++ gcc/testsuite/g++.dg/parse/operator9.C | 5 +++++ gcc/testsuite/g++.old-deja/g++.jason/operator.C | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/parse/operator9.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d0b47f7c562..e60a45b869e 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2019-11-06 Matthias Kretz + + * parser.c (cp_parser_operator): Parse operator?: as an + attempt to overload the conditional operator. + 2019-11-05 Jason Merrill Implement C++20 operator<=>. diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index cbbf946d32c..b17e0336e1c 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -15542,6 +15542,15 @@ cp_parser_operator (cp_parser* parser, location_t start_loc) op = COMPONENT_REF; break; + case CPP_QUERY: + op = COND_EXPR; + /* Consume the `?'. */ + cp_lexer_consume_token (parser->lexer); + /* Look for the matching `:'. */ + cp_parser_require (parser, CPP_COLON, RT_COLON); + consumed = true; + break; + case CPP_OPEN_PAREN: { /* Consume the `('. */ diff --git a/gcc/testsuite/g++.dg/parse/operator9.C b/gcc/testsuite/g++.dg/parse/operator9.C new file mode 100644 index 00000000000..d66355afab5 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/operator9.C @@ -0,0 +1,5 @@ +// { dg-do compile } + +struct A {}; +struct B {}; +int operator?:(bool, A, B); // { dg-error "prohibits overloading" } diff --git a/gcc/testsuite/g++.old-deja/g++.jason/operator.C b/gcc/testsuite/g++.old-deja/g++.jason/operator.C index c2fc212cef0..69a41cf2448 100644 --- a/gcc/testsuite/g++.old-deja/g++.jason/operator.C +++ b/gcc/testsuite/g++.old-deja/g++.jason/operator.C @@ -5,7 +5,7 @@ typedef __SIZE_TYPE__ size_t; struct A { - int operator?:(int a, int b); // { dg-error "expected type-specifier" } + int operator?:(int a, int b); // { dg-error "prohibits overloading" } static int operator()(int a); // { dg-error "14:.static int A::operator\\(\\)\\(int\\). must be a nonstatic member function" } static int operator+(A,A); // { dg-error "14:.static int A::operator\\+\\(A, A\\). must be either a non-static member function or a non-member function" } int operator+(int a, int b = 1); // { dg-error "7:.int A::operator\\+\\(int, int\\). must have either zero or one argument" } -- 2.30.2