]> git.draconx.ca Git - cdecl99.git/commitdiff
Generate specifier strings directly from cdecl.h
authorNick Bowler <nbowler@draconx.ca>
Fri, 12 Mar 2021 05:34:27 +0000 (00:34 -0500)
committerNick Bowler <nbowler@draconx.ca>
Fri, 12 Mar 2021 05:39:25 +0000 (00:39 -0500)
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
src/.gitignore
src/gen-specstr.awk [new file with mode: 0755]
src/namespecs.sed [deleted file]
src/output.c
src/specs.lst [deleted file]

index 36bba03d9834692c0fa5e78352879ae2e52cd101..9bef1c9f3770c119743084022053dd21647cc7e6 100644 (file)
@@ -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
index a82fb997abc570fbb30be2a4a178378fe49010c0..e1f0478cc8a6a550ba282d6ce4f9770421c8c9ae 100644 (file)
@@ -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 (executable)
index 0000000..611b2a8
--- /dev/null
@@ -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 (file)
index 9cb4f6d..0000000
+++ /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 "&";/
index 22fc2e9d0e221746a4ba2fef93de0051ac5153a6..f691765ea592187d038b3d1dd16a2e080bef41b7 100644 (file)
@@ -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 (file)
index d44c4d4..0000000
+++ /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