Line data Source code
1 : /* Additional metadata about a client for a diagnostic context.
2 : Copyright (C) 2022-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 : #ifndef GCC_DIAGNOSTICS_CLIENT_DATA_HOOKS_H
22 : #define GCC_DIAGNOSTICS_CLIENT_DATA_HOOKS_H
23 :
24 : #include "diagnostics/logical-locations.h"
25 :
26 : namespace diagnostics {
27 :
28 : class sarif_object;
29 : class client_version_info;
30 :
31 : /* A bundle of additional metadata, owned by the diagnostics::context,
32 : for querying things about the client, like version data. */
33 :
34 340015 : class client_data_hooks
35 : {
36 : public:
37 : virtual ~client_data_hooks () {}
38 :
39 : void dump (FILE *out, int indent) const;
40 0 : void DEBUG_FUNCTION dump () const { dump (stderr, 0); }
41 :
42 : /* Get version info for this client, or NULL. */
43 : virtual const client_version_info *get_any_version_info () const = 0;
44 :
45 : /* Get the current logical_locations::manager for this client, or null. */
46 : virtual const logical_locations::manager *
47 : get_logical_location_manager () const = 0;
48 :
49 : /* Get the current logical location, or null.
50 : If this returns a non-null logical location, then
51 : get_logical_location_manager must return non-null. */
52 : virtual logical_locations::key
53 : get_current_logical_location () const = 0;
54 :
55 : /* Get a sourceLanguage value for FILENAME, or return NULL.
56 : See SARIF v2.1.0 Appendix J for suggested values. */
57 : virtual const char *
58 : maybe_get_sarif_source_language (const char *filename) const = 0;
59 :
60 : /* Hook to allow client to populate a SARIF "invocation" object with
61 : a custom property bag (see SARIF v2.1.0 section 3.8). */
62 : virtual void
63 : add_sarif_invocation_properties (sarif_object &invocation_obj) const = 0;
64 : };
65 :
66 : /* Implementation of client_data_hooks that delegates vfuncs to an
67 : optional inner object. */
68 :
69 : class client_data_hooks_decorator : public client_data_hooks
70 : {
71 : public:
72 : client_data_hooks_decorator (const client_data_hooks *inner)
73 : : m_inner (inner)
74 : {
75 : }
76 :
77 0 : const client_version_info *get_any_version_info () const override
78 : {
79 0 : if (m_inner)
80 0 : return m_inner->get_any_version_info ();
81 : return nullptr;
82 : }
83 :
84 : const logical_locations::manager *
85 0 : get_logical_location_manager () const override
86 : {
87 0 : if (m_inner)
88 0 : return m_inner->get_logical_location_manager ();
89 : return nullptr;
90 : }
91 :
92 : logical_locations::key
93 0 : get_current_logical_location () const override
94 : {
95 0 : if (m_inner)
96 0 : return m_inner->get_current_logical_location ();
97 0 : return logical_locations::key ();
98 : }
99 :
100 : const char *
101 0 : maybe_get_sarif_source_language (const char *filename) const override
102 : {
103 0 : if (m_inner)
104 0 : return m_inner->maybe_get_sarif_source_language (filename);
105 : return nullptr;
106 : }
107 :
108 : void
109 0 : add_sarif_invocation_properties (sarif_object &invocation_obj) const override
110 : {
111 0 : if (m_inner)
112 0 : m_inner->add_sarif_invocation_properties (invocation_obj);
113 0 : }
114 :
115 : private:
116 : const client_data_hooks *m_inner;
117 : };
118 :
119 : class client_plugin_info;
120 :
121 : /* Abstract base class for a diagnostics::context to get at
122 : version information about the client. */
123 :
124 340015 : class client_version_info
125 : {
126 : public:
127 114 : class plugin_visitor
128 : {
129 : public:
130 : virtual void on_plugin (const client_plugin_info &) = 0;
131 : };
132 :
133 : virtual ~client_version_info () {}
134 :
135 : /* Get a string suitable for use as the value of the "name" property
136 : (SARIF v2.1.0 section 3.19.8). */
137 : virtual const char *get_tool_name () const = 0;
138 :
139 : /* Create a string suitable for use as the value of the "fullName" property
140 : (SARIF v2.1.0 section 3.19.9). */
141 : virtual char *maybe_make_full_name () const = 0;
142 :
143 : /* Get a string suitable for use as the value of the "version" property
144 : (SARIF v2.1.0 section 3.19.13). */
145 : virtual const char *get_version_string () const = 0;
146 :
147 : /* Create a string suitable for use as the value of the "informationUri"
148 : property (SARIF v2.1.0 section 3.19.17). */
149 : virtual char *maybe_make_version_url () const = 0;
150 :
151 : virtual void for_each_plugin (plugin_visitor &v) const = 0;
152 : };
153 :
154 : /* Abstract base class for a diagnostics::context to get at
155 : information about a specific plugin within a client. */
156 :
157 11 : class client_plugin_info
158 : {
159 : public:
160 : /* For use e.g. by SARIF "name" property (SARIF v2.1.0 section 3.19.8). */
161 : virtual const char *get_short_name () const = 0;
162 :
163 : /* For use e.g. by SARIF "fullName" property
164 : (SARIF v2.1.0 section 3.19.9). */
165 : virtual const char *get_full_name () const = 0;
166 :
167 : /* For use e.g. by SARIF "version" property
168 : (SARIF v2.1.0 section 3.19.13). */
169 : virtual const char *get_version () const = 0;
170 : };
171 :
172 : } // namespace diagnostics
173 :
174 : /* Factory function for making an instance of client_data_hooks
175 : for use in the compiler (i.e. with knowledge of "tree", access to
176 : langhooks, etc). */
177 :
178 : extern std::unique_ptr<diagnostics::client_data_hooks>
179 : make_compiler_data_hooks ();
180 :
181 : #endif /* ! GCC_DIAGNOSTICS_CLIENT_DATA_HOOKS_H */
|