ref: 85cdc11b3ad306110ae43fe5b35c04c37cd776fd
parent: dc062d85aca496f752be16b1da3e4747735a8921
author: Ori Bernstein <[email protected]>
date: Sat Dec 24 09:49:21 EST 2011
Fix static analysis warnings.
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -18,7 +18,7 @@
int yylex(void);
Op binop(int toktype);
Stab *curscope;
-int n = 0;
+//int n = 0;
%}
%token<tok> TError
@@ -546,6 +546,8 @@
Op binop(int tt)
{
Op o;
+
+ o = Obad;
switch (tt) {
case TPlus: o = Oadd; break;
case TMinus: o = Osub; break;
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -51,6 +51,7 @@
case Nexpr: t = n->expr.type; break;
case Ndecl: t = decltype(n); break;
default:
+ t = NULL;
die("untypeable %s", nodestr(n->type));
break;
};
--- a/parse/main.c
+++ b/parse/main.c
@@ -12,7 +12,7 @@
#include "parse.h"
Node *file;
-char *outfile;
+static char *outfile;
int debug;
static void usage(char *prog)
@@ -58,7 +58,7 @@
return 0;
}
-void gen()
+void gen(void)
{
printf("GEN!\n");
}
--- a/parse/names.c
+++ b/parse/names.c
@@ -11,25 +11,25 @@
#include "parse.h"
-char *optab[] = {
+static char *optab[] = {
#define O(op) #op,
#include "ops.def"
#undef O
};
-char *nodetab[] = {
+static char *nodetab[] = {
#define N(nt) #nt,
#include "nodes.def"
#undef N
};
-char *littab[] = {
+static char *littab[] = {
#define L(lt) #lt,
#include "lits.def"
#undef L
};
-char *tidtab[] = {
+static char *tidtab[] = {
#define Ty(t) #t,
#include "types.def"
#undef Ty
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -103,12 +103,11 @@
struct Cstr {
int cid; /* unique id */
- /* required members */
- size_t nreqmemb;
- Node **reqmemb;
- /* required functions */
- size_t nreqfn;
- Node **reqfn;
+ char *name;
+ Node **memb; /* type must have these members */
+ size_t nmemb;
+ Node **funcs; /* and declare these funcs */
+ size_t nfuncs;
};
struct Node {
@@ -202,8 +201,10 @@
extern Tok *curtok; /* the last token we tokenized */
extern int line; /* the last line number we tokenized */
extern Node *file; /* the current file we're compiling */
-extern Stab *scope; /* the current scope to insert symbols into */
+extern Stab *curscope; /* the current scope to insert symbols into */
extern Type **typetab; /* type -> type map used by inference. size maintained by type creation code */
+extern int ntypes;
+
extern Type *littypes[]; /* literal type -> type map */
/* data structures */
@@ -252,7 +253,7 @@
Type *mktystruct(int line, Node **decls, size_t ndecls);
Type *mktyunion(int line, Node **decls, size_t ndecls);
Type *mktyenum(int line, Node **decls, size_t ndecls);
-Cstr *mkcstr(int line, char *name, Node **reqmemb, size_t nreqmemb, Node **reqdecl, size_t nreqdecl);
+Cstr *mkcstr(int line, char *name, Node **memb, size_t nmemb, Node **funcs, size_t nfuncs);
/* type manipulation */
int hascstr(Type *t, Cstr *c);
@@ -298,6 +299,7 @@
/* debug */
void dump(Node *t, FILE *fd);
void dumpsym(Sym *s, FILE *fd);
+void dumpstab(Stab *st, FILE *fd);
char *opstr(Op o);
char *nodestr(Ntype nt);
char *litstr(Littype lt);
--- a/parse/tok.c
+++ b/parse/tok.c
@@ -32,12 +32,12 @@
return fbuf[fidx + n];
}
-static int peek()
+static int peek(void)
{
return peekn(0);
}
-static int next()
+static int next(void)
{
int c;
@@ -46,13 +46,13 @@
return c;
}
-void unget()
+static void unget(void)
{
fidx--;
assert(fidx >= 0);
}
-int match(char c)
+static int match(char c)
{
if (peek() == c) {
next();
@@ -62,7 +62,7 @@
}
}
-Tok *mktok(int tt)
+static Tok *mktok(int tt)
{
Tok *t;
@@ -77,7 +77,7 @@
return isalnum(c) || c == '_';
}
-static void eatcomment()
+static void eatcomment(void)
{
int depth;
int startln;
@@ -111,7 +111,7 @@
}
}
-void eatspace()
+static void eatspace(void)
{
int c;
@@ -122,13 +122,13 @@
else if (isspace(c))
next();
else if (c == '/' && peekn(1) == '*')
- eatcomment(c);
+ eatcomment();
else
break;
}
}
-int kwd(char *s)
+static int kwd(char *s)
{
int i;
struct {char* kw; int tt;} kwmap[] = {
@@ -164,7 +164,7 @@
return TIdent;
}
-Tok *kwident()
+static Tok *kwident(void)
{
char buf[1024];
char c;
@@ -182,7 +182,7 @@
return t;
}
-Tok *strlit()
+static Tok *strlit()
{
Tok *t;
int sstart; /* start of string within input buf */
@@ -209,7 +209,7 @@
return t;
}
-Tok *charlit()
+static Tok *charlit()
{
Tok *t;
int sstart; /* start of string within input buf */
@@ -236,7 +236,7 @@
return t;
}
-Tok *oper()
+static Tok *oper(void)
{
int tt;
char c;
@@ -260,10 +260,12 @@
break;
case '.':
if (match('.')) {
- if (match('.'))
+ if (match('.')) {
tt = TEllipsis;
- else
+ } else {
unget();
+ tt = TDot;
+ }
} else {
tt = TDot;
}
@@ -371,7 +373,7 @@
return mktok(tt);
};
-Tok *number(int base)
+static Tok *number(int base)
{
Tok *t;
int start;
@@ -406,7 +408,7 @@
return t;
}
-Tok *numlit()
+static Tok *numlit(void)
{
Tok *t;
@@ -425,7 +427,7 @@
return t;
}
-Tok *toknext()
+static Tok *toknext()
{
Tok *t;
int c;
@@ -477,16 +479,17 @@
if (!fbuf)
die("Out of memory reading %s", file);
nread += n;
- fbuf = realloc(fbuf, nread + 4096);
+ fbuf = xrealloc(fbuf, nread + 4096);
}
fbufsz = nread;
line = 1;
fidx = 0;
+ close(fd);
filename = strdup(file);
}
-int yylex()
+int yylex(void)
{
curtok = toknext();
yylval.tok = curtok;
--- a/parse/type.c
+++ b/parse/type.c
@@ -17,9 +17,13 @@
char *name;
};
-Type *littypes[Nlit] = {0,};
+Type *littypes[Nlit] = {NULL,};
Type **typetab = NULL;
+int ntypes;
+static Cstr **cstrtab;
+static int ncstr;
+
static Typename typenames[] = {
{Tyvoid, "void"},
{Tychar, "char"},
@@ -37,12 +41,6 @@
{Tybad, NULL}
};
-Type **types;
-int ntypes;
-Cstr **cstr;
-int ncstr;
-
-static int nexttid = 0;
Type *mkty(int line, Ty ty)
{
Type *t;
@@ -49,11 +47,28 @@
t = zalloc(sizeof(Type));
t->type = ty;
- t->tid = nexttid++;
- typetab = realloc(typetab, nexttid*sizeof(Type*));
+ t->tid = ntypes++;
+
+ typetab = xrealloc(typetab, ntypes*sizeof(Type*));
return t;
}
+/* steals memb, funcs */
+Cstr *mkcstr(int line, char *name, Node **memb, size_t nmemb, Node **funcs, size_t nfuncs)
+{
+ Cstr *c;
+
+ c = zalloc(sizeof(Cstr));
+ c->name = strdup(name);
+ c->memb = memb;
+ c->nmemb = nmemb;
+ c->funcs = funcs;
+ c->nfuncs = nfuncs;
+ c->cid = ncstr++;
+ cstrtab = xrealloc(cstrtab, ncstr*sizeof(Cstr*));
+ return c;
+}
+
Type *mktyvar(int line)
{
Type *t;
@@ -183,8 +198,13 @@
return len - (end - p);
}
-int tybfmt(char *buf, size_t len, Type *t)
+static int cstrfmt(char *buf, size_t len, Type *t)
{
+ return 0;
+}
+
+static int tybfmt(char *buf, size_t len, Type *t)
+{
char *p;
char *end;
int i;
@@ -270,6 +290,8 @@
break;
}
+ p += cstrfmt(p, end - p, t);
+
return len - (end - p);
}
@@ -290,7 +312,7 @@
#if 0
static Cstr *cstrbuiltins[Ncstr];
#endif
-void tyinit()
+void tyinit(void)
{
#if 0
int i;