]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/producer.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / producer.c
CommitLineData
b32b108a
WT
1/* Producer string parsers for GDB.
2
1d506c26 3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
b32b108a
WT
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "producer.h"
268a13a5 22#include "gdbsupport/selftest.h"
d322d6d6 23#include "gdbsupport/gdb_regex.h"
b32b108a
WT
24
25/* See producer.h. */
26
27int
28producer_is_gcc_ge_4 (const char *producer)
29{
30 int major, minor;
31
32 if (! producer_is_gcc (producer, &major, &minor))
33 return -1;
34 if (major < 4)
35 return -1;
36 if (major > 4)
37 return INT_MAX;
38 return minor;
39}
40
41/* See producer.h. */
42
43int
44producer_is_gcc (const char *producer, int *major, int *minor)
45{
46 const char *cs;
47
48 if (producer != NULL && startswith (producer, "GNU "))
49 {
50 int maj, min;
51
52 if (major == NULL)
53 major = &maj;
54 if (minor == NULL)
55 minor = &min;
56
1d45d909
TV
57 /* Skip GNU. */
58 cs = &producer[strlen ("GNU ")];
59
60 /* Bail out for GNU AS. */
61 if (startswith (cs, "AS "))
62 return 0;
63
b32b108a
WT
64 /* Skip any identifier after "GNU " - such as "C11" "C++" or "Java".
65 A full producer string might look like:
66 "GNU C 4.7.2"
67 "GNU Fortran 4.8.2 20140120 (Red Hat 4.8.2-16) -mtune=generic ..."
68 "GNU C++14 5.0.0 20150123 (experimental)"
69 */
b32b108a 70 while (*cs && !isspace (*cs))
dda83cd7 71 cs++;
b32b108a 72 if (*cs && isspace (*cs))
dda83cd7 73 cs++;
b32b108a
WT
74 if (sscanf (cs, "%d.%d", major, minor) == 2)
75 return 1;
76 }
77
78 /* Not recognized as GCC. */
79 return 0;
80}
81
16e311ab
FW
82/* See producer.h. */
83
98ef1d81
TV
84bool
85producer_is_gas (const char *producer, int *major, int *minor)
86{
87 if (producer == nullptr)
88 {
89 /* No producer, don't know. */
90 return false;
91 }
92
93 /* Detect prefix. */
94 const char prefix[] = "GNU AS ";
95 if (!startswith (producer, prefix))
96 {
97 /* Producer is not gas. */
98 return false;
99 }
100
101 /* Skip prefix. */
102 const char *cs = &producer[strlen (prefix)];
103
104 /* Ensure that major/minor are not nullptrs. */
105 int maj, min;
106 if (major == nullptr)
107 major = &maj;
108 if (minor == nullptr)
109 minor = &min;
110
111 int scanned = sscanf (cs, "%d.%d", major, minor);
112 if (scanned != 2)
113 {
114 /* Unable to scan major/minor version. */
115 return false;
116 }
117
118 return true;
119}
120
121 /* See producer.h. */
122
16e311ab
FW
123bool
124producer_is_icc_ge_19 (const char *producer)
125{
126 int major, minor;
127
128 if (! producer_is_icc (producer, &major, &minor))
129 return false;
130
131 return major >= 19;
132}
5230b05a
WT
133
134/* See producer.h. */
135
136bool
137producer_is_icc (const char *producer, int *major, int *minor)
138{
f5dc2ee3
AB
139 compiled_regex i_re ("Intel(R)", 0, "producer_is_icc");
140 if (producer == nullptr || i_re.exec (producer, 0, nullptr, 0) != 0)
5230b05a
WT
141 return false;
142
143 /* Prepare the used fields. */
144 int maj, min;
fbb3bcfc 145 if (major == nullptr)
5230b05a 146 major = &maj;
fbb3bcfc 147 if (minor == nullptr)
5230b05a
WT
148 minor = &min;
149
150 *minor = 0;
151 *major = 0;
152
f5dc2ee3
AB
153 compiled_regex re ("[0-9]+\\.[0-9]+", REG_EXTENDED, "producer_is_icc");
154 regmatch_t version[1];
155 if (re.exec (producer, ARRAY_SIZE (version), version, 0) == 0
156 && version[0].rm_so != -1)
5230b05a 157 {
f5dc2ee3
AB
158 const char *version_str = producer + version[0].rm_so;
159 sscanf (version_str, "%d.%d", major, minor);
fbb3bcfc 160 return true;
5230b05a 161 }
fbb3bcfc 162
5230b05a
WT
163 return false;
164}
165
c2fd7fae
AKS
166/* See producer.h. */
167
168bool
169producer_is_llvm (const char *producer)
170{
171 return ((producer != NULL) && (startswith (producer, "clang ")
dda83cd7 172 || startswith (producer, " F90 Flang ")));
c2fd7fae
AKS
173}
174
e7e7469e
BL
175/* See producer.h. */
176
177bool
178producer_is_clang (const char *producer, int *major, int *minor)
179{
180 if (producer != nullptr && startswith (producer, "clang version "))
181 {
182 int maj, min;
183 if (major == nullptr)
184 major = &maj;
185 if (minor == nullptr)
186 minor = &min;
187
188 /* The full producer string will look something like
189 "clang version XX.X.X ..."
190 So we can safely ignore all characters before the first digit. */
191 const char *cs = producer + strlen ("clang version ");
192
193 if (sscanf (cs, "%d.%d", major, minor) == 2)
194 return true;
195 }
196 return false;
197}
198
5230b05a
WT
199#if defined GDB_SELF_TEST
200namespace selftests {
201namespace producer {
202
203static void
204producer_parsing_tests ()
205{
206 {
207 /* Check that we don't crash if "Version" is not found in what
208 looks like an ICC producer string. */
209 static const char icc_no_version[] = "Intel(R) foo bar";
210
211 int major = 0, minor = 0;
212 SELF_CHECK (!producer_is_icc (icc_no_version, &major, &minor));
213 SELF_CHECK (!producer_is_gcc (icc_no_version, &major, &minor));
214 }
215
216 {
fbb3bcfc 217 static const char extern_f_14_0[] = "\
5230b05a
WT
218Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on \
219Intel(R) 64, \
220Version 14.0.1.074 Build 20130716";
221
222 int major = 0, minor = 0;
fbb3bcfc
FW
223 SELF_CHECK (producer_is_icc (extern_f_14_0, &major, &minor)
224 && major == 14 && minor == 0);
225 SELF_CHECK (!producer_is_gcc (extern_f_14_0, &major, &minor));
5230b05a
WT
226 }
227
228 {
229 static const char intern_f_14[] = "\
230Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on \
231Intel(R) 64, \
232Version 14.0";
233
234 int major = 0, minor = 0;
235 SELF_CHECK (producer_is_icc (intern_f_14, &major, &minor)
236 && major == 14 && minor == 0);
237 SELF_CHECK (!producer_is_gcc (intern_f_14, &major, &minor));
238 }
239
240 {
241 static const char intern_c_14[] = "\
242Intel(R) C++ Intel(R) 64 Compiler XE for applications running on \
243Intel(R) 64, \
244Version 14.0";
245 int major = 0, minor = 0;
246 SELF_CHECK (producer_is_icc (intern_c_14, &major, &minor)
247 && major == 14 && minor == 0);
248 SELF_CHECK (!producer_is_gcc (intern_c_14, &major, &minor));
249 }
250
251 {
252 static const char intern_c_18[] = "\
253Intel(R) C++ Intel(R) 64 Compiler for applications running on \
254Intel(R) 64, \
255Version 18.0 Beta";
256 int major = 0, minor = 0;
257 SELF_CHECK (producer_is_icc (intern_c_18, &major, &minor)
258 && major == 18 && minor == 0);
259 }
260
261 {
262 static const char gnu[] = "GNU C 4.7.2";
263 SELF_CHECK (!producer_is_icc (gnu, NULL, NULL));
264
265 int major = 0, minor = 0;
266 SELF_CHECK (producer_is_gcc (gnu, &major, &minor)
267 && major == 4 && minor == 7);
268 }
269
270 {
271 static const char gnu_exp[] = "GNU C++14 5.0.0 20150123 (experimental)";
272 int major = 0, minor = 0;
273 SELF_CHECK (!producer_is_icc (gnu_exp, NULL, NULL));
274 SELF_CHECK (producer_is_gcc (gnu_exp, &major, &minor)
275 && major == 5 && minor == 0);
276 }
c2fd7fae
AKS
277
278 {
279 static const char clang_llvm_exp[] = "clang version 12.0.0 (CLANG: bld#8)";
280 int major = 0, minor = 0;
281 SELF_CHECK (!producer_is_icc (clang_llvm_exp, NULL, NULL));
282 SELF_CHECK (!producer_is_gcc (clang_llvm_exp, &major, &minor));
283 SELF_CHECK (producer_is_llvm (clang_llvm_exp));
284 }
285
286 {
287 static const char flang_llvm_exp[] = " F90 Flang - 1.5 2017-05-01";
288 int major = 0, minor = 0;
289 SELF_CHECK (!producer_is_icc (flang_llvm_exp, NULL, NULL));
290 SELF_CHECK (!producer_is_gcc (flang_llvm_exp, &major, &minor));
291 SELF_CHECK (producer_is_llvm (flang_llvm_exp));
292 }
98ef1d81
TV
293
294 {
295 static const char gas_exp[] = "GNU AS 2.39.0";
296 int major = 0, minor = 0;
297 SELF_CHECK (!producer_is_gcc (gas_exp, &major, &minor));
298 SELF_CHECK (producer_is_gas (gas_exp, &major, &minor));
299 SELF_CHECK (major == 2 && minor == 39);
300
301 static const char gas_incomplete_exp[] = "GNU AS ";
302 SELF_CHECK (!producer_is_gas (gas_incomplete_exp, &major, &minor));
303 SELF_CHECK (!producer_is_gcc (gas_incomplete_exp, &major, &minor));
304
305 static const char gas_incomplete_exp_2[] = "GNU AS 2";
306 SELF_CHECK (!producer_is_gas (gas_incomplete_exp_2, &major, &minor));
307 SELF_CHECK (!producer_is_gcc (gas_incomplete_exp_2, &major, &minor));
308 }
309
5230b05a
WT
310}
311}
312}
313#endif
314
6c265988 315void _initialize_producer ();
5230b05a
WT
316void
317_initialize_producer ()
318{
319#if defined GDB_SELF_TEST
320 selftests::register_test
321 ("producer-parser", selftests::producer::producer_parsing_tests);
322#endif
323}