diff macro.c @ 87:2b153df78214

Don't bomb out if a function-like macro is given no arguments. Instead, do the traditional thing and just emit the macro name.
author David A. Holland
date Mon, 10 Jun 2013 21:56:09 -0400
parents c91dc1315745
children 36066289e31a
line wrap: on
line diff
--- a/macro.c	Mon Jun 10 21:37:01 2013 -0400
+++ b/macro.c	Mon Jun 10 21:56:09 2013 -0400
@@ -823,6 +823,27 @@
 	es->curmacro->inuse = false;
 }
 
+/*
+ * The traditional behavior if a function-like macro appears without
+ * arguments is to pretend it isn't a macro; that is, just emit its
+ * name.
+ */
+static
+void
+expand_missingargs(struct expstate *es, struct place *p, bool needspace)
+{
+	if (es->curmacro == NULL) {
+		/* defined */
+		expand_send(es, p, "defined", 7);
+		return;
+	}
+	expand_send(es, p, es->curmacro->name, strlen(es->curmacro->name));
+	/* send a space in case we ate whitespace after the macro name */
+	if (needspace) {
+		expand_send(es, p, " ", 1);
+	}
+}
+
 static
 void
 expand_got_ws(struct expstate *es, struct place *p, char *buf, size_t len)
@@ -877,15 +898,15 @@
 		break;
 	    case ES_WANTLPAREN:
 		if (es->curmacro != NULL) {
-			complain(p, "Expected arguments for macro %s",
-				 es->curmacro->name);
-			complain_fail();
+			expand_missingargs(es, p, true);
+			es->state = ES_NORMAL;
+			/* try again */
+			expand_got_word(es, p, buf, len);
 		} else {
 			/* "defined foo" means "defined(foo)" */
 			expand_newarg(es, buf, len);
 			es->state = ES_NORMAL;
 			expand_domacro(es, p);
-			break;
 		}
 		break;
 	    case ES_NOARG:
@@ -930,13 +951,10 @@
 		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
-		if (es->curmacro) {
-			complain(p, "Expected arguments for macro %s",
-				 es->curmacro->name);
-		} else {
-			complain(p, "Expected arguments for defined()");
-		}
-		complain_fail();
+		expand_missingargs(es, p, false);
+		es->state = ES_NORMAL;
+		/* try again */
+		expand_got_rparen(es, p, buf, len);
 		break;
 	    case ES_NOARG:
 		assert(es->argparens == 0);
@@ -964,13 +982,10 @@
 		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
-		if (es->curmacro) {
-			complain(p, "Expected arguments for macro %s",
-				 es->curmacro->name);
-		} else {
-			complain(p, "Expected arguments for defined()");
-		}
-		complain_fail();
+		expand_missingargs(es, p, false);
+		es->state = ES_NORMAL;
+		/* try again */
+		expand_got_comma(es, p, buf, len);
 		break;
 	    case ES_NOARG:
 		assert(es->argparens == 0);
@@ -995,13 +1010,10 @@
 		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
-		if (es->curmacro) {
-			complain(p, "Expected arguments for macro %s",
-				 es->curmacro->name);
-		} else {
-			complain(p, "Expected arguments for defined()");
-		}
-		complain_fail();
+		expand_missingargs(es, p, false);
+		es->state = ES_NORMAL;
+		/* try again */
+		expand_got_other(es, p, buf, len);
 		break;
 	    case ES_NOARG:
 		expand_newarg(es, buf, len);
@@ -1022,13 +1034,7 @@
 		expand_send_eof(es, p);
 		break;
 	    case ES_WANTLPAREN:
-		if (es->curmacro) {
-			complain(p, "Expected arguments for macro %s",
-				 es->curmacro->name);
-		} else {
-			complain(p, "Expected arguments for defined()");
-		}
-		complain_fail();
+		expand_missingargs(es, p, false);
 		break;
 	    case ES_NOARG:
 	    case ES_HAVEARG: