shithub: scc

Download patch

ref: 43efcd9672e99df6b4717a65d77eb100f3b6ddc3
parent: ce6b776c6933ca8ce6e078896471287b3230cf9c
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Nov 14 02:37:51 EST 2021

cc1: Add more comments in copymacro()

Macro expansion is a complex process, and some of the tokens are a
bit obscure, like for example input ## produces the token $.
To make easier to read the code a few comments are added to show
these translations.

--- a/src/cmd/cc/cc1/cpp.c
+++ b/src/cmd/cc/cc1/cpp.c
@@ -144,7 +144,8 @@
 		switch (yytoken) {
 		case ')':
 		case ',':
-			mp->argp -= 3;  /* remove " , "  or " ) "*/
+			/* remove " , "  or " ) "*/
+			mp->argp -= 3;
 			*mp->argp++ = '\0';
 			return;
 		case '(':
@@ -199,12 +200,6 @@
 	bufsiz = mp->bufsiz;
 	for (s = mp->def; c = *s; ++s) {
 		switch (c) {
-		case '$':
-			while (bp[-1] == ' ')
-				--bp, ++bufsiz;
-			while (s[1] == ' ')
-				++s;
-			break;
 		case '\'':
 			delim = '\'';
 			goto search_delim;
@@ -228,7 +223,15 @@
 			bufsiz -= size;
 			bp += size;
 			break;
+		case '$':
+			/* token concatenation operator */
+			while (bp[-1] == ' ')
+				--bp, ++bufsiz;
+			while (s[1] == ' ')
+				++s;
+			break;
 		case '#':
+			/* stringfier operator */
 			arg = mp->arglist[atoi(s += 2)];
 			s += 2;
 
@@ -254,6 +257,7 @@
 
 			break;
 		case '@':
+			/* parameter substitution */
 			arg = mp->arglist[atoi(++s)];
 			s += 2;
 
@@ -340,7 +344,9 @@
 	next();
 	if (c != '(')
 		return -1;
-	next(); /* skip the '(' */
+
+	/* skip the '(' */
+	next();
 	if (accept(')'))
 		return 0;
 
@@ -403,6 +409,7 @@
 			cpperror("macro too long");
 			return 0;
 		}
+		/* $ token is generated by ## */
 		if (yytoken == '$') {
 			*bp++ = '$';
 			 --bufsiz;