commit 33eecd2b41f96ff1cbc2b422abdfe9731f8b3504
parent d568fd8d152de68813cfe4a3f9a66d9ed9165c69
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 8 Feb 2023 23:13:19 +0100
cc1: Don't evaluate cpp if expression in disabled code
Cpp conditionals have to be tracked to keep the relation between
if/elif/endif but the expressions must not be evaluated because
they can generate errors due to wrong syntax.
Reported-by: Nicholas Fraser <nick@ludocode.com>
Diffstat:
3 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c
@@ -766,8 +766,13 @@ ifclause(int negate, int isifdef)
if (cppctx == NR_COND-1)
error("too many nesting levels of conditional inclusion");
-
n = cppctx++;
+
+ if (cppoff) {
+ status = 0;
+ goto disabled;
+ }
+
namespace = NS_CPP;
next();
@@ -795,6 +800,8 @@ ifclause(int negate, int isifdef)
if (negate)
status = !status;
+
+disabled:
if (status == 0)
++cppoff;
DBG("CPP if result=%d", status);
diff --git a/tests/cc/execute/0222-ifdef.c b/tests/cc/execute/0222-ifdef.c
@@ -0,0 +1,11 @@
+#if 0
+ #if FOO(1)
+ ignored
+ #endif
+#endif
+
+int
+main()
+{
+ return 0;
+}
diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst
@@ -212,3 +212,4 @@
0219-abbrev.c
0220-comma.c
0221-ifdef.c
+0222-ifdef.c