libcc1: unify compiler handling

Both libcc1 plugins have nearly identical copies of code to find the
underlying compiler.  This seemed wasteful to me, so this patch
unifies the copies.

Two minor API changes were needed.

First, the old code used a back-link from the compiler object to the
plugin object to check the 'verbose' setting.  This patch adds a
'verbose' setting directly to the compiler object instead.

Second, the 'find' method implicitly knew which compiler base name
("gcc" or "g++") to use.  This patch makes this a parameter that is
passed in by the plugin.

libcc1

	* libcp1.cc (compiler, compiler_triplet_regexp)
	(compiler_driver_filename): Remove.
	(libcp1::libcp1): Update.
	(make_regexp, libcp1::compiler::find)
	(libcp1::compiler_triplet_regexp::find)
	(libcp1::compiler_driver_filename::find): Remove.
	(libcp1_set_verbose, libcp1_set_arguments)
	(libcp1_set_triplet_regexp, libcp1_set_driver_filename): Update.
	* libcc1.cc (compiler, compiler_triplet_regexp)
	(compiler_driver_filename): Remove.
	(libcc1::libcc1): Update.
	(make_regexp, libcc1::compiler::find)
	(libcc1::compiler_triplet_regexp::find)
	(libcc1::compiler_driver_filename::find): Remove.
	(libcc1_set_verbose, libcc1_set_arguments)
	(libcc1_set_triplet_regexp, libcc1_set_driver_filename): Update.
	* compiler.cc: New file.
	* compiler.hh: New file.
	* Makefile.in: Rebuild.
	* Makefile.am (libcc1_la_SOURCES): Add compiler.hh, compiler.cc.
This commit is contained in:
Tom Tromey 2021-05-04 15:26:58 -06:00
parent 30c9604c2c
commit 1d9c972601
6 changed files with 230 additions and 298 deletions

View File

@ -75,7 +75,7 @@ libcp1plugin_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
LTLDFLAGS = $(shell $(SHELL) $(top_srcdir)/../libtool-ldflags $(LDFLAGS))
libcc1_la_LDFLAGS = -module -export-symbols $(srcdir)/libcc1.sym
libcc1_la_SOURCES = findcomp.cc libcc1.cc libcp1.cc \
names.cc names.hh $(shared_source) \
compiler.cc compiler.hh names.cc names.hh $(shared_source) \
$(marshall_c_source) $(marshall_cxx_source)
libcc1_la_LIBADD = $(libiberty)
libcc1_la_DEPENDENCIES = $(libiberty_dep)

View File

@ -144,8 +144,8 @@ am__installdirs = "$(DESTDIR)$(cc1libdir)" "$(DESTDIR)$(plugindir)"
LTLIBRARIES = $(cc1lib_LTLIBRARIES) $(plugin_LTLIBRARIES)
am__objects_1 = callbacks.lo connection.lo marshall.lo
am__objects_2 =
am_libcc1_la_OBJECTS = findcomp.lo libcc1.lo libcp1.lo names.lo \
$(am__objects_1) $(am__objects_2) $(am__objects_2)
am_libcc1_la_OBJECTS = findcomp.lo libcc1.lo libcp1.lo compiler.lo \
names.lo $(am__objects_1) $(am__objects_2) $(am__objects_2)
libcc1_la_OBJECTS = $(am_libcc1_la_OBJECTS)
@ENABLE_PLUGIN_TRUE@am_libcc1_la_rpath = -rpath $(cc1libdir)
am_libcc1plugin_la_OBJECTS = libcc1plugin.lo $(am__objects_1) \
@ -428,7 +428,7 @@ libcp1plugin_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
LTLDFLAGS = $(shell $(SHELL) $(top_srcdir)/../libtool-ldflags $(LDFLAGS))
libcc1_la_LDFLAGS = -module -export-symbols $(srcdir)/libcc1.sym
libcc1_la_SOURCES = findcomp.cc libcc1.cc libcp1.cc \
names.cc names.hh $(shared_source) \
compiler.cc compiler.hh names.cc names.hh $(shared_source) \
$(marshall_c_source) $(marshall_cxx_source)
libcc1_la_LIBADD = $(libiberty)
@ -577,6 +577,7 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/callbacks.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/compiler.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/connection.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/findcomp.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcc1.Plo@am__quote@

118
libcc1/compiler.cc Normal file
View File

@ -0,0 +1,118 @@
/* Compiler handling for plugin
Copyright (C) 2014-2021 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include <cc1plugin-config.h>
#include <string>
#include <sstream>
#include "libiberty.h"
#include "compiler.hh"
#include "xregex.h"
#include "findcomp.hh"
#include "intl.h"
// Construct an appropriate regexp to match the compiler name.
static std::string
make_regexp (const char *triplet_regexp, const char *compiler)
{
std::stringstream buf;
buf << "^" << triplet_regexp << "-";
// Quote the compiler name in case it has something funny in it.
for (const char *p = compiler; *p; ++p)
{
switch (*p)
{
case '.':
case '^':
case '$':
case '*':
case '+':
case '?':
case '(':
case ')':
case '[':
case '{':
case '\\':
case '|':
buf << '\\';
break;
}
buf << *p;
}
buf << "$";
return buf.str ();
}
char *
cc1_plugin::compiler::find (const char *, std::string &) const
{
return xstrdup (_("Compiler has not been specified"));
}
char *
cc1_plugin::compiler_triplet_regexp::find (const char *base,
std::string &compiler) const
{
std::string rx = make_regexp (triplet_regexp_.c_str (), base);
if (verbose)
fprintf (stderr, _("searching for compiler matching regex %s\n"),
rx.c_str());
regex_t triplet;
int code = regcomp (&triplet, rx.c_str (), REG_EXTENDED | REG_NOSUB);
if (code != 0)
{
size_t len = regerror (code, &triplet, NULL, 0);
char err[len];
regerror (code, &triplet, err, len);
return concat ("Could not compile regexp \"",
rx.c_str (),
"\": ",
err,
(char *) NULL);
}
if (!find_compiler (triplet, &compiler))
{
regfree (&triplet);
return concat ("Could not find a compiler matching \"",
rx.c_str (),
"\"",
(char *) NULL);
}
regfree (&triplet);
if (verbose)
fprintf (stderr, _("found compiler %s\n"), compiler.c_str());
return NULL;
}
char *
cc1_plugin::compiler_driver_filename::find (const char *,
std::string &compiler) const
{
// Simulate fnotice by fprintf.
if (verbose)
fprintf (stderr, _("using explicit compiler filename %s\n"),
driver_filename_.c_str());
compiler = driver_filename_;
return NULL;
}

83
libcc1/compiler.hh Normal file
View File

@ -0,0 +1,83 @@
/* Compiler handling for plugin
Copyright (C) 2014-2020 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef CC1_PLUGIN_COMPILER_HH
#define CC1_PLUGIN_COMPILER_HH
namespace cc1_plugin
{
// Base class for compiler.
class compiler
{
public:
explicit compiler (bool v)
: verbose (v)
{
}
virtual ~compiler () = default;
// Find the compiler. BASE is the base name of the compiler, see
// compiler-name.hh. This sets COMPILER to the resulting path.
// Returns nullptr on success, or a malloc'd error string on
// failure.
virtual char *find (const char *base, std::string &compiler) const;
void set_verbose (bool v)
{
verbose = v;
}
protected:
bool verbose;
};
/* Compiler to set by set_triplet_regexp. */
class compiler_triplet_regexp : public compiler
{
private:
std::string triplet_regexp_;
public:
char *find (const char *base, std::string &compiler) const override;
compiler_triplet_regexp (bool v, std::string triplet_regexp)
: compiler (v), triplet_regexp_ (triplet_regexp)
{
}
};
/* Compiler to set by set_driver_filename. */
class compiler_driver_filename : public compiler
{
private:
std::string driver_filename_;
public:
char *find (const char *base, std::string &compiler) const override;
compiler_driver_filename (bool v, std::string driver_filename)
: compiler (v), driver_filename_ (driver_filename)
{
}
};
}
#endif // CC1_PLUGIN_COMPILER_HH

View File

@ -28,18 +28,15 @@ along with GCC; see the file COPYING3. If not see
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sstream>
#include "marshall.hh"
#include "rpc.hh"
#include "connection.hh"
#include "names.hh"
#include "callbacks.hh"
#include "libiberty.h"
#include "xregex.h"
#include "findcomp.hh"
#include "compiler-name.hh"
#include "intl.h"
#include "gcc-c-interface.h"
#include "compiler.hh"
struct libcc1;
@ -71,54 +68,7 @@ struct libcc1 : public gcc_c_context
/* Non-zero as an equivalent to gcc driver option "-v". */
bool verbose;
/* Compiler to set by set_triplet_regexp or set_driver_filename. */
class compiler
{
protected:
libcc1 *self_;
public:
compiler (libcc1 *self) : self_ (self)
{
}
virtual char *find (std::string &compiler) const;
virtual ~compiler ()
{
}
};
std::unique_ptr<compiler> compilerp;
/* Compiler to set by set_triplet_regexp. */
class compiler_triplet_regexp : public compiler
{
private:
std::string triplet_regexp_;
public:
char *find (std::string &compiler) const override;
compiler_triplet_regexp (libcc1 *self, std::string triplet_regexp)
: compiler (self), triplet_regexp_ (triplet_regexp)
{
}
virtual ~compiler_triplet_regexp ()
{
}
};
/* Compiler to set by set_driver_filename. */
class compiler_driver_filename : public compiler
{
private:
std::string driver_filename_;
public:
char *find (std::string &compiler) const override;
compiler_driver_filename (libcc1 *self, std::string driver_filename)
: compiler (self), driver_filename_ (driver_filename)
{
}
virtual ~compiler_driver_filename ()
{
}
};
std::unique_ptr<cc1_plugin::compiler> compilerp;
};
// A local subclass of connection that holds a back-pointer to the
@ -151,7 +101,7 @@ libcc1::libcc1 (const gcc_base_vtable *v,
args (),
source_file (),
verbose (false),
compilerp (new libcc1::compiler (this))
compilerp (new cc1_plugin::compiler (verbose))
{
base.ops = v;
c_ops = cv;
@ -252,101 +202,14 @@ static const struct gcc_c_fe_vtable c_vtable =
// Construct an appropriate regexp to match the compiler name.
static std::string
make_regexp (const char *triplet_regexp, const char *compiler)
{
std::stringstream buf;
buf << "^" << triplet_regexp << "-";
// Quote the compiler name in case it has something funny in it.
for (const char *p = compiler; *p; ++p)
{
switch (*p)
{
case '.':
case '^':
case '$':
case '*':
case '+':
case '?':
case '(':
case ')':
case '[':
case '{':
case '\\':
case '|':
buf << '\\';
break;
}
buf << *p;
}
buf << "$";
return buf.str ();
}
static void
libcc1_set_verbose (struct gcc_base_context *s, int /* bool */ verbose)
{
libcc1 *self = (libcc1 *) s;
self->verbose = verbose != 0;
}
char *
libcc1::compiler::find (std::string &compiler ATTRIBUTE_UNUSED) const
{
return xstrdup (_("Compiler has not been specified"));
}
char *
libcc1::compiler_triplet_regexp::find (std::string &compiler) const
{
std::string rx = make_regexp (triplet_regexp_.c_str (), C_COMPILER_NAME);
if (self_->verbose)
fprintf (stderr, _("searching for compiler matching regex %s\n"),
rx.c_str());
regex_t triplet;
int code = regcomp (&triplet, rx.c_str (), REG_EXTENDED | REG_NOSUB);
if (code != 0)
{
size_t len = regerror (code, &triplet, NULL, 0);
char err[len];
regerror (code, &triplet, err, len);
return concat ("Could not compile regexp \"",
rx.c_str (),
"\": ",
err,
(char *) NULL);
}
if (!find_compiler (triplet, &compiler))
{
regfree (&triplet);
return concat ("Could not find a compiler matching \"",
rx.c_str (),
"\"",
(char *) NULL);
}
regfree (&triplet);
if (self_->verbose)
fprintf (stderr, _("found compiler %s\n"), compiler.c_str());
return NULL;
}
char *
libcc1::compiler_driver_filename::find (std::string &compiler) const
{
// Simulate fnotice by fprintf.
if (self_->verbose)
fprintf (stderr, _("using explicit compiler filename %s\n"),
driver_filename_.c_str());
compiler = driver_filename_;
return NULL;
if (self->compilerp != nullptr)
self->compilerp->set_verbose (self->verbose);
}
static char *
@ -356,7 +219,7 @@ libcc1_set_arguments (struct gcc_base_context *s,
libcc1 *self = (libcc1 *) s;
std::string compiler;
char *errmsg = self->compilerp->find (compiler);
char *errmsg = self->compilerp->find (C_COMPILER_NAME, compiler);
if (errmsg != NULL)
return errmsg;
@ -374,8 +237,9 @@ libcc1_set_triplet_regexp (struct gcc_base_context *s,
{
libcc1 *self = (libcc1 *) s;
self->compilerp.reset (new libcc1::compiler_triplet_regexp (self,
triplet_regexp));
self->compilerp.reset
(new cc1_plugin::compiler_triplet_regexp (self->verbose,
triplet_regexp));
return NULL;
}
@ -385,8 +249,9 @@ libcc1_set_driver_filename (struct gcc_base_context *s,
{
libcc1 *self = (libcc1 *) s;
self->compilerp.reset (new libcc1::compiler_driver_filename (self,
driver_filename));
self->compilerp.reset
(new cc1_plugin::compiler_driver_filename (self->verbose,
driver_filename));
return NULL;
}

View File

@ -28,17 +28,14 @@ along with GCC; see the file COPYING3. If not see
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sstream>
#include "marshall-cp.hh"
#include "rpc.hh"
#include "connection.hh"
#include "names.hh"
#include "callbacks.hh"
#include "libiberty.h"
#include "xregex.h"
#include "findcomp.hh"
#include "compiler-name.hh"
#include "intl.h"
#include "compiler.hh"
struct libcp1;
@ -72,54 +69,7 @@ struct libcp1 : public gcc_cp_context
/* Non-zero as an equivalent to gcc driver option "-v". */
bool verbose;
/* Compiler to set by set_triplet_regexp or set_driver_filename. */
class compiler
{
protected:
libcp1 *self_;
public:
compiler (libcp1 *self) : self_ (self)
{
}
virtual char *find (std::string &compiler) const;
virtual ~compiler ()
{
}
};
std::unique_ptr<compiler> compilerp;
/* Compiler to set by set_triplet_regexp. */
class compiler_triplet_regexp : public compiler
{
private:
std::string triplet_regexp_;
public:
char *find (std::string &compiler) const override;
compiler_triplet_regexp (libcp1 *self, std::string triplet_regexp)
: compiler (self), triplet_regexp_ (triplet_regexp)
{
}
virtual ~compiler_triplet_regexp ()
{
}
};
/* Compiler to set by set_driver_filename. */
class compiler_driver_filename : public compiler
{
private:
std::string driver_filename_;
public:
char *find (std::string &compiler) const override;
compiler_driver_filename (libcp1 *self, std::string driver_filename)
: compiler (self), driver_filename_ (driver_filename)
{
}
virtual ~compiler_driver_filename ()
{
}
};
std::unique_ptr<cc1_plugin::compiler> compilerp;
};
// A local subclass of connection that holds a back-pointer to the
@ -152,7 +102,7 @@ libcp1::libcp1 (const gcc_base_vtable *v,
args (),
source_file (),
verbose (false),
compilerp (new libcp1::compiler (this))
compilerp (new cc1_plugin::compiler (verbose))
{
base.ops = v;
cp_ops = cv;
@ -275,101 +225,14 @@ static const struct gcc_cp_fe_vtable cp_vtable =
// Construct an appropriate regexp to match the compiler name.
static std::string
make_regexp (const char *triplet_regexp, const char *compiler)
{
std::stringstream buf;
buf << "^" << triplet_regexp << "-";
// Quote the compiler name in case it has something funny in it.
for (const char *p = compiler; *p; ++p)
{
switch (*p)
{
case '.':
case '^':
case '$':
case '*':
case '+':
case '?':
case '(':
case ')':
case '[':
case '{':
case '\\':
case '|':
buf << '\\';
break;
}
buf << *p;
}
buf << "$";
return buf.str ();
}
static void
libcp1_set_verbose (struct gcc_base_context *s, int /* bool */ verbose)
{
libcp1 *self = (libcp1 *) s;
self->verbose = verbose != 0;
}
char *
libcp1::compiler::find (std::string &compiler ATTRIBUTE_UNUSED) const
{
return xstrdup (_("Compiler has not been specified"));
}
char *
libcp1::compiler_triplet_regexp::find (std::string &compiler) const
{
std::string rx = make_regexp (triplet_regexp_.c_str (), CP_COMPILER_NAME);
if (self_->verbose)
fprintf (stderr, _("searching for compiler matching regex %s\n"),
rx.c_str());
regex_t triplet;
int code = regcomp (&triplet, rx.c_str (), REG_EXTENDED | REG_NOSUB);
if (code != 0)
{
size_t len = regerror (code, &triplet, NULL, 0);
char err[len];
regerror (code, &triplet, err, len);
return concat ("Could not compile regexp \"",
rx.c_str (),
"\": ",
err,
(char *) NULL);
}
if (!find_compiler (triplet, &compiler))
{
regfree (&triplet);
return concat ("Could not find a compiler matching \"",
rx.c_str (),
"\"",
(char *) NULL);
}
regfree (&triplet);
if (self_->verbose)
fprintf (stderr, _("found compiler %s\n"), compiler.c_str());
return NULL;
}
char *
libcp1::compiler_driver_filename::find (std::string &compiler) const
{
// Simulate fnotice by fprintf.
if (self_->verbose)
fprintf (stderr, _("using explicit compiler filename %s\n"),
driver_filename_.c_str());
compiler = driver_filename_;
return NULL;
if (self->compilerp != nullptr)
self->compilerp->set_verbose (self->verbose);
}
static char *
@ -379,7 +242,7 @@ libcp1_set_arguments (struct gcc_base_context *s,
libcp1 *self = (libcp1 *) s;
std::string compiler;
char *errmsg = self->compilerp->find (compiler);
char *errmsg = self->compilerp->find (CP_COMPILER_NAME, compiler);
if (errmsg != NULL)
return errmsg;
@ -397,8 +260,9 @@ libcp1_set_triplet_regexp (struct gcc_base_context *s,
{
libcp1 *self = (libcp1 *) s;
self->compilerp.reset (new libcp1::compiler_triplet_regexp (self,
triplet_regexp));
self->compilerp.reset
(new cc1_plugin::compiler_triplet_regexp (self->verbose,
triplet_regexp));
return NULL;
}
@ -408,8 +272,9 @@ libcp1_set_driver_filename (struct gcc_base_context *s,
{
libcp1 *self = (libcp1 *) s;
self->compilerp.reset (new libcp1::compiler_driver_filename (self,
driver_filename));
self->compilerp.reset
(new cc1_plugin::compiler_driver_filename (self->verbose,
driver_filename));
return NULL;
}