simplify -C printing

parse_print_line_comment() and parse_print_comment() are
    combined and made more simply:
        * don't worry about speed with -E option
        * don't handle straya in comments
            Do we need to handle strays in regular
            parse_line_comment() and
            parse_comment() ?
This commit is contained in:
seyko
2016-04-17 10:07:55 +03:00
parent e010b1396b
commit 587aacedf3

131
tccpp.c
View File

@ -542,134 +542,51 @@ static void pp_line(TCCState *s1, BufferedFile *f, int level)
f->line_ref = f->line_num; f->line_ref = f->line_num;
} }
static uint8_t *parse_print_line_comment(uint8_t *p) static uint8_t *parse_print_comment(uint8_t *p, int is_line_comment)
{ {
int c; int c, star_count;
FILE *ppfp = tcc_state->ppfp;
if ((file->line_num - file->line_ref) > 0) { if ((file->line_num - file->line_ref) > 0) {
fputc('\n', tcc_state->ppfp); fputc('\n', ppfp);
file->line_ref++; file->line_ref++;
pp_line(tcc_state, file, 0); pp_line(tcc_state, file, 0);
} }
fputs("// ", tcc_state->ppfp); if (is_line_comment)
fputs("//", ppfp); else
fputs("/*", ppfp);
star_count = 0;
for(;;) { for(;;) {
c = *p; c = *p;
redo:
if (c == '\n' || c == CH_EOF)
break;
fputc(c, tcc_state->ppfp);
if (c == '\\') { if (c == '\\') {
file->buf_ptr = p; file->buf_ptr = p;
c = handle_eob(); c = handle_eob();
p = file->buf_ptr; p = file->buf_ptr;
if (c == '\\') {
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
PEEKC_EOB(c, p);
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
PEEKC_EOB(c, p);
} }
} if (c == CH_EOF) {
} else { if (is_line_comment) break;
goto redo;
}
} else {
p++;
}
}
return p;
}
static uint8_t *parse_print_comment(uint8_t *p)
{
int c;
if ((file->line_num - file->line_ref) > 0) {
fputc('\n', tcc_state->ppfp);
file->line_ref++;
pp_line(tcc_state, file, 0);
}
fputs("/*", tcc_state->ppfp);
for(;;) {
/* fast skip loop */
for(;;) {
c = *p;
if (c == '\n' || c == '*' || c == '\\')
break;
fputc(c, tcc_state->ppfp);
p++;
}
/* now we can handle all the cases */
if (c == '\n') {
fputc(c, tcc_state->ppfp);
file->line_num++;
p++;
} else if (c == '*') {
fputc(c, tcc_state->ppfp);
p++;
for(;;) {
c = *p;
if (c == '*') {
fputc(c, tcc_state->ppfp);
p++;
} else if (c == '/') {
fputc(c, tcc_state->ppfp);
goto end_of_comment;
} else if (c == '\\') {
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == CH_EOF)
tcc_error("unexpected end of file in comment"); tcc_error("unexpected end of file in comment");
if (c == '\\') {
/* skip '\[\r]\n', otherwise just skip the stray */
while (c == '\\') {
fputc(c, tcc_state->ppfp);
PEEKC_EOB(c, p);
if (c == '\n') {
fputc(c, tcc_state->ppfp);
file->line_num++;
PEEKC_EOB(c, p);
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c == '\n') {
fputc(c, tcc_state->ppfp);
file->line_num++;
PEEKC_EOB(c, p);
} }
} else { if (c == '*')
goto after_star; star_count++;
} else {
} if ((c == '/') && star_count && !is_line_comment)
}
} else {
break; break;
star_count = 0;
if (c == '\n') {
if (is_line_comment) break;
file->line_num++;
} }
} }
after_star: ; fputc(c, ppfp);
} else {
/* stray, eob or eof */
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == CH_EOF)
tcc_error("unexpected end of file in comment");
if (c == '\\') {
fputc(c, tcc_state->ppfp);
p++; p++;
} }
} if (!is_line_comment) {
} fputc('/', ppfp);
end_of_comment:
p++; p++;
file->line_ref = file->line_num; file->line_ref = file->line_num;
}
return p; return p;
} }
@ -680,7 +597,7 @@ static uint8_t *parse_line_comment(uint8_t *p, int skip)
p++; p++;
if (tcc_state->option_C && !skip) if (tcc_state->option_C && !skip)
return parse_print_line_comment(p); return parse_print_comment(p, 1);
for(;;) { for(;;) {
c = *p; c = *p;
redo: redo:
@ -719,7 +636,7 @@ ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip)
p++; p++;
if (tcc_state->option_C && !skip) if (tcc_state->option_C && !skip)
return parse_print_comment(p); return parse_print_comment(p, 0);
for(;;) { for(;;) {
/* fast skip loop */ /* fast skip loop */
for(;;) { for(;;) {