From 8b40c60072c0549d18afb1e19fa28ee18036957c Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Fri, 12 Mar 2021 00:34:27 -0500 Subject: [PATCH] Generate specifier strings directly from cdecl.h Since the specs.lst file is no longer used for output ordering, it is now mostly redundant information. There is another list of specifiers: the enumeration constants in cdecl.h. We can fairly easily reconstruct the strings from this instead. --- Makefile.am | 21 ++++++++--------- src/.gitignore | 2 +- src/gen-specstr.awk | 55 +++++++++++++++++++++++++++++++++++++++++++++ src/namespecs.sed | 23 ------------------- src/output.c | 11 ++------- src/specs.lst | 26 --------------------- 6 files changed, 69 insertions(+), 69 deletions(-) create mode 100755 src/gen-specstr.awk delete mode 100644 src/namespecs.sed delete mode 100644 src/specs.lst diff --git a/Makefile.am b/Makefile.am index 36bba03..9bef1c9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,12 +23,11 @@ MAINTAINERCLEANFILES = src/scan.c src/scan.h src/scan.stamp \ DISTCLEANFILES = -CLEANFILES = src/namespecs.h $(EXTRA_LTLIBRARIES) +CLEANFILES = $(EXTRA_LTLIBRARIES) EXTRA_DIST = bootstrap $(DX_BASEDIR)/scripts/fix-gnulib.pl m4/gnulib-cache.m4 \ - src/types.lst src/specs.lst src/namespecs.sed src/parse.y \ - src/parse.stamp src/scan.l src/scan.stamp \ - COPYING.WTFPL2 README.md INSTALL + src/types.lst src/parse.y src/parse.stamp src/scan.l \ + src/scan.stamp COPYING.WTFPL2 README.md INSTALL dist_man_MANS = doc/cdecl99.1 doc/libcdecl.3 @@ -87,14 +86,9 @@ src/parse.lo: src/scan.h src/scan.lo: src/parse.h src/parse-decl.lo: src/scan.h src/parse.h src/typemap.h src/error.lo: src/errtab.h -src/output.lo: src/namespecs.h +src/output.lo: src/specstr.h test/declgen.lo: test/typegen.h -src/namespecs.h: $(srcdir)/src/specs.lst $(srcdir)/src/namespecs.sed - $(AM_V_GEN) sed -f $(srcdir)/src/namespecs.sed \ - < $(srcdir)/src/specs.lst > $@.tmp - $(AM_V_at) mv -f $@.tmp $@ - # Supporting rules for gettext. include $(top_srcdir)/common/snippet/gettext.mk @@ -203,6 +197,13 @@ src/cmdlist.h: src/gen-cmdlist.awk src/execute.c DISTCLEANFILES += src/cmdlist.h EXTRA_DIST += src/gen-cmdlist.awk +src/specstr.h: src/gen-specstr.awk src/cdecl.h + $(AM_V_GEN) $(AWK) -f $(srcdir)/src/gen-specstr.awk \ + $(srcdir)/src/cdecl.h >$@.tmp + $(AM_V_at) mv -f $@.tmp $@ +DISTCLEANFILES += src/specstr.h +EXTRA_DIST += src/gen-specstr.awk + src/typemap.h: src/gen-typemap.awk src/types.lst $(AM_V_GEN) $(AWK) -f $(srcdir)/src/gen-typemap.awk \ $(srcdir)/src/types.lst >$@.tmp diff --git a/src/.gitignore b/src/.gitignore index a82fb99..e1f0478 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -3,8 +3,8 @@ /commands.h /errtab.h /execute.c -/namespecs.h /options.h /parse.[ch] /scan.[ch] +/specstr.h /typemap.h diff --git a/src/gen-specstr.awk b/src/gen-specstr.awk new file mode 100755 index 0000000..611b2a8 --- /dev/null +++ b/src/gen-specstr.awk @@ -0,0 +1,55 @@ +#!/bin/awk -f +# +# Copyright © 2021 Nick Bowler +# +# Generate a function to return the C keyword corresponding to a specifier +# type as a string, for internal use by the output routines. +# +# License WTFPL2: Do What The Fuck You Want To Public License, version 2. +# This is free software: you are free to do what the fuck you want to. +# There is NO WARRANTY, to the extent permitted by law. + +END { + print "/*" + if (FILENAME) { + print " * Automatically generated by gen-specstr.awk from " FILENAME + } else { + print " * Automatically generated by gen-specstr.awk" + } + print " * Do not edit." + print " */" +} + +BEGIN { + kinds["TYPE"] = kinds["STOR"] = kinds["QUAL"] = kinds["FUNC"] = 1 + underscore["BOOL"] = underscore["COMPLEX"] = underscore["IMAGINARY"] = 1 +} + +$1 ~ /^CDECL_/ { + sub(/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ_].*/, "", $1) + + split($1, parts, "_") + if (parts[2] in kinds) { + if (parts[3] == "IDENT") { + s = "" + } else if (parts[3] in underscore) { + s = "_" substr(parts[3], 1, 1) tolower(substr(parts[3], 2)) + } else { + s = tolower(parts[3]) + } + specs[$1] = s + } +} + +END { + print "static inline const char *spec_string(unsigned type)\n{" + print "\tswitch (type) {" + + for (s in specs) { + print "\tcase " s ": return \"" specs[s] "\";" + } + + print "\t}" + print "\n\tassert(0);" + print "}" +} diff --git a/src/namespecs.sed b/src/namespecs.sed deleted file mode 100644 index 9cb4f6d..0000000 --- a/src/namespecs.sed +++ /dev/null @@ -1,23 +0,0 @@ -1i\ -/*\ - * Copyright © 2011 Nick Bowler\ - *\ - * License WTFPL2: Do What The Fuck You Want To Public License, version 2.\ - * This is free software: you are free to do what the fuck you want to.\ - * There is NO WARRANTY, to the extent permitted by law.\ - */ -1i\ -/*\ - * This file is automatically generated by namespecs.sed.\ - */ -/[^_[:alpha:][:space:]]/b -s/[[:space:]][[:space:]]*/ /g -h -s/_//g -y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ -s/\([[:upper:]]*\) \([[:upper:]]*\)/case CDECL_\1_\2:/ -p -x -s/.* // -s/ident// -s/.*/ return "&";/ diff --git a/src/output.c b/src/output.c index 22fc2e9..f691765 100644 --- a/src/output.c +++ b/src/output.c @@ -47,18 +47,11 @@ size_t cdecl__advance(char **buf, size_t *n, size_t amount) return ret + cdecl__advance_(buf, n, rc); } -static const char *explain_spec_simple(struct cdecl_declspec *s) -{ - switch (s->type) { -# include "namespecs.h" - } - - assert(0); -} +#include "specstr.h" static size_t explain_spec(char *buf, size_t n, struct cdecl_declspec *s) { - const char *keyword = explain_spec_simple(s); + const char *keyword = spec_string(s->type); if (keyword[0] && s->ident) return snprintf(buf, n, "%s %s", keyword, s->ident); diff --git a/src/specs.lst b/src/specs.lst deleted file mode 100644 index d44c4d4..0000000 --- a/src/specs.lst +++ /dev/null @@ -1,26 +0,0 @@ -/* The complete list of declaration specifiers, in desired output order. */ -stor typedef -stor extern -stor static -stor auto -stor register -func inline -qual restrict -qual volatile -qual const -type void -type signed -type unsigned -type char -type short -type long -type int -type float -type double -type _Complex -type _Imaginary -type _Bool -type struct -type union -type enum -type ident -- 2.43.0