comparison lwcc/preproc.c @ 307:9e342c4e4b66 ccdev

Added support for __LINE__, __FILE__, __DATE__, and __TIME__ Added support for the standard __LINE__, __FILE__, __DATE__, and __TIME "macros" and updated the readme form lwcc to reflect the current development state. Note: attempts to define these four macros will succeed but the definitions will be ignored.
author William Astle <lost@l-w.ca>
date Sat, 21 Sep 2013 12:27:48 -0600
parents b08787e5b9f3
children 670ea8f90212
comparison
equal deleted inserted replaced
306:b08787e5b9f3 307:9e342c4e4b66
20 */ 20 */
21 21
22 #include <stdio.h> 22 #include <stdio.h>
23 #include <stdlib.h> 23 #include <stdlib.h>
24 #include <string.h> 24 #include <string.h>
25 #include <time.h>
25 #include <unistd.h> 26 #include <unistd.h>
26 27
27 #include <lw_alloc.h> 28 #include <lw_alloc.h>
28 #include <lw_string.h> 29 #include <lw_string.h>
29 30
1332 int pcount; 1333 int pcount;
1333 char *tstr; 1334 char *tstr;
1334 struct token_list *expand_list; 1335 struct token_list *expand_list;
1335 int repl; 1336 int repl;
1336 struct token_list *rtl; 1337 struct token_list *rtl;
1337 1338
1339 // check for built in macros
1340 if (strcmp(mname, "__FILE__") == 0)
1341 {
1342 struct strbuf *sb;
1343
1344 sb = strbuf_new();
1345 strbuf_add(sb, '"');
1346 for (tstr = (char *)(pp -> fn); *tstr; tstr++)
1347 {
1348 if (*tstr == 32 || (*tstr > 34 && *tstr < 127))
1349 {
1350 strbuf_add(sb, *tstr);
1351 }
1352 else
1353 {
1354 strbuf_add(sb, '\\');
1355 strbuf_add(sb, (*tstr >> 6) + '0');
1356 strbuf_add(sb, ((*tstr >> 3) & 7) + '0');
1357 strbuf_add(sb, (*tstr & 7) + '0');
1358 }
1359 }
1360 strbuf_add(sb, '"');
1361 tstr = strbuf_end(sb);
1362 preproc_unget_token(pp, token_create(TOK_STR_LIT, tstr, pp -> lineno, pp -> column, pp -> fn));
1363 lw_free(tstr);
1364 return 1;
1365 }
1366 else if (strcmp(mname, "__LINE__") == 0)
1367 {
1368 char nbuf[25];
1369 snprintf(nbuf, 25, "%d", pp -> lineno);
1370 preproc_unget_token(pp, token_create(TOK_NUMBER, nbuf, pp -> lineno, pp -> column, pp -> fn));
1371 return 1;
1372 }
1373 else if (strcmp(mname, "__DATE__") == 0)
1374 {
1375 char dbuf[14];
1376 struct tm *tv;
1377 time_t tm;
1378 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1379
1380 tm = time(NULL);
1381 tv = localtime(&tm);
1382 snprintf(dbuf, 14, "\"%s %2d %04d\"", months[tv -> tm_mon], tv -> tm_mday, tv -> tm_year + 1900);
1383 preproc_unget_token(pp, token_create(TOK_STR_LIT, dbuf, pp -> lineno, pp -> column, pp -> fn));
1384 return 1;
1385 }
1386 else if (strcmp(mname, "__TIME__") == 0)
1387 {
1388 char tbuf[11];
1389 struct tm *tv;
1390 time_t tm;
1391
1392 tm = time(NULL);
1393 tv = localtime(&tm);
1394 snprintf(tbuf, 11, "\"%02d:%02d:%02d\"", tv -> tm_hour, tv -> tm_min, tv -> tm_sec);
1395 preproc_unget_token(pp, token_create(TOK_STR_LIT, tbuf, pp -> lineno, pp -> column, pp -> fn));
1396 return 1;
1397 }
1398
1338 s = symtab_find(pp, mname); 1399 s = symtab_find(pp, mname);
1339 if (!s) 1400 if (!s)
1340 return 0; 1401 return 0;
1341 1402
1342 for (e = pp -> expand_list; e; e = e -> next) 1403 for (e = pp -> expand_list; e; e = e -> next)