scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit b6847c53e54893f2f3808ff0582cb675c0497528
parent 2a872329eddbee463a4150cfb6082feacb0761c0
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 26 May 2015 09:30:09 +0200

Add predefined macros

C standard defines some predefined macros that are always
present in any implementation of the language. This version
of the compiler support:
	- __STDC__
	- __DATE__
	- __TIME__
	- __STDC_HOSTED__
	- __STDC_VERSION__
	- __LINE__
	- __FILE__

At this moment __STDC__ is 1, even when we are not 100%
compatible with the standard (and we don't want to be),
but in the majority of cases the small differencies are
not signigficatives, so it is more or less safe to
declare it. In the same way, __STDC_VERSION__ is set
to 199409L, that is the value for C90 with 94
ammendents.

Diffstat:
cc1/cc1.h | 2++
cc1/cpp.c | 49++++++++++++++++++++++++++++++++++++++++++++++++-
cc1/main.c | 3+--
3 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -256,6 +256,7 @@ extern Symbol *lookup(unsigned ns); extern Symbol *install(unsigned ns); extern Symbol *newsym(unsigned ns); extern void pushctx(void), popctx(void); +extern void ikeywords(void); /* stmt.c */ extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch); @@ -277,6 +278,7 @@ extern void setfline(unsigned short line); extern char *addinput(char *fname, Symbol *sym); extern void setnamespace(int ns); extern void setsafe(int type); +extern void initcpp(void); #define accept(t) ((yytoken == (t)) ? next() : 0) /* code.c */ diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -5,6 +5,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include "../inc/sizes.h" #include "../inc/cc.h" @@ -16,9 +17,42 @@ static char *argp; static unsigned arglen; static unsigned numif; static Symbol *lastmacro; +static Symbol *symline, *symfile; unsigned char ifstatus[NR_COND]; +static Symbol * +defmacro(char *s) +{ + Symbol *sym; + + strcpy(yytext, s); + sym = lookup(NS_CPP); + sym->flags |= ISDEFINED; + return sym; +} + +void +initcpp(void) +{ + static char sdate[17], stime[14]; + struct tm *tm; + time_t t; + + t = time(NULL); + tm = localtime(&t); + strftime(sdate, sizeof(sdate), "-1#\"%b %d %Y\"", tm); + strftime(stime, sizeof(stime), "-1#\"%H:%M:%S\"", tm); + + defmacro("__STDC__")->u.s = "-1#1"; + defmacro("__DATE__")->u.s = sdate; + defmacro("__TIME__")->u.s = stime; + defmacro("__STDC_HOSTED__")->u.s = "-1#1"; + defmacro("__STDC_VERSION__")->u.s = "-1#199409L"; + symline = defmacro("__LINE__"); + symfile = defmacro("__FILE__"); +} + static bool iden(char **str) { @@ -159,13 +193,25 @@ parsepars(char *buffer, char **listp, int nargs) * is the macro definition, where @dd@ indicates the * parameter number dd */ +#define BUFSIZE ((INPUTSIZ > FILENAME_MAX+5) ? INPUTSIZ : FILENAME_MAX+5) bool expand(Symbol *sym) { unsigned len; - char *arglist[NR_MACROARG], buffer[INPUTSIZ]; + char *arglist[NR_MACROARG], buffer[BUFSIZE]; char c, *bp, *arg, *s = sym->u.s; + if (sym == symfile) { + sprintf(buffer, "-1#\"%s\"", getfname()); + strcpy(addinput(NULL, symfile), buffer); + return 0; + } + if (sym == symline) { + sprintf(buffer, "-1#%d", getfline()); + strcpy(addinput(NULL, symline), buffer); + return 0; + } + lastmacro = sym; if (!parsepars(buffer, arglist, atoi(s))) return 0; @@ -196,6 +242,7 @@ expand(Symbol *sym) expansion_too_long: error("expansion of macro \"%s\" is too long", lastmacro->name); } +#undef BUFSIZE /* * Parse an argument list (par0, par1, ...) and creates diff --git a/cc1/main.c b/cc1/main.c @@ -9,8 +9,6 @@ #include "../inc/cc.h" #include "cc1.h" -extern void ikeywords(void), lexfile(char *file); - int warnings; jmp_buf recover; @@ -65,6 +63,7 @@ main(int argc, char *argv[]) if (argc > 1) usage(); + initcpp(); ikeywords(); if (!addinput(*argv, NULL))