Line data Source code
1 : /* Analyzer-specific extensions to diagnostic-handling.
2 : Copyright (C) 2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "analyzer/common.h"
22 :
23 : #include "diagnostics/digraphs.h"
24 : #include "diagnostics/dumping.h"
25 : #include "context.h"
26 : #include "channels.h"
27 : #include "tree-diagnostic-sink-extensions.h"
28 :
29 : #include "analyzer/exploded-graph.h"
30 :
31 : namespace { namespace analyzer_events = gcc::topics::analyzer_events; }
32 :
33 : #if ENABLE_ANALYZER
34 :
35 : namespace ana {
36 :
37 2 : class lazy_supergraph_diagnostic_digraph
38 : : public lazily_created<diagnostics::digraphs::digraph>
39 : {
40 : public:
41 1 : lazy_supergraph_diagnostic_digraph (const supergraph &sg)
42 1 : : m_sg (sg)
43 : {
44 : }
45 :
46 : std::unique_ptr<diagnostics::digraphs::digraph>
47 1 : create_object () const final override
48 : {
49 1 : auto g = m_sg.make_diagnostics_digraph ();
50 1 : g->set_description ("\"supergraph\" used by '-fanalyzer'"
51 : " internally to represent the user's code");
52 1 : return g;
53 : }
54 :
55 : private:
56 : const supergraph &m_sg;
57 : };
58 :
59 2 : class lazy_exploded_graph_diagnostic_digraph
60 : : public lazily_created<diagnostics::digraphs::digraph>
61 : {
62 : public:
63 1 : lazy_exploded_graph_diagnostic_digraph (const exploded_graph &eg)
64 1 : : m_eg (eg)
65 : {
66 : }
67 :
68 : std::unique_ptr<diagnostics::digraphs::digraph>
69 1 : create_object () const final override
70 : {
71 1 : auto g = m_eg.make_diagnostics_digraph ();
72 1 : g->set_description ("\"exploded graph\" used by '-fanalyzer'"
73 : " internally to represent interprocedural control"
74 : " flow in the user's code along with possible states"
75 : " at each program point");
76 1 : return g;
77 : }
78 :
79 : private:
80 : const exploded_graph &m_eg;
81 : };
82 :
83 : static void
84 1 : report_analyzer_info (diagnostics::sink &sink,
85 : const analyzer_events::on_ana_done &m)
86 : {
87 1 : sink.report_global_digraph
88 1 : (lazy_supergraph_diagnostic_digraph (m.m_eg.get_supergraph ()));
89 :
90 1 : sink.report_global_digraph
91 1 : (lazy_exploded_graph_diagnostic_digraph (m.m_eg));
92 :
93 : // TODO: capture other information???
94 1 : }
95 :
96 : } // namespace ana
97 :
98 : #endif /* #if ENABLE_ANALYZER */
99 :
100 : /* A diagnostics::sink::extension which subscribes to the analyzer
101 : being done and responds to it by adding internal details about the
102 : analysis to the diagnostic sink. */
103 :
104 : class analyzer_sink_extension : public diagnostics::sink::extension
105 : {
106 : public:
107 1 : analyzer_sink_extension (diagnostics::sink &sink)
108 : : extension (sink),
109 1 : m_event_subscriber (sink)
110 : {
111 1 : auto &channel = g->get_channels ().analyzer_events_channel;
112 1 : channel.add_subscriber (m_event_subscriber);
113 1 : }
114 :
115 : void
116 0 : dump (FILE *out, int indent) const
117 : {
118 0 : diagnostics::dumping::emit_heading (out, indent, "analyzer_sink_extension");
119 0 : }
120 :
121 : private:
122 : class event_subscriber : public analyzer_events::subscriber
123 : {
124 : public:
125 1 : event_subscriber (diagnostics::sink &sink) : m_sink (sink) {}
126 :
127 : void
128 1 : on_message (const analyzer_events::on_ana_done &m) final override
129 : {
130 : #if ENABLE_ANALYZER
131 1 : report_analyzer_info (m_sink, m);
132 : #endif
133 1 : }
134 :
135 : private:
136 : diagnostics::sink &m_sink;
137 : } m_event_subscriber;
138 : };
139 :
140 : std::unique_ptr<diagnostics::sink::extension>
141 1 : compiler_extension_factory::make_analyzer_graphs_extension (diagnostics::sink &sink) const
142 : {
143 1 : return std::make_unique<analyzer_sink_extension> (sink);
144 : }
|