fixed comment parsing

tcc-xref
bellard 2003-04-13 18:05:51 +00:00
parent 0c618f4b7f
commit cab6018913
2 changed files with 50 additions and 38 deletions

85
tcc.c
View File

@ -1772,22 +1772,40 @@ static void minp(void)
} }
static void parse_line_comment(void) /* single line C++ comments */
static uint8_t *parse_line_comment(uint8_t *p)
{ {
/* single line C++ comments */ int c;
/* XXX: accept '\\\n' ? */
inp(); p++;
while (ch != '\n' && ch != CH_EOF) for(;;) {
inp(); c = *p;
if (c == '\n' || c == CH_EOF) {
break;
} else 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);
}
}
} else {
p++;
}
}
return p;
} }
static void parse_comment(void) /* C comments */
static uint8_t *parse_comment(uint8_t *p)
{ {
uint8_t *p;
int c; int c;
/* C comments */
p = file->buf_ptr;
p++; p++;
for(;;) { for(;;) {
/* fast skip loop */ /* fast skip loop */
@ -1816,32 +1834,30 @@ static void parse_comment(void)
} else if (c == '\\') { } else if (c == '\\') {
file->buf_ptr = p; file->buf_ptr = p;
c = handle_eob(); c = handle_eob();
p = file->buf_ptr;
if (c == '\\') { if (c == '\\') {
/* skip '\\n', but if '\' followed but another /* skip '\[\r]\n', otherwise just skip the stray */
char, behave asif a stray was parsed */ while (c == '\\') {
ch = file->buf_ptr[0]; PEEKC_EOB(c, p);
while (ch == '\\') { if (c == '\n') {
inp();
if (ch == '\n') {
file->line_num++; file->line_num++;
inp(); PEEKC_EOB(c, p);
} else if (ch == '\r') { } else if (c == '\r') {
inp(); PEEKC_EOB(c, p);
if (ch == '\n') { if (c == '\n') {
file->line_num++; file->line_num++;
inp(); PEEKC_EOB(c, p);
} }
} else { } else {
p = file->buf_ptr; goto after_star;
break;
} }
} }
} }
p = file->buf_ptr;
} else { } else {
break; break;
} }
} }
after_star: ;
} else { } else {
/* stray, eob or eof */ /* stray, eob or eof */
file->buf_ptr = p; file->buf_ptr = p;
@ -1856,8 +1872,7 @@ static void parse_comment(void)
} }
end_of_comment: end_of_comment:
p++; p++;
file->buf_ptr = p; return p;
ch = *p;
} }
#define cinp minp #define cinp minp
@ -1984,12 +1999,12 @@ void preprocess_skip(void)
file->buf_ptr = p; file->buf_ptr = p;
ch = *p; ch = *p;
minp(); minp();
if (ch == '*') {
parse_comment();
} else if (ch == '/') {
parse_line_comment();
}
p = file->buf_ptr; p = file->buf_ptr;
if (ch == '*') {
p = parse_comment(p);
} else if (ch == '/') {
p = parse_line_comment(p);
}
break; break;
case '#': case '#':
@ -3541,14 +3556,10 @@ static inline void next_nomacro1(void)
case '/': case '/':
PEEKC(c, p); PEEKC(c, p);
if (c == '*') { if (c == '*') {
file->buf_ptr = p; p = parse_comment(p);
parse_comment();
p = file->buf_ptr;
goto redo_no_start; goto redo_no_start;
} else if (c == '/') { } else if (c == '/') {
file->buf_ptr = p; p = parse_line_comment(p);
parse_line_comment();
p = file->buf_ptr;
goto redo_no_start; goto redo_no_start;
} else if (c == '=') { } else if (c == '=') {
p++; p++;

View File

@ -1868,7 +1868,8 @@ static int ld_next(TCCState *s1, char *name, int name_size)
case '/': case '/':
minp(); minp();
if (ch == '*') { if (ch == '*') {
parse_comment(); file->buf_ptr = parse_comment(file->buf_ptr);
ch = file->buf_ptr[0];
goto redo; goto redo;
} else { } else {
q = name; q = name;