Correctly handle nested #ifs

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-11-16 22:02:38 +01:00
parent 5b91c2af5e
commit b8a5c87360

25
ftepp.c
View file

@ -58,6 +58,7 @@ typedef struct {
bool newline; bool newline;
unsigned int errors; unsigned int errors;
bool output_on;
ppcondition *conditions; ppcondition *conditions;
ppmacro **macros; ppmacro **macros;
} ftepp_t; } ftepp_t;
@ -133,6 +134,8 @@ static ftepp_t* ftepp_init()
ftepp = (ftepp_t*)mem_a(sizeof(*ftepp)); ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
memset(ftepp, 0, sizeof(*ftepp)); memset(ftepp, 0, sizeof(*ftepp));
ftepp->output_on = true;
return ftepp; return ftepp;
} }
@ -149,15 +152,21 @@ static void ftepp_delete(ftepp_t *self)
static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond) static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
{ {
if (ignore_cond || if (ignore_cond || ftepp->output_on)
!vec_size(ftepp->conditions) ||
vec_last(ftepp->conditions).on)
{ {
printf("%s", str); printf("%s", str);
} }
} }
ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name) static void ftepp_update_output_condition(ftepp_t *ftepp)
{
size_t i;
ftepp->output_on = true;
for (i = 0; i < vec_size(ftepp->conditions); ++i)
ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
}
static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
{ {
size_t i; size_t i;
for (i = 0; i < vec_size(ftepp->macros); ++i) { for (i = 0; i < vec_size(ftepp->macros); ++i) {
@ -643,6 +652,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
return false; return false;
cond.was_on = cond.on; cond.was_on = cond.on;
vec_push(ftepp->conditions, cond); vec_push(ftepp->conditions, cond);
ftepp->output_on = ftepp->output_on && cond.on;
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) { else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
@ -651,6 +661,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
cond.on = !cond.on; cond.on = !cond.on;
cond.was_on = cond.on; cond.was_on = cond.on;
vec_push(ftepp->conditions, cond); vec_push(ftepp->conditions, cond);
ftepp->output_on = ftepp->output_on && cond.on;
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) { else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
@ -661,6 +672,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
pc = &vec_last(ftepp->conditions); pc = &vec_last(ftepp->conditions);
pc->on = !pc->was_on && cond.on; pc->on = !pc->was_on && cond.on;
pc->was_on = pc->was_on || pc->on; pc->was_on = pc->was_on || pc->on;
ftepp_update_output_condition(ftepp);
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) { else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
@ -672,6 +684,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
pc = &vec_last(ftepp->conditions); pc = &vec_last(ftepp->conditions);
pc->on = !pc->was_on && cond.on; pc->on = !pc->was_on && cond.on;
pc->was_on = pc->was_on || pc->on; pc->was_on = pc->was_on || pc->on;
ftepp_update_output_condition(ftepp);
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "elif")) { else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
@ -682,6 +695,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
pc = &vec_last(ftepp->conditions); pc = &vec_last(ftepp->conditions);
pc->on = !pc->was_on && cond.on; pc->on = !pc->was_on && cond.on;
pc->was_on = pc->was_on || pc->on; pc->was_on = pc->was_on || pc->on;
ftepp_update_output_condition(ftepp);
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "if")) { else if (!strcmp(ftepp_tokval(ftepp), "if")) {
@ -689,6 +703,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
return false; return false;
cond.was_on = cond.on; cond.was_on = cond.on;
vec_push(ftepp->conditions, cond); vec_push(ftepp->conditions, cond);
ftepp->output_on = ftepp->output_on && cond.on;
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "else")) { else if (!strcmp(ftepp_tokval(ftepp), "else")) {
@ -698,6 +713,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
pc->on = !pc->was_on; pc->on = !pc->was_on;
pc->had_else = true; pc->had_else = true;
ftepp_next(ftepp); ftepp_next(ftepp);
ftepp_update_output_condition(ftepp);
break; break;
} }
else if (!strcmp(ftepp_tokval(ftepp), "endif")) { else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
@ -707,6 +723,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
} }
vec_pop(ftepp->conditions); vec_pop(ftepp->conditions);
ftepp_next(ftepp); ftepp_next(ftepp);
ftepp_update_output_condition(ftepp);
break; break;
} }
else { else {