* Add debugging functions for cp_parser (issue4389045)
@ 2011-04-11 16:24 Diego Novillo
0 siblings, 0 replies; 7+ messages in thread
From: Diego Novillo @ 2011-04-11 16:24 UTC (permalink / raw)
To: reply, jason, crowl, gcc-patches
This patch adds a debugging function to print the state of a cp_parser
instance. It prints all the status fields in the structure and a window
of 20 tokens around the current token.
This patch is against the pph branch, but it should apply almost
cleanly to trunk.
OK for mainline?
Diego.
cp/ChangeLog.pph
* parser.c (cp_lexer_dump_tokens): Add arguments START_TOKEN and
CURR_TOKEN.
Update all callers.
Print tokens starting with START_TOKEN.
Highlight CURR_TOKEN by enclosing it in [[ ]].
(cp_debug_print_tree_if_set): New.
(cp_debug_print_context): New.
(cp_debug_print_context_stack): New.
(cp_debug_print_flag): New.
(cp_debug_print_unparsed_function): New.
(cp_debug_print_unparsed_queues): New.
(cp_debug_parser): New.
* cp/parser.h (cp_debug_parser): Declare.
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 042a838..0f13a44 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -248,24 +248,49 @@ static FILE *cp_lexer_debug_stream;
sizeof, typeof, or alignof. */
int cp_unevaluated_operand;
-/* FIX pph: #ifdef ENABLE_CHECKING */
-/* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
- tokens. */
+/* Dump up to NUM tokens in BUFFER to FILE starting with token
+ START_TOKEN. If START_TOKEN is NULL, the dump starts with the
+ first token in BUFFER. If NUM is 0, dump all the tokens. If
+ CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
+ highlighted by surrounding it in [[ ]]. */
void
-cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
+cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
+ cp_token *start_token, unsigned num,
+ cp_token *curr_token)
{
unsigned i;
cp_token *token;
+ bool do_print;
fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
if (num == 0)
num = VEC_length (cp_token, buffer);
+ if (start_token && start_token > VEC_address (cp_token, buffer))
+ {
+ cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
+ fprintf (file, " ... ");
+ }
+
+ do_print = false;
for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
{
+ if (token == start_token)
+ do_print = true;
+
+ if (!do_print)
+ continue;
+
+ if (token == curr_token)
+ fprintf (file, "[[");
+
cp_lexer_print_token (file, token);
+
+ if (token == curr_token)
+ fprintf (file, "]]");
+
switch (token->type)
{
case CPP_SEMICOLON:
@@ -296,9 +321,195 @@ cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
void
cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
{
- cp_lexer_dump_tokens (stderr, buffer, 0);
+ cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
+}
+
+
+/* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
+ description for T. */
+
+static void
+cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
+{
+ if (t)
+ {
+ fprintf (file, "%s: ", desc);
+ debug_generic_expr (t);
+ }
+}
+
+
+/* Dump parser context C to FILE. */
+
+static void
+cp_debug_print_context (FILE *file, cp_parser_context *c)
+{
+ const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
+ fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
+ print_generic_expr (file, c->object_type, 0);
+ fprintf (file, "}\n");
+}
+
+
+/* Print the stack of parsing contexts to FILE starting with FIRST. */
+static void
+cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
+{
+ unsigned i;
+ cp_parser_context *c;
+
+ fprintf (file, "Parsing context stack:\n");
+ for (i = 0, c = first; c; c = c->next)
+ {
+ fprintf (file, "\t#%u: ", i);
+ cp_debug_print_context (file, c);
+ }
+}
+
+
+/* Print the value of FLAG to FILE. DESC is a string describing the flag. */
+
+static void
+cp_debug_print_flag (FILE *file, const char *desc, bool flag)
+{
+ if (flag)
+ fprintf (file, "%s: %s\n", desc, flag ? "true" : "false");
+}
+
+
+/* Print an unparsed function entry UF to FILE. */
+
+static void
+cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
+{
+ unsigned i;
+ cp_default_arg_entry *default_arg_fn;
+ tree fn;
+
+ fprintf (file, "\tFunctions with default args:\n");
+ for (i = 0;
+ VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
+ default_arg_fn);
+ i++)
+ {
+ fprintf (file, "\t\tClass type: ");
+ print_generic_expr (file, default_arg_fn->class_type, 0);
+ fprintf (file, "\t\tDeclaration: ");
+ print_generic_expr (file, default_arg_fn->decl, 0);
+ fprintf (file, "\n");
+ }
+
+ fprintf (file, "\n\tFunctions with definitions that require "
+ "post-processing\n\t\t");
+ for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
+ {
+ print_generic_expr (file, fn, 0);
+ fprintf (file, " ");
+ }
+ fprintf (file, "\n");
+}
+
+
+/* Print the stack of unparsed member functions S to FILE. */
+
+static void
+cp_debug_print_unparsed_queues (FILE *file,
+ VEC(cp_unparsed_functions_entry, gc) *s)
+{
+ unsigned i;
+ cp_unparsed_functions_entry *uf;
+
+ fprintf (file, "Unparsed functions\n");
+ for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
+ {
+ fprintf (file, "#%u:\n", i);
+ cp_debug_print_unparsed_function (file, uf);
+ }
}
-/* FIX pph: #endif */
+
+
+/* Dump debugging information for the given PARSER. If FILE is NULL,
+ the output is printed on stderr. */
+
+void
+cp_debug_parser (FILE *file, cp_parser *parser)
+{
+ cp_token *start_token, *first_token, *next_token;
+ const size_t window_size = 20;
+
+ if (file == NULL)
+ file = stderr;
+
+ fprintf (file, "Parser state\n\n");
+ fprintf (file, "Number of tokens: %u\n",
+ VEC_length (cp_token, parser->lexer->buffer));
+ cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
+ cp_debug_print_tree_if_set (file, "Object scope",
+ parser->object_scope);
+ cp_debug_print_tree_if_set (file, "Qualifying scope",
+ parser->qualifying_scope);
+ cp_debug_print_context_stack (file, parser->context);
+ cp_debug_print_flag (file, "Allow GNU extensions",
+ parser->allow_gnu_extensions_p);
+ cp_debug_print_flag (file, "'>' token is greater-than",
+ parser->greater_than_is_operator_p);
+ cp_debug_print_flag (file, "Default args allowed in current "
+ "parameter list", parser->default_arg_ok_p);
+ cp_debug_print_flag (file, "Parsing integral constant-expression",
+ parser->integral_constant_expression_p);
+ cp_debug_print_flag (file, "Allow non-constant expression in current "
+ "constant-expression",
+ parser->allow_non_integral_constant_expression_p);
+ cp_debug_print_flag (file, "Seen non-constant expression",
+ parser->non_integral_constant_expression_p);
+ cp_debug_print_flag (file, "Local names and 'this' forbidden in "
+ "current context",
+ parser->local_variables_forbidden_p);
+ cp_debug_print_flag (file, "In unbraced linkage specification",
+ parser->in_unbraced_linkage_specification_p);
+ cp_debug_print_flag (file, "Parsing a declarator",
+ parser->in_declarator_p);
+ cp_debug_print_flag (file, "In template argument list",
+ parser->in_template_argument_list_p);
+ cp_debug_print_flag (file, "Parsing an iteration statement",
+ parser->in_statement & IN_ITERATION_STMT);
+ cp_debug_print_flag (file, "Parsing a switch statement",
+ parser->in_statement & IN_SWITCH_STMT);
+ cp_debug_print_flag (file, "Parsing a structured OpenMP block",
+ parser->in_statement & IN_OMP_BLOCK);
+ cp_debug_print_flag (file, "Parsing a an OpenMP loop",
+ parser->in_statement & IN_OMP_FOR);
+ cp_debug_print_flag (file, "Parsing an if statement",
+ parser->in_statement & IN_IF_STMT);
+ cp_debug_print_flag (file, "Parsing a type-id in an expression "
+ "context", parser->in_type_id_in_expr_p);
+ cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
+ parser->implicit_extern_c);
+ cp_debug_print_flag (file, "String expressions should be translated "
+ "to execution character set",
+ parser->translate_strings_p);
+ cp_debug_print_flag (file, "Parsing function body outside of a "
+ "local class", parser->in_function_body);
+ cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
+ parser->colon_corrects_to_scope_p);
+ if (parser->type_definition_forbidden_message)
+ fprintf (file, "Error message for forbidden type definitions: %s\n",
+ parser->type_definition_forbidden_message);
+ cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
+ fprintf (file, "Number of class definitions in progress: %u\n",
+ parser->num_classes_being_defined);
+ fprintf (file, "Number of template parameter lists for the current "
+ "declaration: %u\n", parser->num_template_parameter_lists);
+
+ next_token = parser->lexer->next_token;
+ first_token = VEC_address (cp_token, parser->lexer->buffer);
+ start_token = (next_token > first_token + window_size / 2)
+ ? next_token - window_size / 2
+ : first_token;
+ cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
+ next_token);
+}
+
/* Return true if LEXER has a CPP_EOF at the end of the buffer. */
diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
index ac08918..32a22e2 100644
--- a/gcc/cp/parser.h
+++ b/gcc/cp/parser.h
@@ -370,10 +370,12 @@ typedef struct GTY(()) cp_parser {
extern cp_token eof_token;
extern GTY(()) cp_parser *the_parser;
-extern void cp_lexer_dump_tokens (FILE *, VEC(cp_token,gc) *, unsigned);
+extern void cp_lexer_dump_tokens (FILE *, VEC(cp_token,gc) *, cp_token *,
+ unsigned, cp_token *);
extern void cp_lexer_get_tokens (cp_lexer *);
extern cp_token_position cp_lexer_token_position (cp_lexer *, bool);
extern void cp_lexer_debug_tokens (VEC(cp_token,gc) *);
extern cp_token_cache *cp_token_cache_new (cp_token *, cp_token *);
+extern void cp_debug_parser (FILE *, cp_parser *);
#endif /* GCC_CP_PARSER_H */
diff --git a/gcc/cp/pph.c b/gcc/cp/pph.c
index 14a62c8..add6dfc 100644
--- a/gcc/cp/pph.c
+++ b/gcc/cp/pph.c
@@ -525,7 +525,7 @@ static void
pth_dump_hunk (FILE *stream, cp_token_hunk *hunk)
{
pth_dump_identifiers (stream, &hunk->identifiers);
- cp_lexer_dump_tokens (stream, hunk->buffer, 0);
+ cp_lexer_dump_tokens (stream, hunk->buffer, NULL, 0, NULL);
}
/* Dump a debug log of the HUNK. */
@@ -749,7 +749,7 @@ pth_dump_state (FILE *f)
if (state->lexer)
{
fprintf (f, "Tokens in main lexer:\n");
- cp_lexer_dump_tokens (f, state->lexer->buffer, 0);
+ cp_lexer_dump_tokens (f, state->lexer->buffer, NULL, 0, NULL);
}
}
@@ -3133,7 +3133,7 @@ pph_print_token_range (VEC(tree,heap) *v, VEC(cp_token, heap) *vtok)
pph_debug_location (pph_logfile, VEC_last (cp_token, vtok)->location);
fprintf (pph_logfile, "\n");
fprintf (pph_logfile, "PPH: hunk tokens ");
- cp_lexer_dump_tokens (stderr, (VEC(cp_token, gc) *)vtok, 0);
+ cp_lexer_dump_tokens (stderr, (VEC(cp_token, gc) *)vtok, NULL, 0, NULL);
fprintf (pph_logfile, "PPH: hunk ASTs:\n");
for (i = 0; VEC_iterate (tree, v, i, t); i++)
{
--
This patch is available for review at http://codereview.appspot.com/4389045
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Add debugging functions for cp_parser (issue4389045)
@ 2011-04-13 0:02 crowl
2011-04-13 0:53 ` Diego Novillo
2011-04-13 9:25 ` Richard Guenther
0 siblings, 2 replies; 7+ messages in thread
From: crowl @ 2011-04-13 0:02 UTC (permalink / raw)
To: dnovillo; +Cc: jason, gcc-patches, reply
One oddity, otherwise LGTM.
http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c
File gcc/cp/parser.c (right):
http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c#newcode375
gcc/cp/parser.c:375: if (flag)
This code will never print "false", so why have it?
http://codereview.appspot.com/4389045/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Add debugging functions for cp_parser (issue4389045)
2011-04-13 0:02 crowl
@ 2011-04-13 0:53 ` Diego Novillo
2011-04-13 9:25 ` Richard Guenther
1 sibling, 0 replies; 7+ messages in thread
From: Diego Novillo @ 2011-04-13 0:53 UTC (permalink / raw)
To: dnovillo, crowl, jason, gcc-patches, reply
On Tue, Apr 12, 2011 at 20:02, <crowl@google.com> wrote:
> One oddity, otherwise LGTM.
>
>
> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c
> File gcc/cp/parser.c (right):
>
> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c#newcode375
> gcc/cp/parser.c:375: if (flag)
> This code will never print "false", so why have it?
Thanks. I originally had it printing true and false values. I then
changed my mind (too noisy, as most fields are false) but forgot to
change it. I'll fix.
Diego.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Add debugging functions for cp_parser (issue4389045)
2011-04-13 0:02 crowl
2011-04-13 0:53 ` Diego Novillo
@ 2011-04-13 9:25 ` Richard Guenther
2011-04-13 12:29 ` Diego Novillo
1 sibling, 1 reply; 7+ messages in thread
From: Richard Guenther @ 2011-04-13 9:25 UTC (permalink / raw)
To: dnovillo, crowl, jason, gcc-patches, reply
On Wed, Apr 13, 2011 at 2:02 AM, <crowl@google.com> wrote:
> One oddity, otherwise LGTM.
>
>
> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c
> File gcc/cp/parser.c (right):
>
> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c#newcode375
> gcc/cp/parser.c:375: if (flag)
> This code will never print "false", so why have it?
Can you quote the piece of the patch you are commenting on?
Thanks,
Richard.
> http://codereview.appspot.com/4389045/
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Add debugging functions for cp_parser (issue4389045)
2011-04-13 9:25 ` Richard Guenther
@ 2011-04-13 12:29 ` Diego Novillo
2011-04-13 12:39 ` Richard Guenther
0 siblings, 1 reply; 7+ messages in thread
From: Diego Novillo @ 2011-04-13 12:29 UTC (permalink / raw)
To: Richard Guenther; +Cc: crowl, jason, gcc-patches, reply
On Wed, Apr 13, 2011 at 05:25, Richard Guenther
<richard.guenther@gmail.com> wrote:
> On Wed, Apr 13, 2011 at 2:02 AM, <crowl@google.com> wrote:
>> One oddity, otherwise LGTM.
>>
>>
>> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c
>> File gcc/cp/parser.c (right):
>>
>> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c#newcode375
>> gcc/cp/parser.c:375: if (flag)
>> This code will never print "false", so why have it?
>
> Can you quote the piece of the patch you are commenting on?
I'm not sure if that can be controlled. When you respond to a hunk on
Rietveld, it creates the quote automatically. Maybe there is a way of
increasing the scope of the quote. I'll ask.
Diego.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Add debugging functions for cp_parser (issue4389045)
2011-04-13 12:29 ` Diego Novillo
@ 2011-04-13 12:39 ` Richard Guenther
2011-04-13 12:49 ` Diego Novillo
0 siblings, 1 reply; 7+ messages in thread
From: Richard Guenther @ 2011-04-13 12:39 UTC (permalink / raw)
To: Diego Novillo; +Cc: crowl, jason, gcc-patches, reply
On Wed, Apr 13, 2011 at 2:29 PM, Diego Novillo <dnovillo@google.com> wrote:
> On Wed, Apr 13, 2011 at 05:25, Richard Guenther
> <richard.guenther@gmail.com> wrote:
>> On Wed, Apr 13, 2011 at 2:02 AM, <crowl@google.com> wrote:
>>> One oddity, otherwise LGTM.
>>>
>>>
>>> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c
>>> File gcc/cp/parser.c (right):
>>>
>>> http://codereview.appspot.com/4389045/diff/1/gcc/cp/parser.c#newcode375
>>> gcc/cp/parser.c:375: if (flag)
>>> This code will never print "false", so why have it?
>>
>> Can you quote the piece of the patch you are commenting on?
>
> I'm not sure if that can be controlled. When you respond to a hunk on
> Rietveld, it creates the quote automatically. Maybe there is a way of
> increasing the scope of the quote. I'll ask.
Quoting the patch hunk that is associated with the commend would be enough.
Richard.
> Diego.
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Add debugging functions for cp_parser (issue4389045)
2011-04-13 12:39 ` Richard Guenther
@ 2011-04-13 12:49 ` Diego Novillo
0 siblings, 0 replies; 7+ messages in thread
From: Diego Novillo @ 2011-04-13 12:49 UTC (permalink / raw)
To: Richard Guenther; +Cc: crowl, jason, gcc-patches, reply
On Wed, Apr 13, 2011 at 08:39, Richard Guenther
<richard.guenther@gmail.com> wrote:
> On Wed, Apr 13, 2011 at 2:29 PM, Diego Novillo <dnovillo@google.com> wrote:
>>
>> I'm not sure if that can be controlled. When you respond to a hunk on
>> Rietveld, it creates the quote automatically. Maybe there is a way of
>> increasing the scope of the quote. I'll ask.
>
> Quoting the patch hunk that is associated with the commend would be enough.
Agreed, but it's easier if double-click DTRT.
Diego.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2011-04-13 12:49 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-11 16:24 Add debugging functions for cp_parser (issue4389045) Diego Novillo
2011-04-13 0:02 crowl
2011-04-13 0:53 ` Diego Novillo
2011-04-13 9:25 ` Richard Guenther
2011-04-13 12:29 ` Diego Novillo
2011-04-13 12:39 ` Richard Guenther
2011-04-13 12:49 ` Diego Novillo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).