aboutsummaryrefslogtreecommitdiff
path: root/gcc/diagnostic-state.h
AgeCommit message (Collapse)AuthorFilesLines
2025-07-11diagnostics: add support for directed graphs; use them for state graphsDavid Malcolm1-37/+0
In r16-1631-g2334d30cd8feac I added support for capturing state information from -fanalyzer in XML form, and adding a way to visualize these states in HTML output. The data was optionally captured in SARIF output (with "xml-state=yes"), stashing the XML in string form in a property bag. This worked, but there was no way to round-trip the stored data back from SARIF without adding an XML parser to GCC, which I don't want to do. SARIF supports capturing directed graphs, so this patch: (a) adds a new namespace diagnostics::digraphs, with classes digraph, node, and edge, representing directed graphs in a form similar to what SARIF can serialize (b) adds support to GCC's diagnostic subsystem for reporting graphs, either "globally" or as part of a diagnostic. An example in a testsuite plugin emits an error that has a couple of dummy graphs associated with it, and captures the optimization passes as a digraph "globally". Graphs are ignored by text sinks, but are captured by sarif sinks, and the "experimental-html" sink gains SVG-based rendering of any graphs using dot. This HTML output is rather crude; an example can be seen here: https://dmalcolm.fedorapeople.org/gcc/2025-07-10/diagnostic-test-graphs-html.c.html (c) adds support to libgdiagnostics for the above (d) adds support to sarif-replay for the above (round-tripping any graph information) (e) replaces the XML representation of state with a representation based on the above directed graphs, using property bags to stash additional information (e.g. "this is an on-stack buffer") (f) implements round-tripping of this information in sarif-replay To summarize: - previously we could generate HTML diagrams for debugging -fanalyzer directly from gcc, but not from stored .sarif output. - with this patch, we can generate such HTML diagrams both directly *and* from stored .sarif output (provided the SARIF sink was created with "state-graphs=yes") Examples of HTML output can be seen here: https://dmalcolm.fedorapeople.org/gcc/2025-07-10/ where as before j/k can be used to cycle through the events. which is almost identical to the output from the old XML-based implementation seen at: https://dmalcolm.fedorapeople.org/gcc/2025-06-23/ gcc/ChangeLog: * Makefile.in (OBJS-libcommon): Add diagnostic-digraphs.o and diagnostic-state-graphs.o. gcc/ChangeLog: * diagnostic-format-html.cc: Include "diagnostic-format-sarif.h", Replace include of "diagnostic-state.h" with includes of "diagnostic-digraphs.h" and "diagnostic-state-graphs.h". (html_generation_options::html_generation_options): Update for field renaming. (html_builder::m_body_element): New field. (html_builder::html_builder): Initialize m_body_element. (html_builder::maybe_make_state_diagram): Port from XML implementation to state graph implementation. (html_builder::make_element_for_diagnostic): Add any per-diagnostic graphs. (html_builder::add_graph): New. (html_builder::emit_global_graph): New. (html_output_format::report_global_digraph): New. * diagnostic-format-html.h (html_generation_options::m_show_state_diagram_xml): Replace with... (html_generation_options::m_show_state_diagrams_sarif): ...this. (html_generation_options::m_show_state_diagram_dot_src): Rename to... (html_generation_options::m_show_state_diagrams_dot_src): ...this. * diagnostic-format-sarif.cc: Include "diagnostic-digraphs.h" and "diagnostic-state-graphs.h". (sarif_builder::m_run_graphs): New field. (sarif_result::on_nested_diagnostic): Update call to make_location_object to pass arg by pointer. (sarif_builder::sarif_builder): Initialize m_run_graphs. (sarif_builder::report_global_digraph): New. (sarif_builder::make_result_object): Add any graphs to the result object. (sarif_builder::make_locations_arr): Update call to make_location_object to pass arg by pointer. (sarif_builder::make_location_object): Pass param "loc_mgr" by pointer rather than by reference so that it can be null, and handle this case. (copy_any_property_bag): New. (make_sarif_graph): New. (make_sarif_node): New. (make_sarif_edge): New. (sarif_property_bag::set_graph): New. (populate_thread_flow_location_object): Port from XML implementation to state graph implementation. (make_run_object): Store any graphs. (sarif_output_format::report_global_digraph): New. (sarif_generation_options::sarif_generation_options): Rename m_xml_state to m_state_graph. (selftest::test_make_location_object): Update for change to make_location_object. * diagnostic-format-sarif.h: (sarif_generation_options::m_xml_state): Replace with... (sarif_generation_options::m_state_graph): ...this. (class sarif_location_manager): Add forward decl. (diagnostics::digraphs::digraph): New forward decl. (diagnostics::digraphs::node): New forward decl. (diagnostics::digraphs::edge): New forward decl. (sarif_property_bag::set_graph): New decl. (class sarif_graph): New. (class sarif_node): New. (class sarif_edge): New. (make_sarif_graph): New decl. (make_sarif_node): New decl. (make_sarif_edge): New decl. * diagnostic-format-text.h (diagnostic_text_output_format::report_global_digraph): New. * diagnostic-format.h (diagnostic_output_format::report_global_digraph): New vfunc. * diagnostic-digraphs.cc: New file. * diagnostic-digraphs.h: New file. * diagnostic-metadata.h (diagnostics::digraphs::lazy_digraphs): New forward decl. (diagnostic_metadata::diagnostic_metadata): Initialize m_lazy_digraphs. (diagnostic_metadata::set_lazy_digraphs): New. (diagnostic_metadata::get_lazy_digraphs): New. (diagnostic_metadata::m_lazy_digraphs): New field. * diagnostic-output-spec.cc (sarif_scheme_handler::make_sink): Update for XML to state graph changes. (sarif_scheme_handler::make_sarif_gen_opts): Likewise. (html_scheme_handler::make_sink): Rename "show-state-diagram-xml" to "show-state-diagrams-sarif" and use pluralization consistently. * diagnostic-path.cc: Replace include of "xml.h" with "diagnostic-state-graphs.h". (diagnostic_event::maybe_make_xml_state): Replace with... (diagnostic_event::maybe_make_diagnostic_state_graph): ...this. * diagnostic-path.h (diagnostics::digraphs::digraph): New forward decl. (diagnostic_event::maybe_make_xml_state): Replace with... (diagnostic_event::maybe_make_diagnostic_state_graph): ...this. * diagnostic-state-graphs.cc: New file. * diagnostic-state-graphs.h: New file. * diagnostic-state-to-dot.cc: Port implementation from XML to state graphs. * diagnostic-state.h: Deleted file. * diagnostic.cc (diagnostic_context::report_global_digraph): New. * diagnostic.h (diagnostics::digraphs::lazy_digraph): New forward decl. (diagnostic_context::report_global_digraph): New decl. * doc/analyzer.texi (Debugging the Analyzer): Update to reflect change from XML to state graphs. * doc/invoke.texi ("sarif" diagnostics sink): Replace "xml-state" with "state-graphs". ("experimental-html" diagnostics sink): Replace "show-state-diagrams-xml" with "show-state-diagrams-sarif" * doc/libgdiagnostics/topics/compatibility.rst (LIBGDIAGNOSTICS_ABI_3): New. * doc/libgdiagnostics/topics/graphs.rst: New file. * doc/libgdiagnostics/topics/index.rst: Add graphs.rst. * graphviz.h (node_id::operator=): New. * json.h (json::value::dyn_cast_string): New. (json::object::get_num_keys): New accessor. (json::object::get_key): New accessor. (json::string::dyn_cast_string): New. * libgdiagnostics++.h (class libgdiagnostics::graph): New. (class libgdiagnostics::node): New. (class libgdiagnostics::edge): New. (class libgdiagnostics::diagnostic::take_graph): New. (class libgdiagnostics::manager::take_global_graph): New. (class libgdiagnostics::graph::set_description): New. (class libgdiagnostics::graph::get_node_by_id): New. (class libgdiagnostics::graph::get_edge_by_id): New. (class libgdiagnostics::graph::add_edge): New. (class libgdiagnostics::node::set_label): New. (class libgdiagnostics::node::set_location): New. (class libgdiagnostics::node::set_logical_location): New. * libgdiagnostics-private.h: New file. * libgdiagnostics.cc: Define INCLUDE_STRING. Include "diagnostic-digraphs.h", "diagnostic-state-graphs.h", and "libgdiagnostics-private.h". (struct diagnostic_graph): New. (struct diagnostic_node): New. (struct diagnostic_edge): New. (libgdiagnostics_path_event::libgdiagnostics_path_event): Add state_graph param. (libgdiagnostics_path_event::maybe_make_diagnostic_state_graph): New. (libgdiagnostics_path_event::m_state_graph): New field. (diagnostic_execution_path::add_event_va): Add state_graph param. (class prebuilt_digraphs): New. (diagnostic::diagnostic): Use m_graphs in m_metadata. (diagnostic::take_graph): New. (diagnostic::get_graphs): New accessor. (diagnostic::m_graphs): New field. (diagnostic_manager::take_global_graph): New. (diagnostic_execution_path_add_event): Update for new param to add_event_va. (diagnostic_execution_path_add_event_va): Likewise. (diagnostic_graph::add_node_with_id): New public entrypoint. (diagnostic_graph::add_edge_with_label): New public entrypoint. (diagnostic_manager_new_graph): New public entrypoint. (diagnostic_manager_take_global_graph): New public entrypoint. (diagnostic_take_graph): New public entrypoint. (diagnostic_graph_release): New public entrypoint. (diagnostic_graph_set_description): New public entrypoint. (diagnostic_graph_add_node): New public entrypoint. (diagnostic_graph_add_edge): New public entrypoint. (diagnostic_graph_get_node_by_id): New public entrypoint. (diagnostic_graph_get_edge_by_id): New public entrypoint. (diagnostic_node_set_location): New public entrypoint. (diagnostic_node_set_label): New public entrypoint. (diagnostic_node_set_logical_location): New public entrypoint. (private_diagnostic_execution_path_add_event_2): New private entrypoint. (private_diagnostic_graph_set_property_bag): New private entrypoint. (private_diagnostic_node_set_property_bag): New private entrypoint. (private_diagnostic_edge_set_property_bag): New private entrypoint. * libgdiagnostics.h (diagnostic_graph): New typedef. (diagnostic_node): New typedef. (diagnostic_edge): New typedef. (diagnostic_manager_new_graph): New decl. (diagnostic_manager_take_global_graph): New decl. (diagnostic_take_graph): New decl. (diagnostic_graph_release): New decl. (diagnostic_graph_set_description): New decl. (diagnostic_graph_add_node): New decl. (diagnostic_graph_add_edge): New decl. (diagnostic_graph_get_node_by_id): New decl. (diagnostic_graph_get_edge_by_id): New decl. (diagnostic_node_set_label): New decl. (diagnostic_node_set_location): New decl. (diagnostic_node_set_logical_location): New decl. * libgdiagnostics.map (LIBGDIAGNOSTICS_ABI_3): New. * libsarifreplay.cc: Include "libgdiagnostics-private.h". (id_map): New "using". (sarif_replayer::report_invalid_sarif): Update for change to report_problem params. (sarif_replayer::report_unhandled_sarif): Likewise. (sarif_replayer::report_note): New. (sarif_replayer::report_problem): Pass param "ref" by pointer rather than reference and handle it being null. (sarif_replayer::maybe_get_property_bag): New. (sarif_replayer::maybe_get_property_bag_value): New. (sarif_replayer::handle_run_obj): Handle run-level "graphs" as per §3.14.20. (sarif_replayer::handle_result_obj): Handle result-level "graphs" as per §3.27.19. (handle_thread_flow_location_object): Optionally handle graphs stored in property "gcc/diagnostic_event/state_graph" as state graphs. (sarif_replayer::handle_graph_object): New. (sarif_replayer::handle_node_object): New. (sarif_replayer::handle_edge_object): New. (sarif_replayer::get_graph_node_by_id_property): New. * selftest-run-tests.cc (selftest::run_tests): Call selftest::diagnostic_graph_cc_tests and selftest::diagnostic_state_graph_cc_tests. * selftest.h (selftest::diagnostic_graph_cc_tests): New decl. (selftest::diagnostic_state_graph_cc_tests): New decl. gcc/analyzer/ChangeLog: * ana-state-to-diagnostic-state.cc: Reimplement, replacing XML-based implementation with one based on state graphs. * ana-state-to-diagnostic-state.h: Likewise. * checker-event.cc: Replace include of "xml.h" with include of "diagnostic-state-graphs.h". (checker_event::maybe_make_xml_state): Replace with... (checker_event::maybe_make_diagnostic_state_graph): ...this. * checker-event.h: Add include of "diagnostic-digraphs.h". (checker_event::maybe_make_xml_state): Replace decl with... (checker_event::maybe_make_diagnostic_state_graph): ...this. * engine.cc (exploded_node::on_stmt_pre): Replace "_analyzer_dump_xml" with "__analyzer_dump_sarif". * program-state.cc: Replace include of "diagnostic-state.h" with "diagnostic-state-graphs.h". (program_state::dump_dot): Port from XML to state graphs. * program-state.h: Drop reduntant forward decl of xml::document. (program_state::make_xml): Replace decl with... (program_state::make_diagnostic_state_graph): ...this. (program_state::dump_xml_to_pp): Drop decl. (program_state::dump_xml_to_file): Drop decl. (program_state::dump_xml): Drop decl. (program_state::dump_dump_sarif): New decl. * sm-malloc.cc (get_dynalloc_state_for_state): New. (malloc_state_machine::add_state_to_xml): Replace with... (malloc_state_machine::add_state_to_state_graph): ...this. * sm.cc (state_machine::add_state_to_xml): Replace with... (state_machine::add_state_to_state_graph): ...this. (state_machine::add_global_state_to_xml): Replace with... (state_machine::add_global_state_to_state_graph): ...this. * sm.h (class xml_state): Drop forward decl. (class analyzer_state_graph): New forward decl. (state_machine::add_state_to_xml): Replace decl with... (state_machine::add_state_to_state_graph): ...this. (state_machine::add_global_state_to_xml): Replace decl with... (state_machine::add_global_state_to_state_graph): ...this. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/state-diagram-1-sarif.py (test_xml_state): Rename to... (test_state_graph): ...this. Port from XML to SARIF graphs. * gcc.dg/analyzer/state-diagram-1.c: Update sink option from "sarif:xml-state=yes" to "sarif:state-graphs=yes". * gcc.dg/analyzer/state-diagram-5-sarif.c: Likewise. * gcc.dg/analyzer/state-diagram-5-sarif.py: Drop import of ET. (test_nested_types_in_xml_state): Rename to... (test_nested_types_in_state_graph): ...this. Port from XML to SARIF graphs. * gcc.dg/plugin/diagnostic-test-graphs-html.c: New test. * gcc.dg/plugin/diagnostic-test-graphs-html.py: New test script. * gcc.dg/plugin/diagnostic-test-graphs-sarif.c: New test. * gcc.dg/plugin/diagnostic-test-graphs-sarif.py: New test script. * gcc.dg/plugin/diagnostic-test-graphs.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_graphs.cc: New test plugin. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the above. * lib/sarif.py (get_xml_state): Delete. (get_state_graph): New. (def get_state_node_attr): New. (get_state_node_kind): New. (get_state_node_name): New. (get_state_node_type): New. (get_state_node_value): New. * sarif-replay.dg/2.1.0-invalid/3.40.2-duplicate-node-id.sarif: New test. * sarif-replay.dg/2.1.0-invalid/3.41.4-unrecognized-node-id.sarif: New test. * sarif-replay.dg/2.1.0-valid/graphs-check-html.py: New test script. * sarif-replay.dg/2.1.0-valid/graphs-check-sarif-roundtrip.py: New test script. * sarif-replay.dg/2.1.0-valid/graphs.sarif: New test. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2025-06-23diagnostics: add state diagrams to analyzer experimental-html output [PR116792]David Malcolm1-0/+37
This patch adds various support for debugging diagnostic paths and events, intended initially for myself to help with debugging -fanalyzer. It adds the optional ability for a diagnostic_event to supply a description of the predicted state of the program at that point along the diagnostic_path. To isolate the diagnostic subsystem from the analyzer, this representation is currently an xml::document with custom elements. The XML representation is similar to the analyzer's internal state but can be easier to read - for example, rather than storing the contents of memory via byte offsets, it uses fields for structs and element indexes for arrays, recursively. These states are handled by the HTML and SARIF diagnostic sinks. The SARIF sink simply embeds the XML as a string in a property bag of the threadFlowLocation object (SARIF v2.1.0 section 3.38). For HTML output, the "experimental-html" sink gains a new "show-state-diagrams=yes" option i.e.: -fdiagnostics-add-output=experimental-html:show-state-diagrams=yes which converts the state XML into SVG diagrams visualizing the state of memory at each event, inspired by the "ddd" debugger. These can be seen by pressing 'j' and 'k' to single-step forward and backward through events, making it *much* easier to debug -fanalyzer. An example of output can be seen here: https://dmalcolm.fedorapeople.org/gcc/2025-06-23/state-diagram-1.c.html showing an issue in a singly-linked list; there are various other examples in the parent directory. Generating the SVG diagrams requires an invocation of "dot" per event, so it noticeable slows down diagnostic emission, hence the opt-in command-line flag. However, I'm already finding bugs in -fanalyzer with this that I hadn't seen before. Given that the UI is rather clunky and there is lots of room for improvement to the visualizations, for now this feature is marked as being for GCC developers, not end-users. The patch also adds a dot::ast_node class hierarachy to make it easy to create GraphViz dot files with the correct escaping, and adds a C++ wrapper around pex adding some syntactic sugar for invoking subprocesses. gcc/ChangeLog: PR other/116792 * Makefile.in (ANALYZER_OBJS): Add analyzer/ana-state-to-diagnostic-state.o. (OBJS): Move graphviz.o to... (OBJS-libcommon): ...here. Add diagnostic-state-to-dot.o and pex.o. * diagnostic-format-html.cc: Include "diagnostic-state.h" and "graphviz.h". (html_generation_options::html_generation_options): Initialize the new flags. (HTML_SCRIPT): Add function "get_any_state_diagram". Use it when changing current focus id to update the visibility of the pertinent diagram, if any. (print_pre_source): New. (html_builder::maybe_make_state_diagram): New. (html_path_label_writer::html_path_label_writer): Add "path" param. Initialize m_path and m_curr_event_id. (html_path_label_writer::begin_label): Store current event id. (html_path_label_writer::end_label): Attempt to make a state diagram and add it if successful. (html_path_label_writer::get_element_id): New. (html_path_label_writer::m_path): New field. (html_path_label_writer::m_curr_event_id): New field. (html_builder::make_element_for_diagnostic): Pass path to label writer. * diagnostic-format-html.h (html_generation_options::m_show_state_diagrams): New field. (html_generation_options::m_show_state_diagram_xml): New field. (html_generation_options::m_show_state_diagram_dot_src): New field. * diagnostic-format-sarif.cc: Include "xml.h". (populate_thread_flow_location_object): If requested, attempt to generate xml state and add it to the proeprty bag as "gcc/diagnostic_event/xml_state" in xml source form. (sarif_generation_options::sarif_generation_options): Initialize m_xml_state. * diagnostic-format-sarif.h (sarif_generation_options::m_xml_state): New field. * diagnostic-path.cc: Define INCLUDE_MAP. Include "xml.h". (diagnostic_event::maybe_make_xml_state): New. * diagnostic-path.h (class xml::document): New forward decl. (diagnostic_event::maybe_make_xml_state): New vfunc decl. * diagnostic-state-to-dot.cc: New file. * diagnostic-state.h: New file. * digraph.cc: Define INCLUDE_STRING and INCLUDE_VECTOR. * doc/analyzer.texi: Document state diagrams in html output. (__analyzer_dump_dot): New. (__analyzer_dump_xml): New. * doc/invoke.texi (sarif): Add "xml-state" key. (experimental-html): Add keys "show-state-diagrams", "show-state-diagrams-dot-src" and "show-state-diagrams-xml". * graphviz.cc: Define INCLUDE_MAP, INCLUDE_STRING, and INCLUDE_VECTOR. Include "xml.h", "xml-printer.h", "pex.h" and "selftest.h". (graphviz_out::graphviz_out): Extract... (dot::writer::writer): ...this. (graphviz_out::write_indent): Convert to... (dot::writer::write_indent): ...this. (graphviz_out::print): Use get_pp. (graphviz_out::println): Likewise. (graphviz_out::begin_tr): Likewise. (graphviz_out::end_tr): Likewise. (graphviz_out::begin_td): Likewise. (graphviz_out::end_td): Likewise. (graphviz_out::begin_trtd): Likewise. (graphviz_out::end_tdtr): Likewise. (dot::ast_node::dump): New. (dot::id::id): New. (dot::id::print): New. (dot::id::is_identifier_p): New. (dot::kv_pair::print): New. (dot::attr_list::print): New. (dot::stmt_list::print): New. (dot::stmt_list::add_edge): New. (dot::stmt_list::add_attr): New. (dot::graph::print): New. (dot::stmt_with_attr_list::set_label): New. (dot::node_stmt::print): New. (dot::attr_stmt::print): New. (dot::kv_stmt::print): New. (dot::node_id::print): New. (dot::port::print): New. (dot::edge_stmt::print): New. (dot::subgraph::print): New. (dot::make_svg_document_buffer_from_graph): New. (dot::make_svg_from_graph): New. (selftest:test_ids): New. (selftest:test_trivial_graph): New. (selftest:test_layout_example): New. (selftest:graphviz_cc_tests): New. * graphviz.h (xml::node): New forward decl. (class graphviz_out): Split out into... (class dot::writer): ...this new class (struct dot::ast_node): New. (struct dot::id): New. (struct dot::kv_pair): New. (struct dot::attr_list): New. (struct dot::stmt_list): New. (struct dot::graph): New. (struct dot::stmt): New. (struct dot::stmt_with_attr_list): New. (struct dot::node_stmt): New. (struct dot::attr_stmt): New. (struct dot::kv_stmt): New. (enum class dot::compass_pt): New. (struct dot::port): New. (struct dot::node_id): New. (struct dot::edge_stmt): New. (struct dot::subgraph): New. (dot::make_svg_from_graph): New. * opts-diagnostic.cc (sarif_scheme_handler::make_sink): Add "xml-state" flag. (html_scheme_handler::make_sink): Add flags "show-state-diagrams", "show-state-diagram-dot-src", and "show-state-diagram-xml". * pex.cc: New file. * pex.h: New file. * selftest-run-tests.cc (selftest::run_tests): Call graphviz_cc_tests. * selftest.h (selftest::graphviz_cc_tests): New decl. * xml.cc (xml::node_with_children::add_comment): New. (xml::node_with_children::find_child_element): New. (xml::element::get_attr): New. (xml::comment::write_as_xml): New. (selftest::test_printer): Add coverage of find_child_element and get_attr. (selftest::test_comment): New. (selftest::xml_cc_tests): Call test_comment. * xml.h: New forward decls. (xml::node::dyn_cast_text): Use nullptr. (xml::node::dyn_cast_element): New vfunc. (xml::node_with_children::add_comment): New decl. (xml::node_with_children::find_child_element): New decl. (xml::element::dyn_cast_element): New vfunc impl. (xml::element::get_attr): New decl. (struct xml::comment): New xml::node subclass. gcc/analyzer/ChangeLog: PR other/116792 * ana-state-to-diagnostic-state.cc: New file. * ana-state-to-diagnostic-state.h: New file. * checker-event.cc: Include "xml.h". (checker_event::checker_event): Initialize m_path. (checker_event::prepare_for_emission): Store the path pointer into m_path. (checker_event::maybe_make_xml_state): New. (function_entry_event::function_entry_event): Add "state" param and use it to initialize m_state. (superedge_event::get_program_state): New. (call_event::get_program_state): New. (warning_event::get_program_state): New. * checker-event.h (checker_event::get_program_state): New vfunc. (checker_event::maybe_make_xml_state): New decl. (checker_event::m_path): New field. (statement_event::get_program_state): New vfunc impl. (function_entry_event::function_entry_event): Add "state" param. (function_entry_event::get_program_state): New vfunc impl. (function_entry_event::m_state): New field. (state_change_event::get_program_state): New vfunc impl. (superedge_event::get_program_state): New vfunc decl. (warning_event::warning_event): Add "program_state_" param and copy it. (warning_event::get_program_state): New vfunc decl. (warning_event::m_program_state): New field. * checker-path.h (checker_path::checker_path): Add ext_state param. (checker_path::get_ext_state): New accessor. (checker_path::m_ext_state): New field. * common.h: Define INCLUDE_MAP and INCLUDE_STRING. * diagnostic-manager.cc (saved_diagnostic::operator==): Don't deduplicate dump_path_diagnostic instances. (diagnostic_manager::emit_saved_diagnostic): Pass ext_state to checker_path ctor. * engine.cc: (impl_region_model_context::on_state_leak): Pass old and new state to state_machine::on_leak. (exploded_node::on_stmt_pre): Implement __analyzer_dump_xml and __analyzer_dump_dot. * exploded-graph.h (impl_region_model_context::get_state): New. * infinite-recursion.cc (recursive_function_entry_event::recursive_function_entry_event): Add "dst_state" param and pass to function_entry_event ctor. (infinite_recursion_diagnostic::add_function_entry_event): Pass state to event ctor. * kf-analyzer.cc: Include "analyzer/program-state.h" (dump_path_diagnostic::dump_path_diagnostic): Add "state" param. (dump_path_diagnostic::get_final_state): New. (dump_path_diagnostic::m_state): New field. (kf_analyzer_dump_path::impl_call_pre): Pass state to warning. * pending-diagnostic.cc (pending_diagnostic::add_function_entry_event): Pass state to function_entry_event. (pending_diagnostic::add_final_event): Likewise to warning_event. * pending-diagnostic.h (pending_diagnostic::get_final_state): New vfunc decl. * program-state.cc: Include "diagnostic-state.h", "graphviz.h" and "analyzer/ana-state-to-diagnostic-state.h". (program_state::dump_dot): New. * program-state.h: Include "text-art/tree-widget.h" and "analyzer/store.h". (class xml::document): New forward decl. (make_xml): New. (dump_xml_to_pp): New. (dump_xml_to_file): New. (dump_xml): New. (dump_dot): New. * record-layout.cc (record_layout::record_layout): Make param const_tree. * record-layout.h (item::item): Likewise. (item::m_field): Likewise. (record_layout::record_layout): Likewise. (record_layout::begin): New. (record_layout::end): New. * region-model.cc (exposure_through_uninit_copy::complain_about_fully_uninit_item): Use const_tree. (exposure_through_uninit_copy::complain_about_partially_uninit_item): Likewise. * region-model.h (region_model_context::get_state): New vfunc. (noop_region_model_context::get_state): New. (region_model_context_decorator::get_state): New. * sm-fd.cc (fd_leak::fd_leak): Add "final_state" param and capture it if present. (fd_leak::get_final_state): New. (fd_leak::m_final_state): New. (fd_state_machine::on_open): Pass nullptr for new "final_state" param. (fd_state_machine::on_creat): Likewise. (fd_state_machine::on_socket): Likewise. (fd_state_machine::on_accept): Likewise. (fd_state_machine::on_leak): Add state params and pass new state as final state to fd_leak ctor. * sm-file.cc: Include "analyzer/program-state.h". (file_leak::file_leak): Add "final_state" param and capture it if present. (file_leak::get_final_state): New. (file_leak::m_final_state): New. (fileptr_state_machine::on_leak): Add state params and pass new state as final state to fd_leak ctor. * sm-malloc.cc: Include "analyzer/ana-state-to-diagnostic-state.h". (malloc_leak::malloc_leak): Add "final_state" param and use it. (malloc_leak::get_final_state): New vfunc impl. (malloc_leak::m_final_state): New field. (malloc_state_machine::on_leak): Add state params; capture final state. (malloc_state_machine::add_state_to_xml): New. * sm.cc (state_machine::on_leak): Add "old_state" and "new_state" params. Use nullptr. (state_machine::add_state_to_xml): New. (state_machine::add_global_state_to_xml): New. * sm.h (class xml_state): New forward decl. (state_machine::on_leak): Add state params. (state_machine::add_state_to_xml): New vfunc decl. (state_machine::add_global_state_to_xml): New vfunc decl. * store.h (bit_range::operator<): New. * varargs.cc (va_list_leak::va_list_leak): Add final_state param and capture it if non-null. (va_list_leak::get_final_state): New. (va_list_leak::m_final_state): New. (va_list_state_machine::on_leak): Add state params and pass final state to va_list_leak ctor. gcc/testsuite/ChangeLog: PR other/116792 * g++.dg/analyzer/state-diagram.C: New test. * gcc.dg/analyzer/analyzer-decls.h (__analyzer_dump_dot): New decl. (__analyzer_dump_xml): New decl. * gcc.dg/analyzer/state-diagram-1-sarif.py: New test script. * gcc.dg/analyzer/state-diagram-1.c: New test. * gcc.dg/analyzer/state-diagram-2.c: New test. * gcc.dg/analyzer/state-diagram-3.c: New test. * gcc.dg/analyzer/state-diagram-4.c: New test. * gcc.dg/analyzer/state-diagram-5-html.py: New test script. * gcc.dg/analyzer/state-diagram-5-sarif.py: New test script. * gcc.dg/analyzer/state-diagram-5.c: New test. * gcc.dg/plugin/analyzer_cpython_plugin.cc: Define INCLUDE_STRING. * gcc.dg/plugin/analyzer_gil_plugin.cc: Likewise. * gcc.dg/plugin/analyzer_kernel_plugin.cc: Likewise. * gcc.dg/plugin/analyzer_known_fns_plugin.cc: Likewise. * lib/htmltest.py (ns): Add SVG namespace. * lib/sarif.py (get_result_by_index): New. (get_xml_state): New. Signed-off-by: David Malcolm <dmalcolm@redhat.com>