Mercurial > ~dholland > hg > tradcpp > index.cgi
changeset 184:d359d9b86327
Don't rely on anonymous unions.
They break on... dun dun dun... Solaris. Such a shock.
author | David A. Holland |
---|---|
date | Fri, 12 Jun 2015 03:35:01 -0400 |
parents | 4c3375895c6e |
children | 16b4451e34b8 |
files | CHANGES macro.c union.h |
diffstat | 3 files changed, 58 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES Fri Jun 12 03:28:19 2015 -0400 +++ b/CHANGES Fri Jun 12 03:35:01 2015 -0400 @@ -1,6 +1,8 @@ pending - Don't rely on <stdbool.h> existing, as (predictably) it doesn't work on Solaris. + - Similarly, don't rely on C11 anonymous unions as the Solaris + compiler vomits on them. - Typo fix in man page from Jason McIntyre; and change "Usage" to "usage" in usage for pedantic reasons, from Igor Sobrado. - Accept "-" as either input or output file name to mean stdin or
--- a/macro.c Fri Jun 12 03:28:19 2015 -0400 +++ b/macro.c Fri Jun 12 03:35:01 2015 -0400 @@ -32,6 +32,7 @@ #include <stdlib.h> #include <string.h> +#include "union.h" #include "array.h" #include "mode.h" #include "place.h" @@ -41,13 +42,19 @@ struct expansionitem { enum { EI_STRING, EI_PARAM, EI_FILE, EI_LINE } itemtype; union { - char *string; /* EI_STRING */ - unsigned param; /* EI_PARAM */ - }; + char *ei_string; /* for EI_STRING */ + unsigned ei_param; /* for EI_PARAM */ + } UN; }; DECLARRAY(expansionitem, static UNUSED); DEFARRAY(expansionitem, static); +#ifdef NEED_UNION_ACCESSORS +#define ei_string un.ei_string +#define ei_param un.ei_param +#endif + + struct macro { struct place defplace; struct place expansionplace; @@ -78,7 +85,7 @@ ei = domalloc(sizeof(*ei)); ei->itemtype = EI_STRING; - ei->string = dostrdup(string); + ei->ei_string = dostrdup(string); return ei; } @@ -90,7 +97,7 @@ ei = domalloc(sizeof(*ei)); ei->itemtype = EI_STRING; - ei->string = dostrndup(string, len); + ei->ei_string = dostrndup(string, len); return ei; } @@ -102,7 +109,7 @@ ei = domalloc(sizeof(*ei)); ei->itemtype = EI_PARAM; - ei->param = param; + ei->ei_param = param; return ei; } @@ -134,7 +141,7 @@ { switch (ei->itemtype) { case EI_STRING: - dostrfree(ei->string); + dostrfree(ei->ei_string); break; case EI_PARAM: case EI_FILE: @@ -154,12 +161,12 @@ } switch (ei1->itemtype) { case EI_STRING: - if (strcmp(ei1->string, ei2->string) != 0) { + if (strcmp(ei1->ei_string, ei2->ei_string) != 0) { return false; } break; case EI_PARAM: - if (ei1->param != ei2->param) { + if (ei1->ei_param != ei2->ei_param) { return false; } break; @@ -822,10 +829,10 @@ ei = expansionitemarray_get(&es->curmacro->expansion, i); switch (ei->itemtype) { case EI_STRING: - len += strlen(ei->string); + len += strlen(ei->ei_string); break; case EI_PARAM: - arg = stringarray_get(&es->args, ei->param); + arg = stringarray_get(&es->args, ei->ei_param); len += strlen(arg); break; case EI_FILE: @@ -843,10 +850,10 @@ ei = expansionitemarray_get(&es->curmacro->expansion, i); switch (ei->itemtype) { case EI_STRING: - strcat(ret, ei->string); + strcat(ret, ei->ei_string); break; case EI_PARAM: - arg = stringarray_get(&es->args, ei->param); + arg = stringarray_get(&es->args, ei->ei_param); strcat(ret, arg); break; case EI_FILE:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/union.h Fri Jun 12 03:35:01 2015 -0400 @@ -0,0 +1,36 @@ +/*- + * Copyright (c) 2015 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David A. Holland. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(__clang__) || defined(__GNUC__) || __STDC__ > 201101 +#define UN +#undef NEED_UNION_ACCESSORS +#else +#define UN un +#define NEED_UNION_ACCESSORS +#endif