scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 60560f4989f5e451ab2d1ad96bc6ebcab6337017
parent 0914984fe8a45e5928b4f975ee7530574452f19d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 22 Jun 2016 09:07:00 +0200

[cc1] Fix comments across several files

If a comment finishes before of the end of its file then
we have to continue from the previous file, and give
a message error when we arrive to the end of the base
file.

Diffstat:
Mcc1/lex.c | 35+++++++++++++++++++++--------------
Acc1/tests/test063.c | 12++++++++++++
Acc1/tests/test063.h | 9+++++++++
3 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/cc1/lex.c b/cc1/lex.c @@ -127,13 +127,15 @@ newline(void) die("error: input file '%s' too long", input->fname); } -static char +static int readchar(void) { int c; FILE *fp; repeat: + if (eof) + return 0; fp = input->fp; switch (c = getc(fp)) { @@ -157,21 +159,24 @@ repeat: } static void -comment(char type) +comment(int type) { - if (type == '*') { - while (!eof) { - while (readchar() != '*' && !eof) - /* nothing */; - if (readchar() == '/') - break; - } - } else { - while (readchar() != '\n' && !eof) - /* nothing */; + int c; + + c = -1; +repeat: + do { + if (!c) + delinput(); + } while (!eof && (c = readchar()) != type); + + if (eof) { + errorp("unterminated comment"); + return; } - if (eof) - error("unterminated comment"); + + if (type == '*' && (c = readchar()) != '/') + goto repeat; } static int @@ -201,6 +206,8 @@ repeat: peekc = c; c = '/'; } else { + if (c == '/') + c = '\n'; comment(c); c = ' '; } diff --git a/cc1/tests/test063.c b/cc1/tests/test063.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ + +/* +name: TEST063 +description: Test a comment that goes beyond of the end of an included file +error: +test063.c:12: error: unterminated comment +test063.c:12: error: #endif expected +output: +*/ + +#include "test063.h" diff --git a/cc1/tests/test063.h b/cc1/tests/test063.h @@ -0,0 +1,9 @@ + +#ifndef TEST_H_ +#define TEST_H_ + +/* + This is an unterminated comment. + + +#endif