shithub: scc

Download patch

ref: 3ae3ab16c27973a7fdee2bd6daad1aad9cc9910c
parent: 7761303503062221973c8bd9bad4cb019cebe7f6
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Jul 27 05:44:34 EDT 2015

Implement #if

This is a basic implementation of #if, where #elsif and defined() are not
implemented. Some work can be done to integrate cppif() and ifclause()

--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -476,6 +476,24 @@
 }
 
 static void
+cppif(void)
+{
+	Node *expr;
+	int status;
+	unsigned n;
+
+	if (cppctx == NR_COND-1)
+		error("too much nesting levels of conditional inclusion");
+	n = cppctx++;
+
+	if ((expr = iconstexpr()) == NULL)
+		error("parameter of #if is not an integer constant expression");
+	status = expr->sym->u.i != 0;
+	if (!(ifstatus[n] = status))
+		++cppoff;
+}
+
+static void
 ifdef(void)
 {
 	ifclause(1);
@@ -535,6 +553,7 @@
 		{INCLUDE, include},
 		{LINE, line},
 		{IFDEF, ifdef},
+		{IF, cppif},
 		{IFNDEF, ifndef},
 		{ELSE, elseclause},
 		{ENDIF, endif},
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -262,6 +262,7 @@
 		{"include", INCLUDE, INCLUDE},
 		{"line", LINE, LINE},
 		{"ifdef", IFDEF, IFDEF},
+		{"if", IF, IF},
 		{"else", ELSE, ELSE},
 		{"ifndef", IFNDEF, IFNDEF},
 		{"endif", ENDIF, ENDIF},