Skip to content

Commit

Permalink
Support number##identifier token pasting. Example:
Browse files Browse the repository at this point in the history
#define K_DOUBLE(x) x##l
K_DOUBLE(0.5)
  • Loading branch information
dwilliamson committed Mar 10, 2017
1 parent fcc9fde commit b6f4e10
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/fcpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ void Putstring(struct Global *, char *);
void Putint(struct Global *, int);
char *savestring(struct Global *, char *);
ReturnCode addfile(struct Global *, FILE *, char *);
int catenate(struct Global *, ReturnCode *);
int catenate(struct Global *, int lhs_number, ReturnCode *);
void cerror(struct Global *, ErrorCode, ...);
ReturnCode control(struct Global *, int *);
ReturnCode dodefine(struct Global *);
Expand Down Expand Up @@ -1265,7 +1265,7 @@ ReturnCode cppmain(struct Global *global)
}
if((ret=macroid(global, &c))) /* Grab the token */
return(ret);
} while (type[c] == LET && catenate(global, &ret) && !ret);
} while (type[c] == LET && catenate(global, 0, &ret) && !ret);
if(ret)
/* If the loop was broken because of a fatal error! */
return(ret);
Expand All @@ -1291,6 +1291,9 @@ ReturnCode cppmain(struct Global *global)
ret=scannumber(global, c, (ReturnCode(*)(struct Global *, int))output);
if(ret)
return(ret);
catenate(gloabl, 1, &ret); /* Check to see if the number is the lhs of a macro concat */

This comment has been minimized.

Copy link
@bkaradzic

bkaradzic Mar 11, 2017

Did you build this? gloabl?

if (ret)
return(ret);
break;
case QUO: /* char or string const */
go = 0;
Expand Down Expand Up @@ -3691,7 +3694,7 @@ ReturnCode evallex(struct Global *global,
*op=OP_EOE; /* End of expression */
return(FPP_OK);
}
} while ((t = type[c]) == LET && catenate(global, &ret) && !ret);
} while ((t = type[c]) == LET && catenate(global, 0, &ret) && !ret);
if(ret)
/* If the loop was broken because of a fatal error! */
return(ret);
Expand Down Expand Up @@ -3852,7 +3855,7 @@ ReturnCode dosizeof(struct Global *global, int *result)
}
else if (type[c] != LET) /* Exit if not a type */
break;
else if (!catenate(global, &ret) && !ret) { /* Maybe combine tokens */
else if (!catenate(global, 0, &ret) && !ret) { /* Maybe combine tokens */
/*
* Look for this unexpandable token in basic_types.
* The code accepts "int long" as well as "long int"
Expand Down Expand Up @@ -4368,7 +4371,7 @@ ReturnCode macroid(struct Global *global, int *c)
return(FPP_OK);
}

int catenate(struct Global *global, ReturnCode *ret)
int catenate(struct Global *global, int lhs_number, ReturnCode *ret)
{
/*
* A token was just read (via macroid).
Expand All @@ -4380,7 +4383,7 @@ int catenate(struct Global *global, ReturnCode *ret)

#if OK_CONCAT
int c;
char *token1;
char *token1 = "";
#endif

#if OK_CONCAT
Expand All @@ -4389,7 +4392,8 @@ int catenate(struct Global *global, ReturnCode *ret)
return (FALSE);
}
else {
token1 = savestring(global, global->tokenbuf); /* Save first token */
if (lhs_number == 0) /* The lhs number has already been emit */
token1 = savestring(global, global->tokenbuf); /* Save first token */
c=get(global);
if(global->rightconcat) {
*ret=macroid(global, &c); /* Scan next token */
Expand Down Expand Up @@ -4432,7 +4436,8 @@ int catenate(struct Global *global, ReturnCode *ret)
* new (concatenated) token after freeing token1.
* Finally, setup to read the new token.
*/
free(token1); /* Free up memory */
if (lhs_number == 0)
free(token1); /* Free up memory */
*ret=ungetstring(global, global->work); /* Unget the new thing, */
if(*ret)
return(FALSE);
Expand Down

0 comments on commit b6f4e10

Please sign in to comment.