Lexer should keep newlines in merged lines, so will the preprocessor, but therefore the lexer will replace comments with actual spaces so we don't get borken output... also don't error about redifining a macro when inside a non-outputting #if branch

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-11-16 23:13:53 +01:00
parent 990450bfe0
commit 515cafe8bd
2 changed files with 35 additions and 13 deletions

31
ftepp.c
View file

@ -114,10 +114,13 @@ static pptoken *pptoken_make(ftepp_t *ftepp)
{
pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
token->token = ftepp->token;
#if 0
if (token->token == TOKEN_WHITE)
token->value = util_strdup(" ");
else
#else
token->value = util_strdup(ftepp_tokval(ftepp));
#endif
memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
return token;
}
@ -312,7 +315,7 @@ static bool ftepp_define(ftepp_t *ftepp)
case TOKEN_TYPENAME:
case TOKEN_KEYWORD:
macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
if (macro) {
if (macro && ftepp->output_on) {
if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
return false;
ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
@ -338,7 +341,11 @@ static bool ftepp_define(ftepp_t *ftepp)
if (!ftepp_define_body(ftepp, macro))
return false;
vec_push(ftepp->macros, macro);
if (ftepp->output_on)
vec_push(ftepp->macros, macro);
else {
ppmacro_delete(macro);
}
return true;
}
@ -767,15 +774,17 @@ static bool ftepp_undef(ftepp_t *ftepp)
if (!ftepp_skipspace(ftepp))
return false;
switch (ftepp->token) {
case TOKEN_IDENT:
case TOKEN_TYPENAME:
case TOKEN_KEYWORD:
ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
break;
default:
ftepp_error(ftepp, "expected macro name");
return false;
if (ftepp->output_on) {
switch (ftepp->token) {
case TOKEN_IDENT:
case TOKEN_TYPENAME:
case TOKEN_KEYWORD:
ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
break;
default:
ftepp_error(ftepp, "expected macro name");
return false;
}
}
(void)ftepp_next(ftepp);

17
lexer.c
View file

@ -412,13 +412,17 @@ static int lex_skipwhite(lex_file *lex)
if (lex->flags.preprocessing) {
haswhite = true;
/*
lex_tokench(lex, '/');
lex_tokench(lex, '/');
*/
lex_tokench(lex, ' ');
lex_tokench(lex, ' ');
}
while (ch != EOF && ch != '\n') {
if (lex->flags.preprocessing)
lex_tokench(lex, ch);
lex_tokench(lex, ' '); /* ch); */
ch = lex_getch(lex);
}
if (lex->flags.preprocessing) {
@ -433,8 +437,12 @@ static int lex_skipwhite(lex_file *lex)
/* multiline comment */
if (lex->flags.preprocessing) {
haswhite = true;
/*
lex_tokench(lex, '/');
lex_tokench(lex, '*');
*/
lex_tokench(lex, ' ');
lex_tokench(lex, ' ');
}
while (ch != EOF)
@ -444,14 +452,18 @@ static int lex_skipwhite(lex_file *lex)
ch = lex_getch(lex);
if (ch == '/') {
if (lex->flags.preprocessing) {
/*
lex_tokench(lex, '*');
lex_tokench(lex, '/');
*/
lex_tokench(lex, ' ');
lex_tokench(lex, ' ');
}
break;
}
}
if (lex->flags.preprocessing) {
lex_tokench(lex, ch);
lex_tokench(lex, ' '); /* ch); */
}
}
ch = ' '; /* cause TRUE in the isspace check */
@ -686,6 +698,7 @@ int lex_do(lex_file *lex)
break;
}
/* we reached a linemerge */
lex_tokench(lex, '\n');
continue;
}