LLgen: use size_t, reduce warnings, other small changes

Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts.  Sizes were unsigned int in
traditional C but are size_t in C89.

Silence some clang warnings.  Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses.  Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body.  The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`.  The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.

Shuffle extern function declarations.  Add missing declaration for
LLparse().  Stop declaring RENAME(); it doesn't exist.  Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.

Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.

In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().

Remove alloc.h.  If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it.  Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.

Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
This commit is contained in:
George Koehler 2019-10-22 12:56:50 -04:00
parent eb520a343d
commit 8bb395b147
19 changed files with 103 additions and 134 deletions

View file

@ -1,6 +1,6 @@
clibrary {
name = "headers",
hdrs = { "./src/*.h" }
hdrs = { "./src/*.h" } -- rm alloc.h
}
cprogram {

View file

@ -1,8 +1,6 @@
/* $Id$ */
#ifdef LL_DEBUG
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define LL_assert(x) assert(x)
#else
#define LL_assert(x) /* nothing */

View file

@ -118,8 +118,8 @@ static struct nonterminal *nonterminals;
/* These functions must be called instead of the original functions in
* 'malloc.h'. They offer a checking allocation mechanism.
*/
static char *Malloc(unsigned);
static char *Realloc(char*, unsigned);
static void *Malloc(size_t);
static void *Realloc(void *, size_t);
@ -164,11 +164,11 @@ void LLnc_recover(void);
static char *Malloc(unsigned size)
static void *Malloc(size_t size)
{
char *p;
void *p;
if ((p = malloc(size)) == (char *)0) {
if ((p = malloc(size)) == NULL) {
fprintf(stderr, "fatal error: out of memory\n");
exit(1);
}
@ -176,11 +176,11 @@ static char *Malloc(unsigned size)
}
static char *Realloc(char *ptr, unsigned size)
static void *Realloc(void *ptr, size_t size)
{
char *p;
void *p;
if ((p = realloc(ptr, size)) == (char *)0) {
if ((p = realloc(ptr, size)) == NULL) {
fprintf(stderr, "fatal error: out of memory\n");
exit(1);
}
@ -195,13 +195,13 @@ static void init_grammar(void)
int i;
terminals = (struct terminal *)
Malloc((unsigned) LLFIRST_NT * sizeof(struct terminal));
Malloc(LLFIRST_NT * sizeof(struct terminal));
for (i = 0; i < LLFIRST_NT; i++) {
(terminals + i)->link = (struct symbol *)0;
}
nonterminals = (struct nonterminal *)
Malloc((unsigned)LLNNONTERMINALS * sizeof(struct nonterminal));
Malloc(LLNNONTERMINALS * sizeof(struct nonterminal));
for (i = 0; i < LLNNONTERMINALS; i++) {
(nonterminals + i)->rule = (struct lhs *)0;
(nonterminals + i)->link = (struct symbol *)0;
@ -637,8 +637,7 @@ static void new_head(struct stacks *stack, struct stack_elt *ptr)
/* buffer full? */
stack->heads_buf_size += LLHEADS_BUF_INCR;
stack->heads_buf = (struct stack_elt **)
Realloc((char *)
stack->heads_buf, (unsigned)
Realloc(stack->heads_buf,
stack->heads_buf_size *
sizeof(struct stack_elt *)
);
@ -677,8 +676,8 @@ static void to_delete(struct stacks *stack, struct stack_elt *ptr)
else if (stack->nr_cleanups == stack->cleanup_buf_size) {
stack->cleanup_buf_size += LLCLEANUP_BUF_INCR;
stack->cleanup_buf = (struct stack_elt **)
Realloc((char *) stack->cleanup_buf,
(unsigned) stack->cleanup_buf_size *
Realloc(stack->cleanup_buf,
stack->cleanup_buf_size *
sizeof(struct stack_elt *));
}
*(stack->cleanup_buf + stack->nr_cleanups) = ptr;
@ -790,8 +789,7 @@ static int join(struct stacks *stack, struct stack_elt *top, int l_ahead)
/* Allocate one more pointer to descendants */
size = se->nr_nexts * sizeof(struct edge);
se->edges = (struct edge *)Realloc((char *) se->edges,
(unsigned) size);
se->edges = (struct edge *)Realloc(se->edges, size);
/* Link it */
(se->edges + se->nr_nexts - 1)->ptr = top->edges->ptr;
@ -904,8 +902,8 @@ static void generate_heads(struct stacks *stack, struct stack_elt *se,
else if (stack->nr_visited == stack->visited_buf_size) {
stack->visited_buf_size += LL_VIS_INCR;
stack->visited_buf = (struct stack_elt **)
Realloc((char *) stack->visited_buf,
(unsigned) stack->visited_buf_size *
Realloc(stack->visited_buf,
stack->visited_buf_size *
sizeof(struct stack_elt *));
}
*(stack->visited_buf + stack->nr_visited) = next_se;
@ -1089,7 +1087,7 @@ static struct stack_elt *split(struct stack_elt *se)
#endif
new_stack->edges = (struct edge *)
Malloc((unsigned)se->nr_nexts * sizeof(struct edge));
Malloc(se->nr_nexts * sizeof(struct edge));
/* Copy gets the same successors as the original */
memcpy((char *) new_stack->edges, (char *) se->edges,
@ -1242,14 +1240,14 @@ static void match_heads(struct stacks *stack, int symb)
if (stack->heads_buf_size == 0) {
stack->heads_buf_size = LLHEADS_BUF_INCR;
stack->heads_buf = (struct stack_elt **)
Malloc((unsigned)stack->heads_buf_size *
Malloc(stack->heads_buf_size *
sizeof(struct stack_elt *));
}
else if (stack->nr_heads == stack->heads_buf_size) {
stack->heads_buf_size += LLHEADS_BUF_INCR;
stack->heads_buf = (struct stack_elt **)
Realloc((char *) stack->heads_buf,
(unsigned) stack->heads_buf_size *
Realloc(stack->heads_buf,
stack->heads_buf_size *
sizeof(struct stack_elt *));
}
*(stack->heads_buf + stack->nr_heads) =

View file

@ -6,7 +6,6 @@
/* $Id$ */
#ifdef LL_DEBUG
#include <assert.h>
#include <stdio.h>
#define LL_assert(x) assert(x)
#else
#define LL_assert(x) /* nothing */
@ -251,7 +250,7 @@ LL_NOSCANDONE(C_IDENT);
ff->ff_name = p;
ff->ff_next = start;
start = ff;
while (ff = ff->ff_next) {
while ((ff = ff->ff_next)) {
if (! strcmp(p, ff->ff_name)) {
error(linecount, "\"%s\" already used in a %%start", p);
break;
@ -474,7 +473,7 @@ LL6_simpleproduction(
{ if (n_alts >= max_alts-2) {
alt_table = (p_gram ) ralloc(
(p_mem) alt_table,
(unsigned)(max_alts+=ALTINCR)*sizeof(t_gram));
(max_alts+=ALTINCR)*sizeof(t_gram));
}
if (t & DEF) {
if (haddefault) {
@ -687,7 +686,7 @@ LL_SAFE(C_ILLEGAL);
if (n_rules >= max_rules-2) {
rule_table = (p_gram) ralloc(
(p_mem) rule_table,
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
(max_rules+=RULEINCR)*sizeof(t_gram));
}
elmcnt++;
rule_table[n_rules++] =
@ -730,7 +729,7 @@ LL7_elem(
{ if (n_rules >= max_rules-2) {
rule_table = (p_gram) ralloc(
(p_mem) rule_table,
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
(max_rules+=RULEINCR)*sizeof(t_gram));
}
kind = FIXED;
cnt = 0;
@ -782,7 +781,7 @@ LLsdecr(4);
if (n_rules >= max_rules-2) {
rule_table = (p_gram) ralloc(
(p_mem) rule_table,
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
(max_rules+=RULEINCR)*sizeof(t_gram));
}
}
elem = *--(q->t_rule);
@ -1314,7 +1313,7 @@ STATIC p_gram copyrule(register p_gram p,int length)
register p_gram t;
p_gram rule;
t = (p_gram) alloc((unsigned) length * sizeof(t_gram));
t = (p_gram) alloc(length * sizeof(t_gram));
rule = t;
while (length--) {
*t++ = *p++;

View file

@ -131,7 +131,7 @@ def { register string p; }
ff->ff_name = p;
ff->ff_next = start;
start = ff;
while (ff = ff->ff_next) {
while ((ff = ff->ff_next)) {
if (! strcmp(p, ff->ff_name)) {
error(linecount, "\"%s\" already used in a %%start", p);
break;
@ -261,7 +261,7 @@ productions(p_gram *p;)
{ if (n_alts >= max_alts-2) {
alt_table = (p_gram ) ralloc(
(p_mem) alt_table,
(unsigned)(max_alts+=ALTINCR)*sizeof(t_gram));
(max_alts+=ALTINCR)*sizeof(t_gram));
}
if (t & DEF) {
if (haddefault) {
@ -348,7 +348,7 @@ simpleproduction(p_gram *p; register int *conflres;)
if (n_rules >= max_rules-2) {
rule_table = (p_gram) ralloc(
(p_mem) rule_table,
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
(max_rules+=RULEINCR)*sizeof(t_gram));
}
elmcnt++;
rule_table[n_rules++] =
@ -363,7 +363,7 @@ simpleproduction(p_gram *p; register int *conflres;)
{ if (n_rules >= max_rules-2) {
rule_table = (p_gram) ralloc(
(p_mem) rule_table,
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
(max_rules+=RULEINCR)*sizeof(t_gram));
}
kind = FIXED;
cnt = 0;
@ -391,7 +391,7 @@ simpleproduction(p_gram *p; register int *conflres;)
if (n_rules >= max_rules-2) {
rule_table = (p_gram) ralloc(
(p_mem) rule_table,
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
(max_rules+=RULEINCR)*sizeof(t_gram));
}
}
elem = *--(q->t_rule);
@ -653,7 +653,7 @@ STATIC p_gram copyrule(register p_gram p,int length)
register p_gram t;
p_gram rule;
t = (p_gram) alloc((unsigned) length * sizeof(t_gram));
t = (p_gram) alloc(length * sizeof(t_gram));
rule = t;
while (length--) {
*t++ = *p++;

View file

@ -5,7 +5,6 @@
/* $Id$ */
#ifdef LL_DEBUG
#include <assert.h>
#include <stdio.h>
#define LL_assert(x) assert(x)
#else
#define LL_assert(x) /* nothing */

View file

@ -17,7 +17,6 @@
*/
#include <stdlib.h>
# include "alloc.h"
# include "types.h"
# include "extern.h"
@ -27,7 +26,7 @@ static string rcsid = "$Id$";
static string e_nomem = "Out of memory";
p_mem alloc(unsigned int size)
p_mem alloc(size_t size)
{
/*
Allocate "size" bytes. Panic if it fails
@ -38,7 +37,7 @@ p_mem alloc(unsigned int size)
return p;
}
p_mem ralloc(p_mem p,unsigned int size)
p_mem ralloc(p_mem p,size_t size)
{
/*
Re-allocate the chunk of memory indicated by "p", to
@ -61,7 +60,7 @@ p_mem new_mem(register p_info p)
be updated each time this routine is called
*/
p_mem rp;
unsigned sz;
size_t sz;
if (p->i_max >= p->i_top) { /* No more free elements */
sz = p->i_size;

View file

@ -1,19 +0,0 @@
/* Copyright (c) 2019 ACK Project.
* See the copyright notice in the ACK home directory,
* in the file "Copyright".
*
* Created on: 2019-02-16
*
*/
#ifndef ALLOC_H_
#define ALLOC_H_
#include "types.h"
p_mem alloc(unsigned int size);
p_mem ralloc(p_mem p,unsigned int size);
p_mem new_mem(register p_info p);
#endif /* ALLOC_H_ */

View file

@ -19,7 +19,6 @@
# include <stdlib.h>
# include <stdio.h>
# include "alloc.h"
# include "types.h"
# include "extern.h"
# include "sets.h"
@ -198,7 +197,7 @@ void do_compute(void)
for (f = files; f < maxfiles; f++)
{
register p_set s;
f->f_used = s = (p_set) alloc((unsigned) n * sizeof(*(f->f_used)));
f->f_used = s = (p_set) alloc(n * sizeof(*(f->f_used)));
for (i = n; i; i--)
*s++ = 0;
for (i = f->f_nonterminals; i != -1; i = p->n_next)
@ -497,9 +496,7 @@ STATIC int nc_first(p_set setp,register p_gram p,int flag)
q = g_getterm(p);
if (flag == 0)
{
if (nc_first(q->t_nc_first,q->t_rule,0))/*nothing*/;
}
(void)nc_first(q->t_nc_first,q->t_rule,0);
if (!noenter) s |= setunion(setp,q->t_nc_first);
p++;
if (r_getkind(q) == STAR ||
@ -512,9 +509,7 @@ STATIC int nc_first(p_set setp,register p_gram p,int flag)
l = g_getlink(p);
if (flag == 0)
{
if (nc_first(l->l_nc_symbs,l->l_rule,0))/*nothing*/;
}
(void)nc_first(l->l_nc_symbs,l->l_rule,0);
if (noenter == 0)
{
s |= setunion(setp,l->l_nc_symbs);
@ -528,6 +523,7 @@ STATIC int nc_first(p_set setp,register p_gram p,int flag)
register p_start subp;
if (!noenter)
{
if (subpars_sim)
s |= setunion(setp, start_firsts);
else
@ -535,7 +531,7 @@ STATIC int nc_first(p_set setp,register p_gram p,int flag)
for (subp = g_getsubparse(p); subp;
subp = subp->ff_next)
s |= setunion(setp, (&nonterms[subp->ff_nont])->n_nc_first);
}
}
p++;
continue;
@ -863,7 +859,7 @@ STATIC void do_lengthcomp(void)
register p_nont p;
p_mem alloc();
length = (p_length) alloc((unsigned) (nnonterms * sizeof(*length)));
length = (p_length) alloc(nnonterms * sizeof(*length));
for (pl = &length[nnonterms - 1]; pl >= length; pl--)
{
pl->val = pl->cnt = INFINITY;

View file

@ -19,6 +19,8 @@
* some variables that are visible in more than one file
*/
# include "types.h"
# define LTEXTSZ 256 /* Size of longest token */
/*
@ -94,22 +96,38 @@ extern p_gram illegal_gram;
extern int strip_grammar;
extern int in_production;
/* LLgen.g */
void LLparse(void);
/* check.c */
void conflchecks(void);
/* compute.c */
void do_compute(void);
int empty(p_gram);
int t_safety(int, int, int, int);
int t_after(int, int, int);
/* gencode.c */
void gencode(int);
/* machdep.c */
void TMPNAM(string);
string libpath(string);
/* main.c */
void error(int lineno,string s,string t);
void warning(int lineno,string s,string t);
void fatal(int lineno,string s,string t);
int empty(register p_gram);
int t_safety(int, int, int, int);
int t_after(int, int, int);
string store(string);
void name_init(void);
p_gram search(int, register string, int);
void co_reach(void);
void install(string, string);
void copyfile(string);
void install(string, string);
/* name.c */
void name_init(void);
string store(string);
p_gram search(int, string, int);
/* reach.c */
void co_reach(void);
#endif /* EXTERN_H_ */

View file

@ -20,7 +20,6 @@
#include <stdlib.h>
#include <stdio.h>
# include "alloc.h"
# include "types.h"
# include "io.h"
# include "extern.h"
@ -102,7 +101,7 @@ STATIC void doclose(FILE *f)
STATIC int *mk_tokenlist(void)
{
register int i = ntokens;
register int *p = (int *) alloc((unsigned) (i * sizeof(int))) + i;
register int *p = (int *) alloc(i * sizeof(int)) + i;
while (i--)
*--p = -1;
@ -295,7 +294,7 @@ STATIC void genrecovery(void)
for (psetl = setptr; psetl < maxptr; psetl++)
prset(*psetl);
fputs(c_arrend, f);
index = (int *) alloc((unsigned) (assval * sizeof(int)));
index = (int *) alloc(assval * sizeof(int));
for (q = index; q < &index[assval];)
*q++ = -1;
for (t = tokens; t < maxt; t++)
@ -344,7 +343,7 @@ STATIC void genncrecovery(void)
fprintf(f, "#define LLFIRST_NT %d\n", assval);
fprintf(f, "#define LLSETSIZE %d\n", nbytes);
index = (int *) alloc((unsigned) (assval * sizeof(int)));
index = (int *) alloc(assval * sizeof(int));
for (q = index; q < &index[assval];) *q++ = -1;
for (t = tokens; t < maxt; t++)
{
@ -988,14 +987,14 @@ STATIC int *dopush(register p_gram p, int safety, int toplevel, int **pp)
/*
* The safety only matters if toplevel != 0
*/
unsigned int i = 100;
size_t i = 100;
register int *ip = (int *) alloc(100 * sizeof(int));
*pp = ip;
for (;;)
{
if (ip - *pp >= i)
if ((size_t)(ip - *pp) >= i)
{
*pp = (int *) ralloc((p_mem) (*pp), (i + 100) * sizeof(int));
ip = *pp + i;

View file

@ -16,12 +16,8 @@
* Machine dependant things
*/
#include <stdio.h>
#ifdef USE_SYS
#include <system.h>
#endif
#include <stdlib.h>
#include <string.h>
# include "alloc.h"
# include "extern.h"
# include "types.h"
@ -33,29 +29,18 @@ static string rcsid5 = "$Id$";
#define LIBDIR "lib"
#endif
void UNLINK(string x)
{
/* Must remove the file "x" */
#ifdef USE_SYS
sys_remove(x); /* systemcall to remove file */
#else
remove(x);
#endif
}
string libpath(string s)
{
/* Must deliver a full pathname to the library file "s" */
register string p;
register int length;
register size_t length;
char* libdir = getenv("LLGEN_LIB_DIR");
if (!libdir)
libdir = LIBDIR;
length = strlen(libdir) + strlen(s) + 2;
p = (string) alloc((unsigned) length);
p = (string) alloc(length);
strcpy(p,libdir);
strcat(p,"/");
strcat(p,s);

View file

@ -33,14 +33,6 @@ STATIC void readgrammar(int, char *[]);
STATIC void doparse(register p_file);
STATIC void comfatal(void);
extern void UNLINK(string);
extern void RENAME(string, string);
extern void TMPNAM(string);
extern string libpath(string);
extern void conflchecks(void);
extern void do_compute(void);
extern void gencode(int);
int main(int argc, register string argv[])
{
register string arg;
@ -210,8 +202,8 @@ int main(int argc, register string argv[])
}
else
gencode(argc);
UNLINK(f_temp);
UNLINK(f_pars);
remove(f_temp);
remove(f_pars);
if (verbose)
{
fprintf(stderr, "number of nonterminals: %d\n", nnonterms);
@ -235,7 +227,7 @@ STATIC void readgrammar(int argc, char *argv[])
/*
* Build the file structure
*/
files = p = (p_file) alloc((unsigned) (argc + 1) * sizeof(t_file));
files = p = (p_file) alloc((argc + 1) * sizeof(t_file));
if (argc-- == 1)
{
finput = stdin;
@ -328,11 +320,11 @@ STATIC void comfatal(void)
if (fact != NULL)
{
fclose(fact);
UNLINK(f_temp);
remove(f_temp);
}
if (fpars != NULL)
fclose(fpars);
UNLINK(f_pars);
remove(f_pars);
exit(EXIT_FAILURE);
}

View file

@ -19,7 +19,6 @@
#include <string.h>
#include <stdio.h>
# include "alloc.h"
# include "types.h"
# include "extern.h"
# include "assert.h"

View file

@ -88,7 +88,7 @@ void save_grammar(FILE *f)
/* Generate some constants in the grammar file */
/* Allocate terms list */
t_list = (struct t_list *) alloc((unsigned) nterms * sizeof(struct t_list));
t_list = (struct t_list *) alloc(nterms * sizeof(struct t_list));
t_list_index = 0;
sub_list = (struct subparse_list *) alloc(nsubstarts * sizeof(struct subparse_list));

View file

@ -63,7 +63,7 @@ p_set get_set(void)
static p_set sets, maxsets;
if ((p = sets) >= maxsets) {
q = p = (p_set) alloc((unsigned) (50*setsize*sizeof(*sets)));
q = p = (p_set) alloc(50*setsize*sizeof(*sets));
maxsets = p + 50 * setsize;
do {
*q++ = 0;
@ -81,7 +81,7 @@ p_set setalloc(void)
register p_set p;
register int size = setsize;
p = (p_set) alloc((unsigned) (size * sizeof(*p))) + size;
p = (p_set) alloc(size * sizeof(*p)) + size;
do {
*--p = 0;
} while (--size);

View file

@ -6,7 +6,6 @@
/* $Id$ */
#ifdef LL_DEBUG
#include <assert.h>
#include <stdio.h>
#define LL_assert(x) assert(x)
#else
#define LL_assert(x) /* nothing */
@ -368,7 +367,7 @@ int input(void)
*/
register int c;
if (c = backupc) {
if ((c = backupc)) {
/* Last char was "unput()". Deliver it again
*/
backupc = 0;

View file

@ -360,7 +360,7 @@ int input(void)
*/
register int c;
if (c = backupc) {
if ((c = backupc)) {
/* Last char was "unput()". Deliver it again
*/
backupc = 0;

View file

@ -18,6 +18,8 @@
* Type and structure definitions
*/
#include <stddef.h> /* size_t */
typedef int *p_set; /* pointer to bitset */
typedef char *p_mem; /* pointer to some core */
typedef char *string;
@ -259,14 +261,19 @@ typedef struct info_alloc {
/*
* Structure used for dynamically growing arrays
*/
unsigned i_size; /* Size of the array */
unsigned i_esize; /* Size of an element */
unsigned i_incr; /* When filled, add room for i_incr elements */
size_t i_size; /* Size of the array */
size_t i_esize; /* Size of an element */
size_t i_incr; /* When filled, add room for i_incr elements */
p_mem i_ptr; /* ptr to base of array */
p_mem i_max; /* ptr to first free */
p_mem i_top; /* ptr to top of array */
} t_info, *p_info;
/* alloc.c */
p_mem alloc(size_t);
p_mem ralloc(p_mem, size_t);
p_mem new_mem(p_info);
# ifdef NDEBUG
# define STATIC static
# else /* not NDEBUG */