Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / producer.c
blobef1dd93afbcc73159764d3d7e342a7ea249c28da
1 /* Producer string parsers for GDB.
3 Copyright (C) 2012-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "producer.h"
22 #include "gdbsupport/selftest.h"
23 #include "gdbsupport/gdb_regex.h"
25 /* See producer.h. */
27 int
28 producer_is_gcc_ge_4 (const char *producer)
30 int major, minor;
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;
41 /* See producer.h. */
43 int
44 producer_is_gcc (const char *producer, int *major, int *minor)
46 const char *cs;
48 if (producer != NULL && startswith (producer, "GNU "))
50 int maj, min;
52 if (major == NULL)
53 major = &maj;
54 if (minor == NULL)
55 minor = &min;
57 /* Skip any identifier after "GNU " - such as "C11" "C++" or "Java".
58 A full producer string might look like:
59 "GNU C 4.7.2"
60 "GNU Fortran 4.8.2 20140120 (Red Hat 4.8.2-16) -mtune=generic ..."
61 "GNU C++14 5.0.0 20150123 (experimental)"
63 cs = &producer[strlen ("GNU ")];
64 while (*cs && !isspace (*cs))
65 cs++;
66 if (*cs && isspace (*cs))
67 cs++;
68 if (sscanf (cs, "%d.%d", major, minor) == 2)
69 return 1;
72 /* Not recognized as GCC. */
73 return 0;
76 /* See producer.h. */
78 bool
79 producer_is_icc_ge_19 (const char *producer)
81 int major, minor;
83 if (! producer_is_icc (producer, &major, &minor))
84 return false;
86 return major >= 19;
89 /* See producer.h. */
91 bool
92 producer_is_icc (const char *producer, int *major, int *minor)
94 compiled_regex i_re ("Intel(R)", 0, "producer_is_icc");
95 if (producer == nullptr || i_re.exec (producer, 0, nullptr, 0) != 0)
96 return false;
98 /* Prepare the used fields. */
99 int maj, min;
100 if (major == nullptr)
101 major = &maj;
102 if (minor == nullptr)
103 minor = &min;
105 *minor = 0;
106 *major = 0;
108 compiled_regex re ("[0-9]+\\.[0-9]+", REG_EXTENDED, "producer_is_icc");
109 regmatch_t version[1];
110 if (re.exec (producer, ARRAY_SIZE (version), version, 0) == 0
111 && version[0].rm_so != -1)
113 const char *version_str = producer + version[0].rm_so;
114 sscanf (version_str, "%d.%d", major, minor);
115 return true;
118 return false;
121 /* See producer.h. */
123 bool
124 producer_is_llvm (const char *producer)
126 return ((producer != NULL) && (startswith (producer, "clang ")
127 || startswith (producer, " F90 Flang ")));
130 #if defined GDB_SELF_TEST
131 namespace selftests {
132 namespace producer {
134 static void
135 producer_parsing_tests ()
138 /* Check that we don't crash if "Version" is not found in what
139 looks like an ICC producer string. */
140 static const char icc_no_version[] = "Intel(R) foo bar";
142 int major = 0, minor = 0;
143 SELF_CHECK (!producer_is_icc (icc_no_version, &major, &minor));
144 SELF_CHECK (!producer_is_gcc (icc_no_version, &major, &minor));
148 static const char extern_f_14_0[] = "\
149 Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on \
150 Intel(R) 64, \
151 Version 14.0.1.074 Build 20130716";
153 int major = 0, minor = 0;
154 SELF_CHECK (producer_is_icc (extern_f_14_0, &major, &minor)
155 && major == 14 && minor == 0);
156 SELF_CHECK (!producer_is_gcc (extern_f_14_0, &major, &minor));
160 static const char intern_f_14[] = "\
161 Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on \
162 Intel(R) 64, \
163 Version 14.0";
165 int major = 0, minor = 0;
166 SELF_CHECK (producer_is_icc (intern_f_14, &major, &minor)
167 && major == 14 && minor == 0);
168 SELF_CHECK (!producer_is_gcc (intern_f_14, &major, &minor));
172 static const char intern_c_14[] = "\
173 Intel(R) C++ Intel(R) 64 Compiler XE for applications running on \
174 Intel(R) 64, \
175 Version 14.0";
176 int major = 0, minor = 0;
177 SELF_CHECK (producer_is_icc (intern_c_14, &major, &minor)
178 && major == 14 && minor == 0);
179 SELF_CHECK (!producer_is_gcc (intern_c_14, &major, &minor));
183 static const char intern_c_18[] = "\
184 Intel(R) C++ Intel(R) 64 Compiler for applications running on \
185 Intel(R) 64, \
186 Version 18.0 Beta";
187 int major = 0, minor = 0;
188 SELF_CHECK (producer_is_icc (intern_c_18, &major, &minor)
189 && major == 18 && minor == 0);
193 static const char gnu[] = "GNU C 4.7.2";
194 SELF_CHECK (!producer_is_icc (gnu, NULL, NULL));
196 int major = 0, minor = 0;
197 SELF_CHECK (producer_is_gcc (gnu, &major, &minor)
198 && major == 4 && minor == 7);
202 static const char gnu_exp[] = "GNU C++14 5.0.0 20150123 (experimental)";
203 int major = 0, minor = 0;
204 SELF_CHECK (!producer_is_icc (gnu_exp, NULL, NULL));
205 SELF_CHECK (producer_is_gcc (gnu_exp, &major, &minor)
206 && major == 5 && minor == 0);
210 static const char clang_llvm_exp[] = "clang version 12.0.0 (CLANG: bld#8)";
211 int major = 0, minor = 0;
212 SELF_CHECK (!producer_is_icc (clang_llvm_exp, NULL, NULL));
213 SELF_CHECK (!producer_is_gcc (clang_llvm_exp, &major, &minor));
214 SELF_CHECK (producer_is_llvm (clang_llvm_exp));
218 static const char flang_llvm_exp[] = " F90 Flang - 1.5 2017-05-01";
219 int major = 0, minor = 0;
220 SELF_CHECK (!producer_is_icc (flang_llvm_exp, NULL, NULL));
221 SELF_CHECK (!producer_is_gcc (flang_llvm_exp, &major, &minor));
222 SELF_CHECK (producer_is_llvm (flang_llvm_exp));
227 #endif
229 void _initialize_producer ();
230 void
231 _initialize_producer ()
233 #if defined GDB_SELF_TEST
234 selftests::register_test
235 ("producer-parser", selftests::producer::producer_parsing_tests);
236 #endif