Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated cpp #26

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions cpp/cpp.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ process(Tokenrow *trp)
trp->tp += 1;
control(trp);
} else if (!skipping && anymacros)
expandrow(trp, NULL);
expandrow(trp, NULL, Notinmacro);
if (skipping)
setempty(trp);
puttokens(trp);
Expand Down Expand Up @@ -207,12 +207,17 @@ control(Tokenrow *trp)

case KERROR:
trp->tp = tp+1;
error(WARNING, "#error directive: %r", trp);
error(ERROR, "#error directive: %r", trp);
break;

case KWARNING:
trp->tp = tp+1;
error(WARNING, "#warning directive: %r", trp);
break;

case KLINE:
trp->tp = tp+1;
expandrow(trp, "<line>");
expandrow(trp, "<line>", Notinmacro);
tp = trp->bp+2;
kline:
if (tp+1>=trp->lp || tp->type!=NUMBER || tp+3<trp->lp
Expand Down Expand Up @@ -249,6 +254,16 @@ control(Tokenrow *trp)
return;
}

void *
dorealloc(void *ptr, int size)
{
void *p = realloc(ptr, size);

if (p==NULL)
error(FATAL, "Out of memory from realloc");
return p;
}

void *
domalloc(int size)
{
Expand Down
12 changes: 8 additions & 4 deletions cpp/cpp.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ enum toktype { END, UNCLASS, NAME, NUMBER, STRING, CCON, NL, WS, DSHARP,
DSHARP1, NAME1, DEFINED, UMINUS };

enum kwtype { KIF, KIFDEF, KIFNDEF, KELIF, KELSE, KENDIF, KINCLUDE, KDEFINE,
KUNDEF, KLINE, KERROR, KPRAGMA, KDEFINED,
KUNDEF, KLINE, KERROR, KWARNING, KPRAGMA, KDEFINED,
KLINENO, KFILE, KDATE, KTIME, KSTDC, KEVAL };

#define ISDEFINED 01 /* has #defined value */
#define ISKW 02 /* is PP keyword */
#define ISUNCHANGE 04 /* can't be #defined in PP */
#define ISMAC 010 /* builtin macro, e.g. __LINE__ */
#define ISVARMAC 020 /* variadic macro */

#define EOB 0xFE /* sentinel for end of input buffer */
#define EOFC 0xFD /* sentinel for end of input file */
#define XPWS 1 /* token flag: white space to assure token sep. */

enum { Notinmacro, Inmacro };

typedef struct token {
unsigned char type;
unsigned char flag;
Expand Down Expand Up @@ -98,6 +101,7 @@ Source *setsource(char *, FILE *, char *);
void unsetsource(void);
void puttokens(Tokenrow *);
void process(Tokenrow *);
void *dorealloc(void *, int);
void *domalloc(int);
void dofree(void *);
void error(enum errtype, char *, ...);
Expand All @@ -111,11 +115,11 @@ void dodefine(Tokenrow *);
void doadefine(Tokenrow *, int);
void doinclude(Tokenrow *);
void doif(Tokenrow *, enum kwtype);
void expand(Tokenrow *, Nlist *);
void expand(Tokenrow *, Nlist *, int);
void builtin(Tokenrow *, int);
int gatherargs(Tokenrow *, Tokenrow **, int *);
int gatherargs(Tokenrow *, Tokenrow **, int, int *);
void substargs(Nlist *, Tokenrow *, Tokenrow **);
void expandrow(Tokenrow *, char *);
void expandrow(Tokenrow *, char *, int);
void maketokenrow(int, Tokenrow *);
Tokenrow *copytokenrow(Tokenrow *, Tokenrow *);
Token *growtokenrow(Tokenrow *);
Expand Down
25 changes: 13 additions & 12 deletions cpp/eval.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ eval(Tokenrow *trp, int kw)
}
ntok = trp->tp - trp->bp;
kwdefined->val = KDEFINED; /* activate special meaning of defined */
expandrow(trp, "<if>");
expandrow(trp, "<if>", Notinmacro);
kwdefined->val = NAME;
vp = vals;
op = ops;
Expand Down Expand Up @@ -159,7 +159,7 @@ eval(Tokenrow *trp, int kw)
if (tp->type==MINUS)
*op++ = UMINUS;
if (tp->type==STAR || tp->type==AND) {
error(ERROR, "Illegal operator * or & in #if/#elsif");
error(ERROR, "Illegal operator * or & in #if/#elif");
return 0;
}
continue;
Expand Down Expand Up @@ -197,7 +197,7 @@ eval(Tokenrow *trp, int kw)
continue;

default:
error(ERROR,"Bad operator (%t) in #if/#elsif", tp);
error(ERROR,"Bad operator (%t) in #if/#elif", tp);
return 0;
}
}
Expand All @@ -206,14 +206,14 @@ eval(Tokenrow *trp, int kw)
if (evalop(priority[END])!=0)
return 0;
if (op!=&ops[1] || vp!=&vals[1]) {
error(ERROR, "Botch in #if/#elsif");
error(ERROR, "Botch in #if/#elif");
return 0;
}
if (vals[0].type==UND)
error(ERROR, "Undefined expression value");
return vals[0].val;
syntax:
error(ERROR, "Syntax error in #if/#elsif");
error(ERROR, "Syntax error in #if/#elif");
return 0;
}

Expand Down Expand Up @@ -395,7 +395,7 @@ tokval(Token *tp)
{
struct value v;
Nlist *np;
int i, base, c;
int i, base, c, longcc;
unsigned long n;
uchar *p;

Expand Down Expand Up @@ -441,10 +441,10 @@ tokval(Token *tp)
if (*p=='u' || *p=='U')
v.type = UNS;
else if (*p=='l' || *p=='L')
;
{}
else {
error(ERROR,
"Bad number %t in #if/#elsif", tp);
"Bad number %t in #if/#elif", tp);
break;
}
}
Expand All @@ -455,9 +455,10 @@ tokval(Token *tp)
case CCON:
n = 0;
p = tp->t;
longcc = 0;
if (*p=='L') {
p += 1;
error(WARNING, "Wide char constant value undefined");
longcc = 1;
}
p += 1;
if (*p=='\\') {
Expand All @@ -484,7 +485,7 @@ tokval(Token *tp)
}
} else {
static char cvcon[]
= "b\bf\fn\nr\rt\tv\v''\"\"??\\\\";
= "a\ab\bf\fn\nr\rt\tv\v''\"\"??\\\\";
for (i=0; i<sizeof(cvcon); i+=2) {
if (*p == cvcon[i]) {
n = cvcon[i+1];
Expand All @@ -502,13 +503,13 @@ tokval(Token *tp)
n = *p++;
if (*p!='\'')
error(WARNING, "Multibyte character constant undefined");
else if (n>127)
else if (n>127 && longcc==0)
error(WARNING, "Character constant taken as not signed");
v.val = n;
break;

case STRING:
error(ERROR, "String in #if/#elsif");
error(ERROR, "String in #if/#elif");
break;
}
return v;
Expand Down
Empty file modified cpp/getopt.c
100755 → 100644
Empty file.
4 changes: 1 addition & 3 deletions cpp/hideset.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ newhideset(int hs, Nlist *np)
return hs;
if (nhidesets >= maxhidesets) {
maxhidesets = 3*maxhidesets/2+1;
hidesets = (Hideset *)realloc(hidesets, (sizeof (Hideset *))*maxhidesets);
if (hidesets == NULL)
error(FATAL, "Out of memory from realloc");
hidesets = (Hideset *)dorealloc(hidesets, (sizeof (Hideset *))*maxhidesets);
}
hs1 = (Hideset)domalloc(len*sizeof *hs1);
memmove(hs1, nhs, len*sizeof *hs1);
Expand Down
2 changes: 1 addition & 1 deletion cpp/include.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ doinclude(Tokenrow *trp)
goto syntax;
if (trp->tp->type!=STRING && trp->tp->type!=LT) {
len = trp->tp - trp->bp;
expandrow(trp, "<include>");
expandrow(trp, "<include>", Notinmacro);
trp->tp = trp->bp+len;
}
if (trp->tp->type==STRING) {
Expand Down
Empty file modified cpp/lex.c
100755 → 100644
Empty file.
40 changes: 29 additions & 11 deletions cpp/macro.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ dodefine(Tokenrow *trp)
Token *tp;
Nlist *np;
Tokenrow *def, *args;
int dots;

dots = 0;
tp = trp->tp+1;
if (tp>=trp->lp || tp->type!=NAME) {
error(ERROR, "#defined token is not a name");
Expand All @@ -36,7 +38,9 @@ dodefine(Tokenrow *trp)
int err = 0;
for (;;) {
Token *atp;
if (tp->type!=NAME) {
if (tp->type == ELLIPS)
dots++;
else if (tp->type!=NAME) {
err++;
break;
}
Expand All @@ -51,6 +55,8 @@ dodefine(Tokenrow *trp)
tp += 1;
if (tp->type==RP)
break;
if (dots)
error(ERROR, "arguments after '...' in macro");
if (tp->type!=COMMA) {
err++;
break;
Expand Down Expand Up @@ -83,6 +89,8 @@ dodefine(Tokenrow *trp)
np->ap = args;
np->vp = def;
np->flag |= ISDEFINED;
if(dots)
np->flag |= ISVARMAC;
}

/*
Expand Down Expand Up @@ -130,7 +138,7 @@ doadefine(Tokenrow *trp, int type)
* Flag is NULL if more input can be gathered.
*/
void
expandrow(Tokenrow *trp, char *flag)
expandrow(Tokenrow *trp, char *flag, int inmacro)
{
Token *tp;
Nlist *np;
Expand Down Expand Up @@ -162,7 +170,7 @@ expandrow(Tokenrow *trp, char *flag)
if (np->flag&ISMAC)
builtin(trp, np->val);
else {
expand(trp, np);
expand(trp, np, inmacro);
}
tp = trp->tp;
}
Expand All @@ -176,7 +184,7 @@ expandrow(Tokenrow *trp, char *flag)
* (ordinarily the beginning of the expansion)
*/
void
expand(Tokenrow *trp, Nlist *np)
expand(Tokenrow *trp, Nlist *np, int inmacro)
{
Tokenrow ntr;
int ntokc, narg, i;
Expand All @@ -188,8 +196,9 @@ expand(Tokenrow *trp, Nlist *np)
if (np->ap==NULL) /* parameterless */
ntokc = 1;
else {
ntokc = gatherargs(trp, atr, &narg);
ntokc = gatherargs(trp, atr, (np->flag&ISVARMAC) ? rowlen(np->ap) : 0, &narg);
if (narg<0) { /* not actually a call (no '(') */
/* error(WARNING, "%d %r\n", narg, trp); */
/* gatherargs has already pushed trp->tr to the next token */
return;
}
Expand All @@ -205,7 +214,8 @@ expand(Tokenrow *trp, Nlist *np)
dofree(atr[i]);
}
}
doconcat(&ntr); /* execute ## operators */
if(!inmacro)
doconcat(&ntr); /* execute ## operators */
hs = newhideset(trp->tp->hideset, np);
for (tp=ntr.bp; tp<ntr.lp; tp++) { /* distribute hidesets */
if (tp->type==NAME) {
Expand All @@ -228,7 +238,7 @@ expand(Tokenrow *trp, Nlist *np)
* trp->tp is not changed relative to the tokenrow.
*/
int
gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
gatherargs(Tokenrow *trp, Tokenrow **atr, int dots, int *narg)
{
int parens = 1;
int ntok = 0;
Expand All @@ -245,8 +255,10 @@ gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
if (trp->tp >= trp->lp) {
gettokens(trp, 0);
if ((trp->lp-1)->type==END) {
/* error(WARNING, "reach END\n"); */
trp->lp -= 1;
trp->tp -= ntok;
if (*narg>=0)
trp->tp -= ntok;
return ntok;
}
}
Expand Down Expand Up @@ -301,7 +313,9 @@ gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
parens--;
if (lp->type==DSHARP)
lp->type = DSHARP1; /* ## not special in arg */
if (lp->type==COMMA && parens==0 || parens<0 && (lp-1)->type!=LP) {
if ((lp->type==COMMA && parens==0) || (parens<0 && (lp-1)->type!=LP)) {
if (lp->type == COMMA && dots && *narg == dots-1)
continue;
if (*narg>=NARG-1)
error(FATAL, "Sorry, too many macro arguments");
ttr.bp = ttr.tp = bp;
Expand Down Expand Up @@ -339,12 +353,14 @@ substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
}
if (rtr->tp->type==NAME
&& (argno = lookuparg(np, rtr->tp)) >= 0) {
if (rtr->tp < rtr->bp)
error(ERROR, "access out of bounds");
if ((rtr->tp+1)<rtr->lp && (rtr->tp+1)->type==DSHARP /* don't look beyond end */
|| rtr->tp!=rtr->bp && (rtr->tp-1)->type==DSHARP) /* don't look before beginning */
insertrow(rtr, 1, atr[argno]);
else {
copytokenrow(&tatr, atr[argno]);
expandrow(&tatr, "<macro>");
expandrow(&tatr, "<macro>", Inmacro);
insertrow(rtr, 1, &tatr);
dofree(tatr.bp);
}
Expand Down Expand Up @@ -407,6 +423,8 @@ lookuparg(Nlist *mac, Token *tp)

if (tp->type!=NAME || mac->ap==NULL)
return -1;
if((mac->flag & ISVARMAC) && strcmp((char*)tp->t, "__VA_ARGS__") == 0)
return rowlen(mac->ap) - 1;
for (ap=mac->ap->bp; ap<mac->ap->lp; ap++) {
if (ap->len==tp->len && strncmp((char*)ap->t,(char*)tp->t,ap->len)==0)
return ap - mac->ap->bp;
Expand Down Expand Up @@ -435,7 +453,7 @@ stringify(Tokenrow *vp)
error(ERROR, "Stringified macro arg is too long");
break;
}
if (tp->wslen && (tp->flag&XPWS)==0)
if (tp->wslen /* && (tp->flag&XPWS)==0 */)
*sp++ = ' ';
for (i=0, cp=tp->t; i<tp->len; i++) {
if (instring && (*cp=='"' || *cp=='\\'))
Expand Down
1 change: 1 addition & 0 deletions cpp/nlist.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct kwtab {
"undef", KUNDEF, ISKW,
"line", KLINE, ISKW,
"error", KERROR, ISKW,
"warning", KWARNING, ISKW, // extension to ANSI
"pragma", KPRAGMA, ISKW,
"eval", KEVAL, ISKW,
"defined", KDEFINED, ISDEFINED+ISUNCHANGE,
Expand Down
4 changes: 1 addition & 3 deletions cpp/tokens.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ growtokenrow(Tokenrow *trp)
int nlast = trp->lp - trp->bp;

trp->max = 3*trp->max/2 + 1;
trp->bp = (Token *)realloc(trp->bp, trp->max*sizeof(Token));
if (trp->bp == NULL)
error(FATAL, "Out of memory from realloc");
trp->bp = (Token *)dorealloc(trp->bp, trp->max*sizeof(Token));
trp->lp = &trp->bp[nlast];
trp->tp = &trp->bp[ncur];
return trp->lp;
Expand Down
Empty file modified cpp/unix.c
100755 → 100644
Empty file.