[rfc] Be in language c more c++ compatible

Jan Kratochvil
2011-07-15T19:19:46Z

Hi,

I always see some terrible C++ GDB bug so I fix in some hours and then find out
it works in GDB when one has `set language c++'. This usually happens with
artificial testcases like the one today:
http://sourceware.org/ml/gdb-patches/2011-07/msg00387.html

I was told by Keith it also happens to him.

echo 'class C { typedef int t; t i; } c;'|g++ -c -o 1.o -g -x c++ -

FSF GDB HEAD
(gdb) show language
The current source language is "auto; currently c".
(gdb) whatis C::t
type = C::t
(gdb) set language c++
(gdb) whatis C::t
type = int

patched FSF GDB HEAD:
(gdb) show language
The current source language is "auto; currently c".
(gdb) whatis C::t
type = int
(gdb) set language c++
(gdb) whatis C::t
type = int

The patch has two parts (two files), each part is sufficient to fix the problem
above.

The first (c-exp.y) part parses those parts of `language c++' which cannot
(I believe - RFC) lead to misinterpretation of any valid C code even in
`language c'.

The second (valops.c) part is there because with `language c++' parsing "C::t"
is straight OP_TYPE handled correctly in evaluate_subexp_standard. But with
`language c' it gets parsed as OP_SCOPE and I find there a bug, copied the code
there.

Sure this patch is not a complete implementation of the idea, just an
incremental improvement.

No regressions on {x86_64,x86_64-m32,i686}-fedora15-linux-gnu.

Thanks,
Jan

gdb/
2011-07-15 Jan Kratochvil <jan.kratochvil@redhat.com>

* c-exp.y (tokentab3, tokentab2): Remove the cxx_only field, comment it.
(lex_one_token): No longer check them there.
(yylex): Do not check for language_cplus.
* valops.c (value_maybe_namespace_elt): Dereference LOC_TYPEDEF for
EVAL_AVOID_SIDE_EFFECTS.

--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -1867,38 +1867,40 @@ struct token
int cxx_only;
};

+/* cxx_only is not used. */
static const struct token tokentab3[] =
{
- {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
- {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
- {"->*", ARROW_STAR, BINOP_END, 1}
+ {">>=", ASSIGN_MODIFY, BINOP_RSH},
+ {"<<=", ASSIGN_MODIFY, BINOP_LSH},
+ {"->*", ARROW_STAR, BINOP_END}
};

+/* cxx_only is not used. */
static const struct token tokentab2[] =
{
- {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
- {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
- {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
- {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
- {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
- {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
- {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
- {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
- {"++", INCREMENT, BINOP_END, 0},
- {"--", DECREMENT, BINOP_END, 0},
- {"->", ARROW, BINOP_END, 0},
- {"&&", ANDAND, BINOP_END, 0},
- {"||", OROR, BINOP_END, 0},
+ {"+=", ASSIGN_MODIFY, BINOP_ADD},
+ {"-=", ASSIGN_MODIFY, BINOP_SUB},
+ {"*=", ASSIGN_MODIFY, BINOP_MUL},
+ {"/=", ASSIGN_MODIFY, BINOP_DIV},
+ {"%=", ASSIGN_MODIFY, BINOP_REM},
+ {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
+ {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
+ {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
+ {"++", INCREMENT, BINOP_END},
+ {"--", DECREMENT, BINOP_END},
+ {"->", ARROW, BINOP_END},
+ {"&&", ANDAND, BINOP_END},
+ {"||", OROR, BINOP_END},
/* "::" is *not* only C++: gdb overrides its meaning in several
different ways, e.g., 'filename'::func, function::variable. */
- {"::", COLONCOLON, BINOP_END, 0},
- {"<<", LSH, BINOP_END, 0},
- {">>", RSH, BINOP_END, 0},
- {"==", EQUAL, BINOP_END, 0},
- {"!=", NOTEQUAL, BINOP_END, 0},
- {"<=", LEQ, BINOP_END, 0},
- {">=", GEQ, BINOP_END, 0},
- {".*", DOT_STAR, BINOP_END, 1}
+ {"::", COLONCOLON, BINOP_END},
+ {"<<", LSH, BINOP_END},
+ {">>", RSH, BINOP_END},
+ {"==", EQUAL, BINOP_END},
+ {"!=", NOTEQUAL, BINOP_END},
+ {"<=", LEQ, BINOP_END},
+ {">=", GEQ, BINOP_END},
+ {".*", DOT_STAR, BINOP_END}
};

/* Identifier-like tokens. */
@@ -2075,10 +2077,6 @@ lex_one_token (void)
for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
{
- if (tokentab3[i].cxx_only
- && parse_language->la_language != language_cplus)
- break;
-
lexptr += 3;
yylval.opcode = tokentab3[i].opcode;
return tokentab3[i].token;
@@ -2088,10 +2086,6 @@ lex_one_token (void)
for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
{
- if (tokentab2[i].cxx_only
- && parse_language->la_language != language_cplus)
- break;
-
lexptr += 2;
yylval.opcode = tokentab2[i].opcode;
if (in_parse_field && tokentab2[i].token == ARROW)
@@ -2525,8 +2519,7 @@ yylex (void)
current.token = lex_one_token ();
if (current.token == NAME)
current.token = classify_name (expression_context_block);
- if (parse_language->la_language != language_cplus
- || (current.token != TYPENAME && current.token != COLONCOLON))
+ if (current.token != TYPENAME && current.token != COLONCOLON)
return current.token;

first_was_coloncolon = current.token == COLONCOLON;
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -3501,7 +3501,16 @@ value_maybe_namespace_elt (const struct type *curtype,
return NULL;
else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
&& (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
- result = allocate_value (SYMBOL_TYPE (sym));
+ {
+ struct type *type = SYMBOL_TYPE (sym);
+
+ /* If this is a typedef, then find its immediate target. We use
+ check_typedef to resolve stubs, but we ignore its result because we do
+ not want to dig past all typedefs. */
+ check_typedef (type);
+ type = TYPE_TARGET_TYPE (type);
+ return allocate_value (type);
+ }
else
result = value_of_variable (sym, get_selected_block (0));