From ce6fc53682acb605e6358e22b5eccf4780a61a22 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sun, 20 Nov 2011 15:17:52 -0500 Subject: [PATCH] Generate the specifier switch statement automatically. We already have a list of specifier names, so use them! --- Makefile.am | 6 ++++ src/.gitignore | 1 + src/namespecs.sed | 23 +++++++++++++++ src/output.c | 72 ++++++++++------------------------------------- 4 files changed, 45 insertions(+), 57 deletions(-) create mode 100644 src/namespecs.sed diff --git a/Makefile.am b/Makefile.am index a6782d7..ea3330c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,12 +55,18 @@ src/parse-decl.lo: src/scan.h src/parse.h src/typemap.lo: src/validtypes.h src/error.lo: src/errtab.h src/normalize.lo: src/ordspecs.h +src/output.lo: src/namespecs.h src/validtypes.h: $(srcdir)/src/types.lst $(srcdir)/src/validtypes.sed $(AM_V_GEN)sed -f $(srcdir)/src/validtypes.sed \ < $(srcdir)/src/types.lst > $@.tmp $(AM_V_at)mv -f $@.tmp $@ +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 $@ + src/ordspecs.h: $(srcdir)/src/specs.lst $(srcdir)/src/ordspecs.sed $(AM_V_GEN) sed -f $(srcdir)/src/ordspecs.sed \ < $(srcdir)/src/specs.lst > $@.tmp diff --git a/src/.gitignore b/src/.gitignore index 50d03eb..faa646e 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -3,5 +3,6 @@ parse.[ch] scan.stamp scan.[ch] validtypes.h +namespecs.h ordspecs.h errtab.h diff --git a/src/namespecs.sed b/src/namespecs.sed new file mode 100644 index 0000000..9cb4f6d --- /dev/null +++ b/src/namespecs.sed @@ -0,0 +1,23 @@ +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 3b4fa9f..8fffd24 100644 --- a/src/output.c +++ b/src/output.c @@ -48,66 +48,24 @@ size_t cdecl__advance(char **buf, size_t *n, size_t amount) return ret + cdecl__advance_(buf, n, rc); } -static size_t explain_spec(char *buf, size_t n, struct cdecl_declspec *s) +static const char *explain_spec_simple(struct cdecl_declspec *s) { switch (s->type) { - /* Function specifiers */ - case CDECL_FUNC_INLINE: - return snprintf(buf, n, "inline"); - /* Storage-class specifiers */ - case CDECL_STOR_TYPEDEF: - return snprintf(buf, n, "typedef"); - case CDECL_STOR_EXTERN: - return snprintf(buf, n, "extern"); - case CDECL_STOR_STATIC: - return snprintf(buf, n, "static"); - case CDECL_STOR_AUTO: - return snprintf(buf, n, "auto"); - case CDECL_STOR_REGISTER: - return snprintf(buf, n, "register"); - /* Type qualifiers */ - case CDECL_QUAL_RESTRICT: - return snprintf(buf, n, "restrict"); - case CDECL_QUAL_VOLATILE: - return snprintf(buf, n, "volatile"); - case CDECL_QUAL_CONST: - return snprintf(buf, n, "const"); - /* Type specifiers */ - case CDECL_TYPE_VOID: - return snprintf(buf, n, "void"); - case CDECL_TYPE_CHAR: - return snprintf(buf, n, "char"); - case CDECL_TYPE_SHORT: - return snprintf(buf, n, "short"); - case CDECL_TYPE_INT: - return snprintf(buf, n, "int"); - case CDECL_TYPE_LONG: - return snprintf(buf, n, "long"); - case CDECL_TYPE_FLOAT: - return snprintf(buf, n, "float"); - case CDECL_TYPE_DOUBLE: - return snprintf(buf, n, "double"); - case CDECL_TYPE_SIGNED: - return snprintf(buf, n, "signed"); - case CDECL_TYPE_UNSIGNED: - return snprintf(buf, n, "unsigned"); - case CDECL_TYPE_BOOL: - return snprintf(buf, n, "_Bool"); - case CDECL_TYPE_COMPLEX: - return snprintf(buf, n, "_Complex"); - case CDECL_TYPE_IMAGINARY: - return snprintf(buf, n, "_Imaginary"); - case CDECL_TYPE_STRUCT: - return snprintf(buf, n, "struct %s", s->ident); - case CDECL_TYPE_UNION: - return snprintf(buf, n, "union %s", s->ident); - case CDECL_TYPE_ENUM: - return snprintf(buf, n, "enum %s", s->ident); - case CDECL_TYPE_IDENT: - return snprintf(buf, n, "%s", s->ident); - default: - assert(0); +# include "namespecs.h" } + + assert(0); +} + +static size_t explain_spec(char *buf, size_t n, struct cdecl_declspec *s) +{ + const char *keyword = explain_spec_simple(s); + + if (keyword[0] && s->ident) + return snprintf(buf, n, "%s %s", keyword, s->ident); + else if (s->ident) + return snprintf(buf, n, "%s", s->ident); + return snprintf(buf, n, "%s", keyword); } /* -- 2.43.2