From 015e2294c2f321e67891202cd441c87b0ce31239 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Wed, 4 May 2011 09:34:41 +0430 Subject: [PATCH] tok: handle // comments I don't like // comments but this patch cleans up cpp.c a bit and that's nice. --- cpp.c | 59 ++++++++++++++++++++++++++++------------------------------- tok.c | 21 ++++++++++++++------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/cpp.c b/cpp.c index 0cd88e2..b382257 100644 --- a/cpp.c +++ b/cpp.c @@ -159,14 +159,25 @@ static void read_word(char *dst) *dst = '\0'; } -static void jumpcomment(void) +static int jumpcomment(void) { - while (++cur < len) { - if (buf[cur] == '*' && buf[cur + 1] == '/') { - cur += 2; - break; + if (buf[cur] == '/' && buf[cur + 1] == '*') { + while (++cur < len) { + if (buf[cur] == '*' && buf[cur + 1] == '/') { + cur += 2; + return 0; + } } } + if (buf[cur] == '/' && buf[cur + 1] == '/') { + while (++cur < len) { + if (buf[cur] == '\n') { + cur++; + return 0; + } + } + } + return 1; } static void read_tilleol(char *dst) @@ -176,9 +187,7 @@ static void read_tilleol(char *dst) while (cur < len && buf[cur] != '\n') { if (buf[cur] == '\\' && buf[cur + 1] == '\n') cur += 2; - else if (buf[cur] == '/' && buf[cur + 1] == '*') - jumpcomment(); - else + else if (jumpcomment()) *dst++ = buf[cur++]; } *dst = '\0'; @@ -220,22 +229,23 @@ static int include_find(char *name, int std) return -1; } -static void jumpstr(void) +static int jumpstr(void) { if (buf[cur] == '\'') { while (cur < len && buf[++cur] != '\'') if (buf[cur] == '\\') cur++; cur++; - return; + return 0; } if (buf[cur] == '"') { while (cur < len && buf[++cur] != '"') if (buf[cur] == '\\') cur++; cur++; - return; + return 0; } + return 1; } static void readarg(char *s) @@ -243,6 +253,8 @@ static void readarg(char *s) int depth = 0; int beg = cur; while (cur < len && (depth || buf[cur] != ',' && buf[cur] != ')')) { + if (!jumpstr() || !jumpcomment()) + continue; switch (buf[cur]) { case '(': case '[': @@ -256,15 +268,8 @@ static void readarg(char *s) cur++; depth--; break; - case '\'': - case '"': - jumpstr(); - break; default: - if (buf[cur] == '/' && buf[cur + 1] == '*') - jumpcomment(); - else - cur++; + cur++; } } if (s) { @@ -376,14 +381,10 @@ static void jumpifs(int jumpelse) depth++; continue; } - if (buf[cur] == '/' && buf[cur + 1] == '*') { - jumpcomment(); + if (!jumpcomment()) continue; - } - if (buf[cur] == '\'' || buf[cur] == '"') { - jumpstr(); + if (!jumpstr()) continue; - } cur++; } } @@ -561,14 +562,10 @@ int cpp_read(char *s) while (cur < len) { if (buf[cur] == '#') break; - if (buf[cur] == '/' && buf[cur + 1] == '*') { - jumpcomment(); + if (!jumpcomment()) continue; - } - if (buf[cur] == '\'' || buf[cur] == '"') { - jumpstr(); + if (!jumpstr()) continue; - } if (isalpha(buf[cur]) || buf[cur] == '_') { char word[NAMELEN]; read_word(word); diff --git a/tok.c b/tok.c index 3b1604f..b3c805c 100644 --- a/tok.c +++ b/tok.c @@ -46,7 +46,7 @@ static struct { static char *tok3[] = { "<<=", ">>=", "...", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=", - "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*" + "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->" }; static int get_tok3(int num) @@ -193,14 +193,21 @@ static int skipws(void) cur++; if (cur == len) continue; - if (TOK2(buf + cur) != TOK2("/*")) - return 0; - while (++cur < len) { - if (buf[cur] == '*' && buf[cur + 1] == '/') { - cur += 2; - break; + if (buf[cur] == '/' && buf[cur + 1] == '/') { + while (cur < len && buf[cur] != '\n') + cur++; + continue; + } + if (buf[cur] == '/' && buf[cur + 1] == '*') { + while (++cur < len) { + if (buf[cur] == '*' && buf[cur + 1] == '/') { + cur += 2; + break; + } } + continue; } + break; } return 0; } -- 2.11.4.GIT