shevek / jcpp

The C Preprocessor as a Java library
http://www.anarres.org/projects/jcpp/
Apache License 2.0
106 stars 36 forks source link

Empty variadic arguments #23

Closed iserikov closed 9 years ago

iserikov commented 9 years ago
  1. Empty (missing) variadic arguments should be accepted.
  2. ``, ## VA_ARGS'' should remove the comma, if the variadic argument is empty.
shevek commented 9 years ago
diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java
index 18a0605..767fa32 100644
--- a/src/main/java/org/anarres/cpp/Preprocessor.java
+++ b/src/main/java/org/anarres/cpp/Preprocessor.java
@@ -782,16 +782,29 @@ public class Preprocessor implements Closeable {
                 /* space may still be true here, thus trailing space
                  * is stripped from arguments. */

+
                 if (args.size() != m.getArgs()) {
-                    error(tok,
-                            "macro " + m.getName()
-                            + " has " + m.getArgs() + " parameters "
-                            + "but given " + args.size() + " args");
-                    /* We could replay the arg tokens, but I
-                     * note that GNU cpp does exactly what we do,
-                     * i.e. output the macro name and chew the args.
-                     */
-                    return false;
+                    if (m.isVariadic()) {
+                        if (args.size() == m.getArgs() - 1)
+                            args.add(new Argument ());
+                        else {
+                            error(tok,
+                                  "variadic macro " + m.getName()
+                                  + " has at least " + (m.getArgs() - 1) + " parameters "
+                                  + "but given " + args.size() + " args");
+                            return false;
+                        }
+                    } else {
+                        error(tok,
+                              "macro " + m.getName()
+                              + " has " + m.getArgs() + " parameters "
+                              + "but given " + args.size() + " args");
+                        /* We could replay the arg tokens, but I
+                         * note that GNU cpp does exactly what we do,
+                         * i.e. output the macro name and chew the args.
+                         */
+                        return false;
+                    }
                 }

                 for (Argument a : args) {