commit 8e07fdb59f3eaca0e5e8ad160f4d2ae37270360e
parent 661b5b01217b553c0e0b142ae41bdf9c9b2ac14b
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Thu, 29 Jan 2026 15:48:18 +0100
cc1: Parse correctly **/ comments
The parser was consuming always a character when a * was found, making
that even number of * followed by a / were not considered an end of
comment.
Diffstat:
4 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/src/cmd/scc-cc/cc1/cpp.c b/src/cmd/scc-cc/cc1/cpp.c
@@ -874,6 +874,7 @@ ifclause(int negate, int isifdef)
if (cppctx == NR_COND-1)
error("too many nesting levels of conditional inclusion");
n = cppctx++;
+ DBG("CPP ifclause updates cppctx=%d", cppctx);
if (n > 0 && !ifstate[n-1].enabled) {
done = 1;
@@ -921,6 +922,7 @@ disabled:
static void
cppif(void)
{
+ DBG("CPP line=%u if cppctx=%d", lineno, cppctx);
disexpand = 0;
ifclause(0, 0);
}
@@ -928,18 +930,22 @@ cppif(void)
static void
ifdef(void)
{
+ DBG("CPP line=%u ifdef cppctx=%d", lineno, cppctx);
ifclause(0, 1);
}
static void
ifndef(void)
{
+ DBG("CPP line=%u ifndef cppctx=%d", lineno, cppctx);
ifclause(1, 1);
}
static void
cppelse(void)
{
+ DBG("CPP line=%u else cppctx=%d", lineno, cppctx);
+
if (cppctx == 0 || ifstate[cppctx-1].iselse) {
cpperror("#else without #ifdef/ifndef");
return;
@@ -966,6 +972,8 @@ cppelse(void)
static void
elif(void)
{
+ DBG("CPP line=%u elif cppctx=%d", lineno, cppctx);
+
if (cppctx == 0 || ifstate[cppctx-1].iselse) {
cpperror("#elif without #ifdef/ifndef");
return;
@@ -983,6 +991,7 @@ elif(void)
cppoff = 1;
} else {
--cppctx;
+ DBG("elif updates cppctx=%d", cppctx);
cppif();
}
}
@@ -990,6 +999,8 @@ elif(void)
static void
endif(void)
{
+ DBG("CPP line=%u endif cppctx=%d", lineno, cppctx);
+
if (cppctx == 0)
error("#endif without #if");
@@ -999,6 +1010,7 @@ endif(void)
cppoff = 0;
--cppctx;
+ DBG("CPP endif updates cppctx=%d", cppctx);
next();
}
diff --git a/src/cmd/scc-cc/cc1/lex.c b/src/cmd/scc-cc/cc1/lex.c
@@ -211,8 +211,9 @@ comment(int type)
{
int c;
+ c = readchar();
repeat:
- while ((c = readchar()) != EOF && c != type)
+ for ( ; c != EOF && c != type; c = readchar())
;
if (c == EOF) {
diff --git a/tests/cc/execute/0235-comment.c b/tests/cc/execute/0235-comment.c
@@ -0,0 +1,15 @@
+/***********************************************************
+************************************************************/
+#ifndef WHAT
+# define WHAT 1
+int x;
+/*
+ * A comment
+ */
+
+int
+main(void)
+{
+ return x;
+}
+#endif
diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst
@@ -225,3 +225,4 @@
0232-cppmacro.c
0233-ifelif.c
0234-macro.c
+0235-comment.c