9front - general discussion about 9front
 help / color / mirror / Atom feed
From: ori@eigenstate.org
To: 9front@9front.org
Subject: Re: [9front] x509.c for ape in libsec.a
Date: Sun, 12 Jul 2020 18:39:06 -0700	[thread overview]
Message-ID: <0721D0AD992D75DF6B9C990154CC1CFE@eigenstate.org> (raw)
In-Reply-To: <FD382D45BEE0CBE709B6694073D3D7CC@hera.eonet.ne.jp>

>> Alright, I think I have a fix for this issue. We need to put the
>> macro into the hideset before doing the final pass, or there's
>> potential for macro expansion to cycle in some cases.
>> 
>> 	expand(A) => B
>> 	expand(B) => A
>> 	expand(A) => B
> 
> Thank you very much Ori.
> 
> Yes, it works fine here, too.
> 
> Kenji

It isn't quite right, though I haven't found any real-world
code that causes the problem, just artificial test cases.

I think this one should be regression-free.

diff -r 0cfcc2f8b898 sys/src/cmd/cpp/cpp.h
--- a/sys/src/cmd/cpp/cpp.h	Sat Jul 11 13:28:58 2020 -0700
+++ b/sys/src/cmd/cpp/cpp.h	Sun Jul 12 18:38:00 2020 -0700
@@ -110,7 +110,7 @@
 void	expand(Tokenrow *, Nlist *);
 void	builtin(Tokenrow *, int);
 int	gatherargs(Tokenrow *, Tokenrow **, int, int *);
-void	substargs(Nlist *, Tokenrow *, Tokenrow **);
+void	substargs(Nlist *, Tokenrow *, Tokenrow **, int);
 void	expandrow(Tokenrow *, char *);
 void	maketokenrow(int, Tokenrow *);
 Tokenrow *copytokenrow(Tokenrow *, Tokenrow *);
diff -r 0cfcc2f8b898 sys/src/cmd/cpp/macro.c
--- a/sys/src/cmd/cpp/macro.c	Sat Jul 11 13:28:58 2020 -0700
+++ b/sys/src/cmd/cpp/macro.c	Sun Jul 12 18:38:00 2020 -0700
@@ -169,9 +169,8 @@
 		}
 		if (np->flag&ISMAC)
 			builtin(trp, np->val);
-		else {
+		else
 			expand(trp, np);
-		}
 		tp = trp->tp;
 	}
 	if (flag)
@@ -186,18 +185,19 @@
 void
 expand(Tokenrow *trp, Nlist *np)
 {
+	static int depth;
+	int ntokc, narg, i, hs;
+	Tokenrow *atr[NARG+1];
 	Tokenrow ntr;
-	int ntokc, narg, i;
 	Token *tp;
-	Tokenrow *atr[NARG+1];
-	int hs;
 
+	depth++;
 	copytokenrow(&ntr, np->vp);		/* copy macro value */
 	if (np->ap==NULL) {			/* parameterless */
 		ntokc = 1;
 		/* substargs for handling # and ## */
 		atr[0] = nil;
-		substargs(np, &ntr, atr);
+		substargs(np, &ntr, atr, trp->tp->hideset);
 	} else {
 		ntokc = gatherargs(trp, atr, (np->flag&ISVARMAC) ? rowlen(np->ap) : 0, &narg);
 		if (narg<0) {			/* not actually a call (no '(') */
@@ -210,12 +210,13 @@
 			trp->tp += ntokc;
 			return;
 		}
-		substargs(np, &ntr, atr);	/* put args into replacement */
+		substargs(np, &ntr, atr, trp->tp->hideset);	/* put args into replacement */
 		for (i=0; i<narg; i++) {
 			dofree(atr[i]->bp);
 			dofree(atr[i]);
 		}
 	}
+
 	hs = newhideset(trp->tp->hideset, np);
 	for (tp=ntr.bp; tp<ntr.lp; tp++) {	/* distribute hidesets */
 		if (tp->type==NAME) {
@@ -343,19 +344,20 @@
 	}
 	return 0;
 }
+
 /*
  * substitute the argument list into the replacement string
  *  This would be simple except for ## and #
  */
 void
-substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
+substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr, int hideset)
 {
 	Tokenrow ttr, rp, rn;
 	Token *tp, *ap, *an, *pp, *pn;
 	int ntok, argno, hs;
 
 	for (rtr->tp=rtr->bp; rtr->tp<rtr->lp; ) {
-		if(rtr->tp->hideset && checkhideset(rtr->tp->hideset, np)) {
+		if(rtr->tp->hideset && checkhideset(hideset, np)) {
 			rtr->tp++;
 		} else if (rtr->tp->type==SHARP) {	/* string operator */
 			tp = rtr->tp;
@@ -405,10 +407,10 @@
 				*ttr.tp = *rtr->tp;
 
 				hs = newhideset(rtr->tp->hideset, np);
-				if(ttr.tp->hideset == 0)
+				if(hideset == 0)
 					ttr.tp->hideset = hs;
 				else
-					ttr.tp->hideset = unionhideset(ttr.tp->hideset, hs);
+					ttr.tp->hideset = unionhideset(hideset, hs);
 				expandrow(&ttr, (char*)np->name);
 				for(tp = ttr.bp; tp != ttr.lp; tp++)
 					if(tp->type == COMMA)



      reply	other threads:[~2020-07-13  1:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-01  5:37 kokamoto
2020-07-02  4:54 ` [9front] " kokamoto
2020-07-02 14:48   ` Ori Bernstein
2020-07-04  0:39     ` kokamoto
2020-07-08  5:19       ` kokamoto
2020-07-09  4:19         ` kokamoto
2020-07-09  4:25           ` ori
2020-07-09 23:31             ` kokamoto
2020-07-10  0:23               ` kokamoto
2020-07-10  0:27               ` ori
2020-07-10  0:53                 ` Dr. Kenji Okamoto
2020-07-10  1:31                   ` kokamoto
2020-07-10  4:12                     ` ori
2020-07-10  4:36                       ` kokamoto
2020-07-13  1:39                         ` ori [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0721D0AD992D75DF6B9C990154CC1CFE@eigenstate.org \
    --to=ori@eigenstate.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).