From 98cd4d123aa14598b1f0d54c22663c8200a96d9c Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 15 Jul 2021 15:04:07 -0400 Subject: [PATCH] analyzer: add -fdump-analyzer-exploded-paths gcc/analyzer/ChangeLog: * analyzer.opt (fdump-analyzer-exploded-paths): New. * diagnostic-manager.cc (diagnostic_manager::emit_saved_diagnostic): Implement it. * engine.cc (exploded_path::dump_to_pp): Add ext_state param and use it to dump states if non-NULL. (exploded_path::dump): Likewise. (exploded_path::dump_to_file): New. * exploded-graph.h (exploded_path::dump_to_pp): Add ext_state param. (exploded_path::dump): Likewise. (exploded_path::dump): Likewise. (exploded_path::dump_to_file): New. gcc/ChangeLog: * doc/invoke.texi (-fdump-analyzer-exploded-paths): New. Signed-off-by: David Malcolm --- gcc/analyzer/analyzer.opt | 4 ++++ gcc/analyzer/diagnostic-manager.cc | 11 ++++++++++ gcc/analyzer/engine.cc | 34 ++++++++++++++++++++++++------ gcc/analyzer/exploded-graph.h | 9 +++++--- gcc/doc/invoke.texi | 6 ++++++ 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/gcc/analyzer/analyzer.opt b/gcc/analyzer/analyzer.opt index dd34495abd5..7b77ae8a73d 100644 --- a/gcc/analyzer/analyzer.opt +++ b/gcc/analyzer/analyzer.opt @@ -210,6 +210,10 @@ fdump-analyzer-exploded-nodes-3 Common RejectNegative Var(flag_dump_analyzer_exploded_nodes_3) Dump a textual representation of the exploded graph to SRCFILE.eg-ID.txt. +fdump-analyzer-exploded-paths +Common RejectNegative Var(flag_dump_analyzer_exploded_paths) +Dump a textual representation of each diagnostic's exploded path to SRCFILE.IDX.KIND.epath.txt. + fdump-analyzer-feasibility Common RejectNegative Var(flag_dump_analyzer_feasibility) Dump various analyzer internals to SRCFILE.*.fg.dot and SRCFILE.*.tg.dot. diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc index b7d263b4217..d005facc20b 100644 --- a/gcc/analyzer/diagnostic-manager.cc +++ b/gcc/analyzer/diagnostic-manager.cc @@ -1164,6 +1164,17 @@ diagnostic_manager::emit_saved_diagnostic (const exploded_graph &eg, inform_n (loc, num_dupes, "%i duplicate", "%i duplicates", num_dupes); + if (flag_dump_analyzer_exploded_paths) + { + auto_timevar tv (TV_ANALYZER_DUMP); + pretty_printer pp; + pp_printf (&pp, "%s.%i.%s.epath.txt", + dump_base_name, sd.get_index (), sd.m_d->get_kind ()); + char *filename = xstrdup (pp_formatted_text (&pp)); + epath->dump_to_file (filename, eg.get_ext_state ()); + inform (loc, "exploded path written to %qs", filename); + free (filename); + } } delete pp; } diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc index 8f3e7f781b2..dc07a79e185 100644 --- a/gcc/analyzer/engine.cc +++ b/gcc/analyzer/engine.cc @@ -3630,10 +3630,12 @@ exploded_path::feasible_p (logger *logger, feasibility_problem **out, return true; } -/* Dump this path in multiline form to PP. */ +/* Dump this path in multiline form to PP. + If EXT_STATE is non-NULL, then show the nodes. */ void -exploded_path::dump_to_pp (pretty_printer *pp) const +exploded_path::dump_to_pp (pretty_printer *pp, + const extrinsic_state *ext_state) const { for (unsigned i = 0; i < m_edges.length (); i++) { @@ -3643,28 +3645,48 @@ exploded_path::dump_to_pp (pretty_printer *pp) const eedge->m_src->m_index, eedge->m_dest->m_index); pp_newline (pp); + + if (ext_state) + eedge->m_dest->dump_to_pp (pp, *ext_state); } } /* Dump this path in multiline form to FP. */ void -exploded_path::dump (FILE *fp) const +exploded_path::dump (FILE *fp, const extrinsic_state *ext_state) const { pretty_printer pp; pp_format_decoder (&pp) = default_tree_printer; pp_show_color (&pp) = pp_show_color (global_dc->printer); pp.buffer->stream = fp; - dump_to_pp (&pp); + dump_to_pp (&pp, ext_state); pp_flush (&pp); } /* Dump this path in multiline form to stderr. */ DEBUG_FUNCTION void -exploded_path::dump () const +exploded_path::dump (const extrinsic_state *ext_state) const { - dump (stderr); + dump (stderr, ext_state); +} + +/* Dump this path verbosely to FILENAME. */ + +void +exploded_path::dump_to_file (const char *filename, + const extrinsic_state &ext_state) const +{ + FILE *fp = fopen (filename, "w"); + if (!fp) + return; + pretty_printer pp; + pp_format_decoder (&pp) = default_tree_printer; + pp.buffer->stream = fp; + dump_to_pp (&pp, &ext_state); + pp_flush (&pp); + fclose (fp); } /* class feasibility_problem. */ diff --git a/gcc/analyzer/exploded-graph.h b/gcc/analyzer/exploded-graph.h index 2d25e5e5167..1d8b73da7c4 100644 --- a/gcc/analyzer/exploded-graph.h +++ b/gcc/analyzer/exploded-graph.h @@ -895,9 +895,12 @@ public: exploded_node *get_final_enode () const; - void dump_to_pp (pretty_printer *pp) const; - void dump (FILE *fp) const; - void dump () const; + void dump_to_pp (pretty_printer *pp, + const extrinsic_state *ext_state) const; + void dump (FILE *fp, const extrinsic_state *ext_state) const; + void dump (const extrinsic_state *ext_state = NULL) const; + void dump_to_file (const char *filename, + const extrinsic_state &ext_state) const; bool feasible_p (logger *logger, feasibility_problem **out, engine *eng, const exploded_graph *eg) const; diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index ea8812425e9..62e165f8d1e 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -428,6 +428,7 @@ Objective-C and Objective-C++ Dialects}. -fdump-analyzer-exploded-nodes @gol -fdump-analyzer-exploded-nodes-2 @gol -fdump-analyzer-exploded-nodes-3 @gol +-fdump-analyzer-exploded-paths @gol -fdump-analyzer-feasibility @gol -fdump-analyzer-json @gol -fdump-analyzer-state-purge @gol @@ -9651,6 +9652,11 @@ Dump a textual representation of the ``exploded graph'' to one dump file per node, to @file{@var{file}.eg-@var{id}.txt}. This is typically a large number of dump files. +@item -fdump-analyzer-exploded-paths +@opindex fdump-analyzer-exploded-paths +Dump a textual representation of the ``exploded path'' for each +diagnostic to @file{@var{file}.@var{idx}.@var{kind}.epath.txt}. + @item -fdump-analyzer-feasibility @opindex dump-analyzer-feasibility Dump internal details about the analyzer's search for feasible paths.