History

fixed comment parsing

bellard committed 23 years ago

Commit cab60181 parent 0c618f42 files · +48 −36Browse files

Unified · Split

2 files
  1. tcc.c +46 −35
  2. tccelf.c +2 −1
tcc.c+46−35
@@ -1772,22 +1772,40 @@ static void minp(void) · 1771 unmodified lines
17721772 }
17731773
17741774
1775−static void parse_line_comment(void)
1775+/* single line C++ comments */
1776+static uint8_t *parse_line_comment(uint8_t *p)
17761777 {
1777− /* single line C++ comments */
1778− /* XXX: accept '\\\n' ? */
1779− inp();
1780− while (ch != '\n' && ch != CH_EOF)
1781− inp();
1778+ int c;
1779+
1780+ p++;
1781+ for(;;) {
1782+ c = *p;
1783+ if (c == '\n' || c == CH_EOF) {
1784+ break;
1785+ } else if (c == '\\') {
1786+ PEEKC_EOB(c, p);
1787+ if (c == '\n') {
1788+ file->line_num++;
1789+ PEEKC_EOB(c, p);
1790+ } else if (c == '\r') {
1791+ PEEKC_EOB(c, p);
1792+ if (c == '\n') {
1793+ file->line_num++;
1794+ PEEKC_EOB(c, p);
1795+ }
1796+ }
1797+ } else {
1798+ p++;
1799+ }
1800+ }
1801+ return p;
17821802 }
17831803
1784−static void parse_comment(void)
1804+/* C comments */
1805+static uint8_t *parse_comment(uint8_t *p)
17851806 {
1786− uint8_t *p;
17871807 int c;
17881808
1789− /* C comments */
1790− p = file->buf_ptr;
17911809 p++;
17921810 for(;;) {
17931811 /* fast skip loop */
@@ -1816,32 +1834,30 @@ static void parse_comment(void) · 22 unmodified lines
18161834 } else if (c == '\\') {
18171835 file->buf_ptr = p;
18181836 c = handle_eob();
1837+ p = file->buf_ptr;
18191838 if (c == '\\') {
1820− /* skip '\\n', but if '\' followed but another
1821− char, behave asif a stray was parsed */
1822− ch = file->buf_ptr[0];
1823− while (ch == '\\') {
1824− inp();
1825− if (ch == '\n') {
1839+ /* skip '\[\r]\n', otherwise just skip the stray */
1840+ while (c == '\\') {
1841+ PEEKC_EOB(c, p);
1842+ if (c == '\n') {
18261843 file->line_num++;
1827− inp();
1828− } else if (ch == '\r') {
1829− inp();
1830− if (ch == '\n') {
1844+ PEEKC_EOB(c, p);
1845+ } else if (c == '\r') {
1846+ PEEKC_EOB(c, p);
1847+ if (c == '\n') {
18311848 file->line_num++;
1832− inp();
1849+ PEEKC_EOB(c, p);
18331850 }
18341851 } else {
1835− p = file->buf_ptr;
1836− break;
1852+ goto after_star;
18371853 }
18381854 }
18391855 }
1840− p = file->buf_ptr;
18411856 } else {
18421857 break;
18431858 }
18441859 }
1860+ after_star: ;
18451861 } else {
18461862 /* stray, eob or eof */
18471863 file->buf_ptr = p;
@@ -1856,8 +1872,7 @@ static void parse_comment(void) · 8 unmodified lines
18561872 }
18571873 end_of_comment:
18581874 p++;
1859− file->buf_ptr = p;
1860− ch = *p;
1875+ return p;
18611876 }
18621877
18631878 #define cinp minp
@@ -1984,12 +1999,12 @@ void preprocess_skip(void) · 120 unmodified lines
19841999 file->buf_ptr = p;
19852000 ch = *p;
19862001 minp();
2002+ p = file->buf_ptr;
19872003 if (ch == '*') {
1988− parse_comment();
2004+ p = parse_comment(p);
19892005 } else if (ch == '/') {
1990− parse_line_comment();
2006+ p = parse_line_comment(p);
19912007 }
1992− p = file->buf_ptr;
19932008 break;
19942009
19952010 case '#':
@@ -3541,14 +3556,10 @@ static inline void next_nomacro1(void) · 1545 unmodified lines
35413556 case '/':
35423557 PEEKC(c, p);
35433558 if (c == '*') {
3544− file->buf_ptr = p;
3545− parse_comment();
3546− p = file->buf_ptr;
3559+ p = parse_comment(p);
35473560 goto redo_no_start;
35483561 } else if (c == '/') {
3549− file->buf_ptr = p;
3550− parse_line_comment();
3551− p = file->buf_ptr;
3562+ p = parse_line_comment(p);
35523563 goto redo_no_start;
35533564 } else if (c == '=') {
35543565 p++;
tccelf.c+2−1
@@ -1868,7 +1868,8 @@ static int ld_next(TCCState *s1, char *name, int name_size) · 1867 unmodified lines
18681868 case '/':
18691869 minp();
18701870 if (ch == '*') {
1871− parse_comment();
1871+ file->buf_ptr = parse_comment(file->buf_ptr);
1872+ ch = file->buf_ptr[0];
18721873 goto redo;
18731874 } else {
18741875 q = name;

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