Line data Source code
1 : /* Implementation of diagnostics::client_data_hooks for the compilers
2 : (e.g. with knowledge of "tree", lang_hooks, and timevars).
3 : Copyright (C) 2022-2026 Free Software Foundation, Inc.
4 : Contributed by David Malcolm <dmalcolm@redhat.com>.
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify it under
9 : the terms of the GNU General Public License as published by the Free
10 : Software Foundation; either version 3, or (at your option) any later
11 : version.
12 :
13 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #define INCLUDE_VECTOR
23 : #include "config.h"
24 : #include "system.h"
25 : #include "coretypes.h"
26 : #include "version.h"
27 : #include "tree.h"
28 : #include "diagnostic.h"
29 : #include "tree-logical-location.h"
30 : #include "diagnostics/client-data-hooks.h"
31 : #include "diagnostics/sarif-sink.h"
32 : #include "langhooks.h"
33 : #include "plugin.h"
34 : #include "timevar.h"
35 :
36 : /* Concrete class for supplying a diagnostics::context with information
37 : about a specific plugin within the client, when the client is the
38 : compiler (i.e. a GCC plugin). */
39 :
40 : class compiler_diagnostic_client_plugin_info
41 : : public diagnostics::client_plugin_info
42 : {
43 : public:
44 11 : compiler_diagnostic_client_plugin_info (const plugin_name_args *args)
45 11 : : m_args (args)
46 : {
47 : }
48 :
49 11 : const char *get_short_name () const final override
50 : {
51 11 : return m_args->base_name;
52 : }
53 :
54 11 : const char *get_full_name () const final override
55 : {
56 11 : return m_args->full_name;
57 : }
58 :
59 11 : const char *get_version () const final override
60 : {
61 11 : return m_args->version;
62 : }
63 :
64 : private:
65 : const plugin_name_args *m_args;
66 : };
67 :
68 : /* Concrete subclass of client_version_info for use by compilers proper,
69 : (i.e. using lang_hooks, and with knowledge of GCC plugins). */
70 :
71 340015 : class compiler_version_info : public diagnostics::client_version_info
72 : {
73 : public:
74 228 : const char *get_tool_name () const final override
75 : {
76 228 : return lang_hooks.name;
77 : }
78 :
79 : /* Compare with toplev.cc: print_version.
80 : TARGET_NAME is passed in by the Makefile. */
81 : char *
82 114 : maybe_make_full_name () const final override
83 : {
84 114 : return xasprintf ("%s %sversion %s (%s)",
85 : get_tool_name (), pkgversion_string, version_string,
86 114 : TARGET_NAME);
87 : }
88 :
89 114 : const char *get_version_string () const final override
90 : {
91 114 : return version_string;
92 : }
93 :
94 114 : char *maybe_make_version_url () const final override
95 : {
96 114 : return xasprintf ("https://gcc.gnu.org/gcc-%i/", GCC_major_version);
97 : }
98 :
99 114 : void for_each_plugin (plugin_visitor &visitor) const final override
100 : {
101 114 : ::for_each_plugin (on_plugin_cb, &visitor);
102 114 : }
103 :
104 : private:
105 : static void
106 11 : on_plugin_cb (const plugin_name_args *args,
107 : void *user_data)
108 : {
109 11 : compiler_diagnostic_client_plugin_info cpi (args);
110 11 : client_version_info::plugin_visitor *visitor
111 : = (client_version_info::plugin_visitor *)user_data;
112 11 : visitor->on_plugin (cpi);
113 11 : }
114 : };
115 :
116 : /* Subclass of diagnostics::client_data_hooks for use by compilers proper
117 : i.e. with knowledge of "tree", access to langhooks, timevars etc. */
118 :
119 340015 : class compiler_data_hooks : public diagnostics::client_data_hooks
120 : {
121 : public:
122 : const diagnostics::client_version_info *
123 228 : get_any_version_info () const final override
124 : {
125 228 : return &m_version_info;
126 : }
127 :
128 : const diagnostics::logical_locations::manager *
129 5244 : get_logical_location_manager () const final override
130 : {
131 5244 : return &m_logical_location_manager;
132 : }
133 :
134 : diagnostics::logical_locations::key
135 395 : get_current_logical_location () const final override
136 : {
137 395 : return m_logical_location_manager.key_from_tree (current_function_decl);
138 : }
139 :
140 : const char *
141 132 : maybe_get_sarif_source_language (const char *filename) const final override
142 : {
143 132 : return lang_hooks.get_sarif_source_language (filename);
144 : }
145 :
146 : void
147 114 : add_sarif_invocation_properties (diagnostics::sarif_object &invocation_obj)
148 : const final override
149 : {
150 114 : if (g_timer)
151 8 : if (auto timereport_val = g_timer->make_json ())
152 : {
153 8 : auto &bag_obj
154 8 : = invocation_obj.get_or_create_properties ();
155 8 : bag_obj.set ("gcc/timeReport", std::move (timereport_val));
156 :
157 : /* If the user requested SARIF output, then assume they want the
158 : time report data in the SARIF output, and *not* later emitted on
159 : stderr.
160 : Implement this by cleaning up the global timer instance now. */
161 8 : delete g_timer;
162 8 : g_timer = nullptr;
163 8 : }
164 114 : }
165 :
166 : private:
167 : compiler_version_info m_version_info;
168 : tree_logical_location_manager m_logical_location_manager;
169 : };
170 :
171 : /* Create a compiler_data_hooks (so that the class can be local
172 : to this file). */
173 :
174 : std::unique_ptr<diagnostics::client_data_hooks>
175 340015 : make_compiler_data_hooks ()
176 : {
177 340015 : return std::make_unique<compiler_data_hooks> ();
178 : }
|