Canonicalize conversion operators
Commit Message
Consider a conversion operator such as:
operator foo const* const* ();
There are two small parser problems, highlighted by this test:
(gdb) p operator foo const* const*
There is no field named operatorfoo const* const *
GDB is looking up the symbol "operatorfoo const* const*" -- it is missing a
space between the keyword "operator" and the type name "foo const* const*".
Pedro also discovered this problem and has a patch for it pending on his
cxx-breakpoint-improvements branch.
However, something not address by Pedro's patch is that this input of the
user-defined type needs to be canonicalized so that different "spellings" of
the type are recognized:
(gdb) p operator const foo* const *
There is no field named operator const foo* const *
gdb/ChangeLog:
* c-exp.y (oper): Canonicalize conversion operators of user-defined
types.
Add whitespace to front of type name.
gdb/testsuite/ChangeLog:
* gdb.cp/cpexprs.cc (base) <operator fluff const* const*>: New
method.
(main): Call it.
* gdb.cp/cpexprs.exp: Add new conversion operator to test matrix.
Add additional user-defined conversion operator tests.
---
gdb/c-exp.y | 10 ++++++++--
gdb/testsuite/gdb.cp/cpexprs.cc | 2 ++
gdb/testsuite/gdb.cp/cpexprs.exp | 16 ++++++++++++++++
3 files changed, 26 insertions(+), 2 deletions(-)
Comments
On 10/17/2017 11:12 PM, Keith Seitz wrote:
> Consider a conversion operator such as:
>
> operator foo const* const* ();
>
> There are two small parser problems, highlighted by this test:
>
> (gdb) p operator foo const* const*
> There is no field named operatorfoo const* const *
>
> GDB is looking up the symbol "operatorfoo const* const*" -- it is missing a
> space between the keyword "operator" and the type name "foo const* const*".
> Pedro also discovered this problem and has a patch for it pending on his
> cxx-breakpoint-improvements branch.
>
> However, something not address by Pedro's patch is that this input of the
> user-defined type needs to be canonicalized so that different "spellings" of
> the type are recognized:
>
> (gdb) p operator const foo* const *
> There is no field named operator const foo* const *
>
Indeed.
> @@ -1630,7 +1630,13 @@ oper: OPERATOR NEW
>
> c_print_type ($2, NULL, &buf, -1, 0,
> &type_print_raw_options);
> - $$ = operator_stoken (buf.c_str ());
> +
> + /* This also needs canonicalization. */
> + std::string canon
> + = " " + cp_canonicalize_string (buf.c_str ());
> + if (canon.length () == 1)
> + canon = " " + buf.string ();
> + $$ = operator_stoken (canon.c_str ());
The length() == 1 check gave me pause. It's checking that
cp_canonicalize_string returned empty of course, but it
wasn't super clear on a quick skim.
I think you could write it like this, making that part clearer,
and also saving a few string dups and copies:
/* This also needs canonicalization. */
std::string canon
= cp_canonicalize_string (buf.c_str ());
if (canon.empty ())
canon = std::move (buf.string ());
$$ = operator_stoken ((" " + canon).c_str ());
>
> +# Make sure conversion operator names are canonicalized and properly
> +# "spelled."
Period should outside the quote, I think.
Otherwise OK.
Thanks,
Pedro Alves
On 10/18/2017 04:19 AM, Pedro Alves wrote:
>> @@ -1630,7 +1630,13 @@ oper: OPERATOR NEW
>>
>> c_print_type ($2, NULL, &buf, -1, 0,
>> &type_print_raw_options);
>> - $$ = operator_stoken (buf.c_str ());
>> +
>> + /* This also needs canonicalization. */
>> + std::string canon
>> + = " " + cp_canonicalize_string (buf.c_str ());
>> + if (canon.length () == 1)
>> + canon = " " + buf.string ();
>> + $$ = operator_stoken (canon.c_str ());
>
> The length() == 1 check gave me pause. It's checking that
> cp_canonicalize_string returned empty of course, but it
> wasn't super clear on a quick skim.
>
> I think you could write it like this, making that part clearer,
> and also saving a few string dups and copies:
>
> /* This also needs canonicalization. */
> std::string canon
> = cp_canonicalize_string (buf.c_str ());
> if (canon.empty ())
> canon = std::move (buf.string ());
> $$ = operator_stoken ((" " + canon).c_str ());
>
>
Yes, indeed. ISTR having something similar to that at one point, but I don't know why I changed it. [Maybe that was during the C++03/C++11 shake out?]
In any case, committed with that change.
Thank you for the review.
Keith
@@ -1554,7 +1554,7 @@ oper: OPERATOR NEW
| OPERATOR '>'
{ $$ = operator_stoken (">"); }
| OPERATOR ASSIGN_MODIFY
- { const char *op = "unknown";
+ { const char *op = " unknown";
switch ($2)
{
case BINOP_RSH:
@@ -1630,7 +1630,13 @@ oper: OPERATOR NEW
c_print_type ($2, NULL, &buf, -1, 0,
&type_print_raw_options);
- $$ = operator_stoken (buf.c_str ());
+
+ /* This also needs canonicalization. */
+ std::string canon
+ = " " + cp_canonicalize_string (buf.c_str ());
+ if (canon.length () == 1)
+ canon = " " + buf.string ();
+ $$ = operator_stoken (canon.c_str ());
}
;
@@ -270,6 +270,7 @@ public:
operator int () const { return 21; } // base::operator int
operator fluff* () const { return new fluff (); } // base::operator fluff*
operator fluff** () const { return &g_fluff; } // base::operator fluff**
+ operator fluff const* const* () const { return &g_fluff; } // base::operator fluff const* const*
};
class base1 : public virtual base
@@ -448,6 +449,7 @@ test_function (int argc, char* argv[]) // test_function
char* str = a;
fluff* flp = a;
fluff** flpp = a;
+ fluff const* const* flcpcp = a;
CV_f(CV::i);
@@ -407,6 +407,10 @@ add {base::operator int} \
{int (const base * const)} \
- \
-
+add {base::operator fluff const* const*} \
+ {const fluff * const *(const base * const)} \
+ - \
+ -
# Templates
add {tclass<char>::do_something} \
@@ -746,5 +750,17 @@ gdb_test "p CV_f(CV::i)" " = 43"
gdb_test "p CV_f('cpexprs.cc'::CV::t)" \
{ = {int \(int\)} 0x[0-9a-f]+ <CV_f\(int\)>}
+# Make sure conversion operator names are canonicalized and properly
+# "spelled."
+gdb_test "p base::operator const fluff * const *" \
+ [get "base::operator fluff const* const*" print] \
+ "canonicalized conversion operator name 1"
+gdb_test "p base::operator const fluff* const*" \
+ [get "base::operator fluff const* const*" print] \
+ "canonicalized conversion operator name 2"
+gdb_test "p base::operator derived*" \
+ "There is no field named operator derived\\*" \
+ "undefined conversion operator"
+
gdb_exit
return 0