Branch data 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-2025 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 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "version.h"
26 : : #include "tree.h"
27 : : #include "diagnostic.h"
28 : : #include "tree-logical-location.h"
29 : : #include "diagnostics/client-data-hooks.h"
30 : : #include "diagnostics/sarif-sink.h"
31 : : #include "langhooks.h"
32 : : #include "plugin.h"
33 : : #include "timevar.h"
34 : :
35 : : /* Concrete class for supplying a diagnostics::context with information
36 : : about a specific plugin within the client, when the client is the
37 : : compiler (i.e. a GCC plugin). */
38 : :
39 : : class compiler_diagnostic_client_plugin_info
40 : : : public diagnostics::client_plugin_info
41 : : {
42 : : public:
43 : 11 : compiler_diagnostic_client_plugin_info (const plugin_name_args *args)
44 : 11 : : m_args (args)
45 : : {
46 : : }
47 : :
48 : 11 : const char *get_short_name () const final override
49 : : {
50 : 11 : return m_args->base_name;
51 : : }
52 : :
53 : 11 : const char *get_full_name () const final override
54 : : {
55 : 11 : return m_args->full_name;
56 : : }
57 : :
58 : 11 : const char *get_version () const final override
59 : : {
60 : 11 : return m_args->version;
61 : : }
62 : :
63 : : private:
64 : : const plugin_name_args *m_args;
65 : : };
66 : :
67 : : /* Concrete subclass of client_version_info for use by compilers proper,
68 : : (i.e. using lang_hooks, and with knowledge of GCC plugins). */
69 : :
70 : 339415 : class compiler_version_info : public diagnostics::client_version_info
71 : : {
72 : : public:
73 : 202 : const char *get_tool_name () const final override
74 : : {
75 : 202 : return lang_hooks.name;
76 : : }
77 : :
78 : : /* Compare with toplev.cc: print_version.
79 : : TARGET_NAME is passed in by the Makefile. */
80 : : char *
81 : 101 : maybe_make_full_name () const final override
82 : : {
83 : 101 : return xasprintf ("%s %sversion %s (%s)",
84 : : get_tool_name (), pkgversion_string, version_string,
85 : 101 : TARGET_NAME);
86 : : }
87 : :
88 : 101 : const char *get_version_string () const final override
89 : : {
90 : 101 : return version_string;
91 : : }
92 : :
93 : 101 : char *maybe_make_version_url () const final override
94 : : {
95 : 101 : return xasprintf ("https://gcc.gnu.org/gcc-%i/", GCC_major_version);
96 : : }
97 : :
98 : 101 : void for_each_plugin (plugin_visitor &visitor) const final override
99 : : {
100 : 101 : ::for_each_plugin (on_plugin_cb, &visitor);
101 : 101 : }
102 : :
103 : : private:
104 : : static void
105 : 11 : on_plugin_cb (const plugin_name_args *args,
106 : : void *user_data)
107 : : {
108 : 11 : compiler_diagnostic_client_plugin_info cpi (args);
109 : 11 : client_version_info::plugin_visitor *visitor
110 : : = (client_version_info::plugin_visitor *)user_data;
111 : 11 : visitor->on_plugin (cpi);
112 : 11 : }
113 : : };
114 : :
115 : : /* Subclass of diagnostics::client_data_hooks for use by compilers proper
116 : : i.e. with knowledge of "tree", access to langhooks, timevars etc. */
117 : :
118 : 339415 : class compiler_data_hooks : public diagnostics::client_data_hooks
119 : : {
120 : : public:
121 : : const diagnostics::client_version_info *
122 : 202 : get_any_version_info () const final override
123 : : {
124 : 202 : return &m_version_info;
125 : : }
126 : :
127 : : const diagnostics::logical_locations::manager *
128 : 4091 : get_logical_location_manager () const final override
129 : : {
130 : 4091 : return &m_logical_location_manager;
131 : : }
132 : :
133 : : diagnostics::logical_locations::key
134 : 380 : get_current_logical_location () const final override
135 : : {
136 : 380 : return m_logical_location_manager.key_from_tree (current_function_decl);
137 : : }
138 : :
139 : : const char *
140 : 119 : maybe_get_sarif_source_language (const char *filename) const final override
141 : : {
142 : 119 : return lang_hooks.get_sarif_source_language (filename);
143 : : }
144 : :
145 : : void
146 : 101 : add_sarif_invocation_properties (diagnostics::sarif_object &invocation_obj)
147 : : const final override
148 : : {
149 : 101 : if (g_timer)
150 : 8 : if (auto timereport_val = g_timer->make_json ())
151 : : {
152 : 8 : auto &bag_obj
153 : 8 : = invocation_obj.get_or_create_properties ();
154 : 8 : bag_obj.set ("gcc/timeReport", std::move (timereport_val));
155 : :
156 : : /* If the user requested SARIF output, then assume they want the
157 : : time report data in the SARIF output, and *not* later emitted on
158 : : stderr.
159 : : Implement this by cleaning up the global timer instance now. */
160 : 8 : delete g_timer;
161 : 8 : g_timer = nullptr;
162 : 8 : }
163 : 101 : }
164 : :
165 : : private:
166 : : compiler_version_info m_version_info;
167 : : tree_logical_location_manager m_logical_location_manager;
168 : : };
169 : :
170 : : /* Create a compiler_data_hooks (so that the class can be local
171 : : to this file). */
172 : :
173 : : std::unique_ptr<diagnostics::client_data_hooks>
174 : 339415 : make_compiler_data_hooks ()
175 : : {
176 : 339415 : return std::make_unique<compiler_data_hooks> ();
177 : : }
|