History

tccpp: alternative #pragma push/pop_macro

using next_nomacro() so that for example #define push_macro foobar does not affect how the pragma works (same behavior as gcc, albeit not MS's cl).

grischka committed 11 years ago

Commit 72e8ff11 parent 7c271862 files · +38 −11Browse files

Unified · Split

2 files
  1. tccpp.c +36 −11
  2. tcctok.h +2 −0
tccpp.c+36−11
@@ -1114,7 +1114,6 @@ ST_FUNC void define_undef(Sym *s) · 1113 unmodified lines
11141114 define_print(s, 1);
11151115 table_ident[v - TOK_IDENT]->sym_define = NULL;
11161116 }
1117− s->v = 0;
11181117 }
11191118
11201119 ST_INLN Sym *define_find(int v)
@@ -1378,9 +1377,7 @@ static inline void add_cached_include(TCCState *s1, const char *filename, int if · 257 unmodified lines
13781377
13791378 static void pragma_parse(TCCState *s1)
13801379 {
1381− int val;
1382−
1383− next();
1380+ next_nomacro();
13841381 if (tok == TOK_pack) {
13851382 /*
13861383 This may be:
@@ -1399,7 +1396,7 @@ static void pragma_parse(TCCState *s1) · 12 unmodified lines
13991396 }
14001397 s1->pack_stack_ptr--;
14011398 } else {
1402− val = 0;
1399+ int val = 0;
14031400 if (tok != ')') {
14041401 if (tok == TOK_ASM_push) {
14051402 next();
@@ -1408,18 +1405,17 @@ static void pragma_parse(TCCState *s1) · 2 unmodified lines
14081405 s1->pack_stack_ptr++;
14091406 skip(',');
14101407 }
1411− if (tok != TOK_CINT) {
1412− pack_error:
1413− tcc_error("invalid pack pragma");
1414− }
1408+ if (tok != TOK_CINT)
1409+ goto pragma_err;
14151410 val = tokc.i;
14161411 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1417− goto pack_error;
1412+ goto pragma_err;
14181413 next();
14191414 }
14201415 *s1->pack_stack_ptr = val;
1421− skip(')');
14221416 }
1417+ if (tok != ')')
1418+ goto pragma_err;
14231419 } else if (tok == TOK_comment) {
14241420 if (s1->ms_extensions) {
14251421 next();
@@ -1446,11 +1442,40 @@ static void pragma_parse(TCCState *s1) · 20 unmodified lines
14461442 } else {
14471443 tcc_warning("#pragma comment(lib) is ignored");
14481444 }
1445+ } else if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1446+ int t = tok, v;
1447+ Sym *s;
1448+
1449+ if (next_nomacro(), tok != '(')
1450+ goto pragma_err;
1451+ if (next_nomacro(), tok != TOK_STR)
1452+ goto pragma_err;
1453+ v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1454+ if (next_nomacro(), tok != ')')
1455+ goto pragma_err;
1456+ if (t == TOK_push_macro) {
1457+ while (NULL == (s = define_find(v)))
1458+ define_push(v, 0, NULL, NULL);
1459+ s->type.ref = s; /* set push boundary */
1460+ } else {
1461+ for (s = define_stack; s; s = s->prev)
1462+ if (s->v == v && s->type.ref == s) {
1463+ s->type.ref = NULL;
1464+ break;
1465+ }
1466+ }
1467+ if (s)
1468+ table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1469+ else
1470+ tcc_warning("unbalanced #pragma pop_macro");
14491471 } else if (tok == TOK_once) {
14501472 add_cached_include(s1, file->filename, TOK_once);
14511473 } else {
14521474 tcc_warning("unknown #pragma %s", get_tok_str(tok, &tokc));
14531475 }
1476+ return;
1477+pragma_err:
1478+ tcc_error("malformed #pragma directive");
14541479 }
14551480
14561481 /* is_bof is true if first non space token at beginning of file */
tcctok.h+2−0
@@ -155,6 +155,8 @@ · 154 unmodified lines
155155 #endif
156156 DEF(TOK_comment, "comment")
157157 DEF(TOK_lib, "lib")
158+ DEF(TOK_push_macro, "push_macro")
159+ DEF(TOK_pop_macro, "pop_macro")
158160 DEF(TOK_once, "once")
159161
160162 /* builtin functions or variables */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t