]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Add _Bool fallback to public header.
authorNick Bowler <nbowler@draconx.ca>
Sun, 3 Dec 2023 22:31:26 +0000 (17:31 -0500)
committerNick Bowler <nbowler@draconx.ca>
Mon, 4 Dec 2023 05:24:26 +0000 (00:24 -0500)
Even though we have a fallback for _Bool when building the library,
it is missing in some source files that include cdecl.h.  But more
importantly, it is not available to downstream users of the installed
header file.

As far as I'm aware, all compilers which support _Bool in any mode also
support it in strict C89 mode, so it should be sufficient to just use
_Bool if configure detected support for it.  We integrate the newfangled
bake-config script so that the installed header embeds this config.

There is also one inline function which returns _Bool.  We can just
change it to return int instead, since there is no ABI implication.

Makefile.am
doc/libcdecl.3
src/.gitignore
src/cdecl.h
t/installcheck.c

index ee4f7799cb56a250695e637ce2f3888f098f200e..58ec75e2c423d078827a6465c9e1b233f3ba0757 100644 (file)
@@ -29,12 +29,21 @@ EXTRA_DIST = bootstrap $(DX_BASEDIR)/scripts/fix-gnulib.pl m4/gnulib-cache.m4 \
 
 dist_man_MANS = doc/cdecl99.1 doc/libcdecl.3
 
-include_HEADERS = src/cdecl.h
 noinst_HEADERS = conf_pre.h conf_post.h common/src/help.h common/src/tap.h \
-                 common/src/xtra.h src/scan.h src/parse.h t/declgen.h t/test.h
+                 common/src/xtra.h src/cdecl.h src/scan.h src/parse.h \
+                 t/declgen.h t/test.h
 
 noinst_DATA = $(MOFILES)
 
+nodist_include_HEADERS = src/config/cdecl.h
+src/config/cdecl.h: config.h src/cdecl.h $(DX_BASEDIR)/scripts/bake-config.awk
+       $(AM_V_GEN) $(MKDIR_P) $(@D)
+       $(AM_V_at) $(AWK) -f $(DX_BASEDIR)/scripts/bake-config.awk \
+         config.h $(srcdir)/src/cdecl.h >$@-t
+       $(AM_V_at) mv -f $@-t $@
+EXTRA_DIST += $(DX_BASEDIR)/scripts/bake-config.awk
+CLEANFILES += src/config/cdecl.h
+
 shared_gl_objects = $(gnulib_symfiles:.glsym=.lo)
 static_gl_objects = $(gnulib_extra_objects:.lo=.@OBJEXT@)
 
@@ -346,7 +355,7 @@ t_installcheck_CFLAGS =
 t_installcheck_SHORTNAME = x
 
 installcheck-local:
-       rm -f t/installcheck$(EXEEXT)
+       rm -f t/installcheck$(EXEEXT) $(t_installcheck_OBJECTS)
        $(MAKE) $(AM_MAKEFLAGS) t/installcheck$(EXEEXT)
        t/installcheck$(EXEEXT) | diff - $(srcdir)/t/installcheck.exp
 .PHONY: installcheck-local
index 022795acb173e5342cb2ed0d2ebbc9ca79e9e09e..4a882a0b1e474213c7526737b2d7bc1cee0f8772 100644 (file)
@@ -1,4 +1,4 @@
-.Dd November 18, 2023
+.Dd December 3, 2023
 .Dt LIBCDECL \&3 "Cdecl99 Developer's Manual"
 .Os cdecl99
 .Sh NAME
@@ -17,7 +17,7 @@
 .Fd const struct cdecl_error *cdecl_get_error(void);
 .Pp
 .Fd int cdecl_spec_kind(struct cdecl_declspec *spec);
-.Fd bool cdecl_is_abstract(struct cdecl_declarator *declarator);
+.Fd int cdecl_is_abstract(struct cdecl_declarator *declarator);
 .Sh DESCRIPTION
 .Nm
 provides support for parsing C declarations and translating them to something
@@ -230,7 +230,7 @@ union member points to the C string containing the identifier.
 Since a null declarator may be deeply nested in the declarator chain, the
 function
 .Pp
-.Fd bool cdecl_is_abstract(struct cdecl_declarator *declarator);
+.Fd int cdecl_is_abstract(struct cdecl_declarator *declarator);
 .Pp
 can be used to determine whether or not a given declarator declares an
 identifier.
@@ -267,15 +267,17 @@ is positive, then this is an array declarator with the specified length.
 Otherwise, this is an incomplete array declarator.
 .Ss Function Declarators
 .Bd -literal -offset indent
+typedef _Bool cdecl_bool; /* depends on configuration */
 struct cdecl_function {
        struct cdecl *parameters;
-       _Bool variadic;
+       cdecl_bool variadic;
 };
 .Ed
 .Pp
 If
 .Va parameters
-is null, then this is a non-prototype function declarator.
+is null, then this is a non-prototype function declarator with an empty
+identifier list.
 Otherwise,
 .Va parameters
 points to the first element of a singly-linked list of declarations
@@ -284,15 +286,27 @@ Note that, unlike toplevel declarations, each function parameter has exactly
 one full declarator (abstract or otherwise).
 If
 .Va variadic
-is true, then the function is variadic.
+is non-zero, then the function is variadic.
+.Pp
+Please note that if the compiler used to build the library does not support
+.Vt _Bool ,
+then
+.Vt cdecl_bool
+will be defined as
+.Vt signed char
+instead.
+In most cases these will have a compatible binary representation, provided
+that applications do not set
+.Va variadic
+to any values besides 0 or 1.
 .Pp
-Note that old-style function declarations with non-empty identifier lists are
-not directly represented here: this is because they are syntactically identical
-to a prototype where every parameter is a typedef name.
-Since
+Old-style function declarations with non-empty identifier lists cannot be
+directly represented by this structure.
+Such declarations are syntactically identical to a prototype with every
+parameter consisting solely of a typedef name.
 .Nm
-isn't a C compiler, there is no way for its parser to tell these two kinds of
-declarations apart.
+cannot tell these apart when parsing and thus will return a parameter
+list, which can be rendered as expected.
 .Sh ERROR HANDLING
 Some functions in
 .Nm
index d32bd40f927834523805efe19de3fdba4a2fa3b8..25259c756fdd6533fa5cde744eb99140789c189b 100644 (file)
@@ -1,6 +1,7 @@
 /*.stamp
 /cmdlist.h
 /commands.h
+/config
 /errmsg.h
 /execute.c
 /keywords.c
index 20a94f2b2c4bb4a2305a1952045e57d877b51204..97f39bca800d8b49fb9be56daeccc5a806798fff 100644 (file)
@@ -1,18 +1,18 @@
 /*
- *  Copyright © 2011, 2021, 2023 Nick Bowler
+ * Copyright © 2011, 2021, 2023 Nick Bowler
  *
- *  This program 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 of the License, or
- *  (at your option) any later version.
+ * This program 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 of the License, or
+ * (at your option) any later version.
  *
- *  This program 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.
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifndef CDECL_H_
 #include <stddef.h>
 #include <stdint.h>
 
+/* Compatibility typedefs */
+#if HAVE__BOOL
+typedef _Bool cdecl_bool;
+#else
+typedef signed char cdecl_bool;
+#endif
+
 /* Declaration specifier kinds. */
 enum {
        CDECL_SPEC_TYPE = 256,
        CDECL_SPEC_STOR = 512,
        CDECL_SPEC_QUAL = 1024,
-       CDECL_SPEC_FUNC = 2048,
+       CDECL_SPEC_FUNC = 2048
 };
 
 enum {
@@ -54,7 +61,7 @@ enum {
        CDECL_QUAL_RESTRICT = CDECL_SPEC_QUAL,
        CDECL_QUAL_VOLATILE,
        CDECL_QUAL_CONST,
-       CDECL_FUNC_INLINE = CDECL_SPEC_FUNC,
+       CDECL_FUNC_INLINE = CDECL_SPEC_FUNC
 };
 
 /* Declarator types. */
@@ -63,7 +70,7 @@ enum {
        CDECL_DECL_IDENT,
        CDECL_DECL_POINTER,
        CDECL_DECL_ARRAY,
-       CDECL_DECL_FUNCTION,
+       CDECL_DECL_FUNCTION
 };
 
 struct cdecl {
@@ -89,7 +96,7 @@ struct cdecl {
                        } array;
                        struct cdecl_function {
                                struct cdecl *parameters;
-                               _Bool variadic;
+                               cdecl_bool variadic;
                        } function;
                } u;
        } *declarators;
@@ -107,7 +114,7 @@ static inline int cdecl_spec_kind(const struct cdecl_declspec *spec)
        return spec->type & ~(CDECL_SPEC_TYPE-1u);
 }
 
-static inline _Bool cdecl_is_abstract(const struct cdecl_declarator *d)
+static inline int cdecl_is_abstract(const struct cdecl_declarator *d)
 {
        while (d->child)
                d = d->child;
index 1765671b57afc3db93d23a9aeaca07da5c2ebb2f..42de06502ac44d7756255d7ea72bbbc09d6f698d 100644 (file)
@@ -17,6 +17,9 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+/* Define configuration macros used in cdecl.h to expand to syntax errors. */
+#define HAVE__BOOL (0xdeadc0de * "HAVE__BOOL")
+
 #include <cdecl.h>
 
 int main(void)