ref: b6847c53e54893f2f3808ff0582cb675c0497528
parent: 2a872329eddbee463a4150cfb6082feacb0761c0
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue May 26 05:30:09 EDT 2015
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.
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -256,6 +256,7 @@
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 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 */
--- 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 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 @@
* 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 @@
expansion_too_long:
error("expansion of macro \"%s\" is too long", lastmacro->name);
}
+#undef BUFSIZE
/*
* Parse an argument list (par0, par1, ...) and creates
--- 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 @@
if (argc > 1)
usage();
+ initcpp();
ikeywords();
if (!addinput(*argv, NULL))