]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcc1/libcp1.cc
libcc1: use unique_ptr more
[thirdparty/gcc.git] / libcc1 / libcp1.cc
1 /* The library used by gdb.
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <cc1plugin-config.h>
21 #include <vector>
22 #include <string>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <sys/wait.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include <sstream>
32 #include "marshall-cp.hh"
33 #include "rpc.hh"
34 #include "connection.hh"
35 #include "names.hh"
36 #include "callbacks.hh"
37 #include "libiberty.h"
38 #include "xregex.h"
39 #include "findcomp.hh"
40 #include "compiler-name.hh"
41 #include "intl.h"
42
43 struct libcp1;
44
45 class libcp1_connection;
46
47 // The C compiler context that we hand back to our caller.
48 struct libcp1 : public gcc_cp_context
49 {
50 libcp1 (const gcc_base_vtable *, const gcc_cp_fe_vtable *);
51
52 // A convenience function to print something.
53 void print (const char *str)
54 {
55 this->print_function (this->print_datum, str);
56 }
57
58 std::unique_ptr<libcp1_connection> connection;
59
60 gcc_cp_oracle_function *binding_oracle;
61 gcc_cp_symbol_address_function *address_oracle;
62 gcc_cp_enter_leave_user_expr_scope_function *enter_scope;
63 gcc_cp_enter_leave_user_expr_scope_function *leave_scope;
64 void *oracle_datum;
65
66 void (*print_function) (void *datum, const char *message);
67 void *print_datum;
68
69 std::vector<std::string> args;
70 std::string source_file;
71
72 /* Non-zero as an equivalent to gcc driver option "-v". */
73 bool verbose;
74
75 /* Compiler to set by set_triplet_regexp or set_driver_filename. */
76 class compiler
77 {
78 protected:
79 libcp1 *self_;
80 public:
81 compiler (libcp1 *self) : self_ (self)
82 {
83 }
84 virtual char *find (std::string &compiler) const;
85 virtual ~compiler ()
86 {
87 }
88 };
89
90 std::unique_ptr<compiler> compilerp;
91
92 /* Compiler to set by set_triplet_regexp. */
93 class compiler_triplet_regexp : public compiler
94 {
95 private:
96 std::string triplet_regexp_;
97 public:
98 char *find (std::string &compiler) const override;
99 compiler_triplet_regexp (libcp1 *self, std::string triplet_regexp)
100 : compiler (self), triplet_regexp_ (triplet_regexp)
101 {
102 }
103 virtual ~compiler_triplet_regexp ()
104 {
105 }
106 };
107
108 /* Compiler to set by set_driver_filename. */
109 class compiler_driver_filename : public compiler
110 {
111 private:
112 std::string driver_filename_;
113 public:
114 char *find (std::string &compiler) const override;
115 compiler_driver_filename (libcp1 *self, std::string driver_filename)
116 : compiler (self), driver_filename_ (driver_filename)
117 {
118 }
119 virtual ~compiler_driver_filename ()
120 {
121 }
122 };
123 };
124
125 // A local subclass of connection that holds a back-pointer to the
126 // gcc_c_context object that we provide to our caller.
127 class libcp1_connection : public cc1_plugin::connection
128 {
129 public:
130
131 libcp1_connection (int fd, int aux_fd, libcp1 *b)
132 : connection (fd, aux_fd),
133 back_ptr (b)
134 {
135 }
136
137 void print (const char *buf) override
138 {
139 back_ptr->print (buf);
140 }
141
142 libcp1 *back_ptr;
143 };
144
145 libcp1::libcp1 (const gcc_base_vtable *v,
146 const gcc_cp_fe_vtable *cv)
147 : binding_oracle (NULL),
148 address_oracle (NULL),
149 oracle_datum (NULL),
150 print_function (NULL),
151 print_datum (NULL),
152 args (),
153 source_file (),
154 verbose (false),
155 compilerp (new libcp1::compiler (this))
156 {
157 base.ops = v;
158 cp_ops = cv;
159 }
160
161 \f
162
163 // Enclose these functions in an anonymous namespace because they
164 // shouldn't be exported, but they can't be static because they're
165 // used as template arguments.
166 namespace {
167 // This is a wrapper function that is called by the RPC system and
168 // that then forwards the call to the library user. Note that the
169 // return value is not used; the type cannot be 'void' due to
170 // limitations in our simple RPC.
171 int
172 cp_call_binding_oracle (cc1_plugin::connection *conn,
173 enum gcc_cp_oracle_request request,
174 const char *identifier)
175 {
176 libcp1 *self = ((libcp1_connection *) conn)->back_ptr;
177
178 self->binding_oracle (self->oracle_datum, self, request, identifier);
179 return 1;
180 }
181
182 // This is a wrapper function that is called by the RPC system and
183 // that then forwards the call to the library user.
184 gcc_address
185 cp_call_symbol_address (cc1_plugin::connection *conn, const char *identifier)
186 {
187 libcp1 *self = ((libcp1_connection *) conn)->back_ptr;
188
189 return self->address_oracle (self->oracle_datum, self, identifier);
190 }
191
192 int
193 cp_call_enter_scope (cc1_plugin::connection *conn)
194 {
195 libcp1 *self = ((libcp1_connection *) conn)->back_ptr;
196
197 self->enter_scope (self->oracle_datum, self);
198 return 1;
199 }
200
201 int
202 cp_call_leave_scope (cc1_plugin::connection *conn)
203 {
204 libcp1 *self = ((libcp1_connection *) conn)->back_ptr;
205
206 self->leave_scope (self->oracle_datum, self);
207 return 1;
208 }
209 } /* anonymous namespace */
210
211 \f
212
213 static void
214 set_callbacks (struct gcc_cp_context *s,
215 gcc_cp_oracle_function *binding_oracle,
216 gcc_cp_symbol_address_function *address_oracle,
217 gcc_cp_enter_leave_user_expr_scope_function *enter_scope,
218 gcc_cp_enter_leave_user_expr_scope_function *leave_scope,
219 void *datum)
220 {
221 libcp1 *self = (libcp1 *) s;
222
223 self->binding_oracle = binding_oracle;
224 self->address_oracle = address_oracle;
225 self->enter_scope = enter_scope;
226 self->leave_scope = leave_scope;
227 self->oracle_datum = datum;
228 }
229
230 // Instances of this rpc<> template function are installed into the
231 // "cp_vtable". These functions are parameterized by type and method
232 // name and forward the call via the connection.
233
234 template<typename R, const char *&NAME, typename... Arg>
235 R rpc (struct gcc_cp_context *s, Arg... rest)
236 {
237 libcp1 *self = (libcp1 *) s;
238 R result;
239
240 if (!cc1_plugin::call (self->connection.get (), NAME, &result, rest...))
241 return 0;
242 return result;
243 }
244
245 static const struct gcc_cp_fe_vtable cp_vtable =
246 {
247 GCC_CP_FE_VERSION_0,
248 set_callbacks,
249
250 #define GCC_METHOD0(R, N) \
251 rpc<R, cc1_plugin::cp::N>,
252 #define GCC_METHOD1(R, N, A) \
253 rpc<R, cc1_plugin::cp::N, A>,
254 #define GCC_METHOD2(R, N, A, B) \
255 rpc<R, cc1_plugin::cp::N, A, B>,
256 #define GCC_METHOD3(R, N, A, B, C) \
257 rpc<R, cc1_plugin::cp::N, A, B, C>,
258 #define GCC_METHOD4(R, N, A, B, C, D) \
259 rpc<R, cc1_plugin::cp::N, A, B, C, D>,
260 #define GCC_METHOD5(R, N, A, B, C, D, E) \
261 rpc<R, cc1_plugin::cp::N, A, B, C, D, E>,
262 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
263 rpc<R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
264
265 #include "gcc-cp-fe.def"
266
267 #undef GCC_METHOD0
268 #undef GCC_METHOD1
269 #undef GCC_METHOD2
270 #undef GCC_METHOD3
271 #undef GCC_METHOD4
272 #undef GCC_METHOD5
273 #undef GCC_METHOD7
274 };
275
276 \f
277
278 // Construct an appropriate regexp to match the compiler name.
279 static std::string
280 make_regexp (const char *triplet_regexp, const char *compiler)
281 {
282 std::stringstream buf;
283
284 buf << "^" << triplet_regexp << "-";
285
286 // Quote the compiler name in case it has something funny in it.
287 for (const char *p = compiler; *p; ++p)
288 {
289 switch (*p)
290 {
291 case '.':
292 case '^':
293 case '$':
294 case '*':
295 case '+':
296 case '?':
297 case '(':
298 case ')':
299 case '[':
300 case '{':
301 case '\\':
302 case '|':
303 buf << '\\';
304 break;
305 }
306 buf << *p;
307 }
308 buf << "$";
309
310 return buf.str ();
311 }
312
313 static void
314 libcp1_set_verbose (struct gcc_base_context *s, int /* bool */ verbose)
315 {
316 libcp1 *self = (libcp1 *) s;
317
318 self->verbose = verbose != 0;
319 }
320
321 char *
322 libcp1::compiler::find (std::string &compiler ATTRIBUTE_UNUSED) const
323 {
324 return xstrdup (_("Compiler has not been specified"));
325 }
326
327 char *
328 libcp1::compiler_triplet_regexp::find (std::string &compiler) const
329 {
330 std::string rx = make_regexp (triplet_regexp_.c_str (), CP_COMPILER_NAME);
331 if (self_->verbose)
332 fprintf (stderr, _("searching for compiler matching regex %s\n"),
333 rx.c_str());
334 regex_t triplet;
335 int code = regcomp (&triplet, rx.c_str (), REG_EXTENDED | REG_NOSUB);
336 if (code != 0)
337 {
338 size_t len = regerror (code, &triplet, NULL, 0);
339 char err[len];
340
341 regerror (code, &triplet, err, len);
342
343 return concat ("Could not compile regexp \"",
344 rx.c_str (),
345 "\": ",
346 err,
347 (char *) NULL);
348 }
349
350 if (!find_compiler (triplet, &compiler))
351 {
352 regfree (&triplet);
353 return concat ("Could not find a compiler matching \"",
354 rx.c_str (),
355 "\"",
356 (char *) NULL);
357 }
358 regfree (&triplet);
359 if (self_->verbose)
360 fprintf (stderr, _("found compiler %s\n"), compiler.c_str());
361 return NULL;
362 }
363
364 char *
365 libcp1::compiler_driver_filename::find (std::string &compiler) const
366 {
367 // Simulate fnotice by fprintf.
368 if (self_->verbose)
369 fprintf (stderr, _("using explicit compiler filename %s\n"),
370 driver_filename_.c_str());
371 compiler = driver_filename_;
372 return NULL;
373 }
374
375 static char *
376 libcp1_set_arguments (struct gcc_base_context *s,
377 int argc, char **argv)
378 {
379 libcp1 *self = (libcp1 *) s;
380
381 std::string compiler;
382 char *errmsg = self->compilerp->find (compiler);
383 if (errmsg != NULL)
384 return errmsg;
385
386 self->args.push_back (compiler);
387
388 for (int i = 0; i < argc; ++i)
389 self->args.push_back (argv[i]);
390
391 return NULL;
392 }
393
394 static char *
395 libcp1_set_triplet_regexp (struct gcc_base_context *s,
396 const char *triplet_regexp)
397 {
398 libcp1 *self = (libcp1 *) s;
399
400 self->compilerp.reset (new libcp1::compiler_triplet_regexp (self,
401 triplet_regexp));
402 return NULL;
403 }
404
405 static char *
406 libcp1_set_driver_filename (struct gcc_base_context *s,
407 const char *driver_filename)
408 {
409 libcp1 *self = (libcp1 *) s;
410
411 self->compilerp.reset (new libcp1::compiler_driver_filename (self,
412 driver_filename));
413 return NULL;
414 }
415
416 static char *
417 libcp1_set_arguments_v0 (struct gcc_base_context *s,
418 const char *triplet_regexp,
419 int argc, char **argv)
420 {
421 char *errmsg = libcp1_set_triplet_regexp (s, triplet_regexp);
422 if (errmsg != NULL)
423 return errmsg;
424
425 return libcp1_set_arguments (s, argc, argv);
426 }
427
428 static void
429 libcp1_set_source_file (struct gcc_base_context *s,
430 const char *file)
431 {
432 libcp1 *self = (libcp1 *) s;
433
434 self->source_file = file;
435 }
436
437 static void
438 libcp1_set_print_callback (struct gcc_base_context *s,
439 void (*print_function) (void *datum,
440 const char *message),
441 void *datum)
442 {
443 libcp1 *self = (libcp1 *) s;
444
445 self->print_function = print_function;
446 self->print_datum = datum;
447 }
448
449 static int
450 fork_exec (libcp1 *self, char **argv, int spair_fds[2], int stderr_fds[2])
451 {
452 pid_t child_pid = fork ();
453
454 if (child_pid == -1)
455 {
456 close (spair_fds[0]);
457 close (spair_fds[1]);
458 close (stderr_fds[0]);
459 close (stderr_fds[1]);
460 return 0;
461 }
462
463 if (child_pid == 0)
464 {
465 // Child.
466 dup2 (stderr_fds[1], 1);
467 dup2 (stderr_fds[1], 2);
468 close (stderr_fds[0]);
469 close (stderr_fds[1]);
470 close (spair_fds[0]);
471
472 execvp (argv[0], argv);
473 _exit (127);
474 }
475 else
476 {
477 // Parent.
478 close (spair_fds[1]);
479 close (stderr_fds[1]);
480
481 cc1_plugin::status result = cc1_plugin::FAIL;
482 if (self->connection->send ('H')
483 && ::cc1_plugin::marshall (self->connection.get (),
484 GCC_CP_FE_VERSION_0))
485 result = self->connection->wait_for_query ();
486
487 close (spair_fds[0]);
488 close (stderr_fds[0]);
489
490 while (true)
491 {
492 int status;
493
494 if (waitpid (child_pid, &status, 0) == -1)
495 {
496 if (errno != EINTR)
497 return 0;
498 }
499
500 if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
501 return 0;
502 break;
503 }
504
505 if (!result)
506 return 0;
507 return 1;
508 }
509 }
510
511 static int
512 libcp1_compile (struct gcc_base_context *s,
513 const char *filename)
514 {
515 libcp1 *self = (libcp1 *) s;
516
517 int fds[2];
518 if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) != 0)
519 {
520 self->print ("could not create socketpair\n");
521 return 0;
522 }
523
524 int stderr_fds[2];
525 if (pipe (stderr_fds) != 0)
526 {
527 self->print ("could not create pipe\n");
528 close (fds[0]);
529 close (fds[1]);
530 return 0;
531 }
532
533 self->args.push_back ("-fplugin=libcp1plugin");
534 char buf[100];
535 if (snprintf (buf, sizeof (buf), "-fplugin-arg-libcp1plugin-fd=%d", fds[1])
536 >= (long) sizeof (buf))
537 abort ();
538 self->args.push_back (buf);
539
540 self->args.push_back (self->source_file);
541 self->args.push_back ("-c");
542 self->args.push_back ("-o");
543 self->args.push_back (filename);
544 if (self->verbose)
545 self->args.push_back ("-v");
546
547 self->connection.reset (new libcp1_connection (fds[0], stderr_fds[0], self));
548
549 cc1_plugin::callback_ftype *fun
550 = cc1_plugin::callback<int,
551 enum gcc_cp_oracle_request,
552 const char *,
553 cp_call_binding_oracle>;
554 self->connection->add_callback ("binding_oracle", fun);
555
556 fun = cc1_plugin::callback<gcc_address,
557 const char *,
558 cp_call_symbol_address>;
559 self->connection->add_callback ("address_oracle", fun);
560
561 fun = cc1_plugin::callback<int,
562 cp_call_enter_scope>;
563 self->connection->add_callback ("enter_scope", fun);
564
565 fun = cc1_plugin::callback<int,
566 cp_call_leave_scope>;
567 self->connection->add_callback ("leave_scope", fun);
568
569 char **argv = new (std::nothrow) char *[self->args.size () + 1];
570 if (argv == NULL)
571 return 0;
572
573 for (unsigned int i = 0; i < self->args.size (); ++i)
574 argv[i] = const_cast<char *> (self->args[i].c_str ());
575 argv[self->args.size ()] = NULL;
576
577 return fork_exec (self, argv, fds, stderr_fds);
578 }
579
580 static int
581 libcp1_compile_v0 (struct gcc_base_context *s, const char *filename,
582 int verbose)
583 {
584 libcp1_set_verbose (s, verbose);
585 return libcp1_compile (s, filename);
586 }
587
588 static void
589 libcp1_destroy (struct gcc_base_context *s)
590 {
591 libcp1 *self = (libcp1 *) s;
592
593 delete self;
594 }
595
596 static const struct gcc_base_vtable vtable =
597 {
598 GCC_FE_VERSION_0,
599 libcp1_set_arguments_v0,
600 libcp1_set_source_file,
601 libcp1_set_print_callback,
602 libcp1_compile_v0,
603 libcp1_destroy,
604 libcp1_set_verbose,
605 libcp1_compile,
606 libcp1_set_arguments,
607 libcp1_set_triplet_regexp,
608 libcp1_set_driver_filename,
609 };
610
611 extern "C" gcc_cp_fe_context_function gcc_cp_fe_context;
612
613 #ifdef __GNUC__
614 #pragma GCC visibility push(default)
615 #endif
616
617 extern "C"
618 struct gcc_cp_context *
619 gcc_cp_fe_context (enum gcc_base_api_version base_version,
620 enum gcc_cp_api_version cp_version)
621 {
622 if ((base_version != GCC_FE_VERSION_0 && base_version != GCC_FE_VERSION_1)
623 || cp_version != GCC_CP_FE_VERSION_0)
624 return NULL;
625
626 return new libcp1 (&vtable, &cp_vtable);
627 }