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