diff options
-rw-r--r-- | src/main/java/org/anarres/cpp/Preprocessor.java | 7 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/CppReaderTest.java | 7 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/PreprocessorTest.java | 24 | ||||
-rw-r--r-- | src/test/resources/varargs.c | 8 |
4 files changed, 43 insertions, 3 deletions
diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java index f636872..6fb3249 100644 --- a/src/main/java/org/anarres/cpp/Preprocessor.java +++ b/src/main/java/org/anarres/cpp/Preprocessor.java @@ -905,6 +905,13 @@ public class Preprocessor implements Closeable { case IDENTIFIER: args.add(tok.getText()); break; + case ELLIPSIS: + // Unnamed Variadic macro + args.add("__VA_ARGS__"); + // We just named the ellipsis, but we unget the token + // to allow the ELLIPSIS handling below to process it. + source_untoken(tok); + break; case NL: case EOF: error(tok, diff --git a/src/test/java/org/anarres/cpp/CppReaderTest.java b/src/test/java/org/anarres/cpp/CppReaderTest.java index 6f98bd4..7f86f01 100644 --- a/src/test/java/org/anarres/cpp/CppReaderTest.java +++ b/src/test/java/org/anarres/cpp/CppReaderTest.java @@ -38,6 +38,13 @@ public class CppReaderTest { } @Test + public void testVarargs() + throws Exception { + // The newlines are irrelevant, We want exactly one "foo" + testCppReader("#include <varargs.c>\n"); + } + + @Test public void testPragmaOnce() throws Exception { // The newlines are irrelevant, We want exactly one "foo" diff --git a/src/test/java/org/anarres/cpp/PreprocessorTest.java b/src/test/java/org/anarres/cpp/PreprocessorTest.java index fb2a8ac..c8bd7a1 100644 --- a/src/test/java/org/anarres/cpp/PreprocessorTest.java +++ b/src/test/java/org/anarres/cpp/PreprocessorTest.java @@ -32,7 +32,7 @@ public class PreprocessorTest { private static class I { - private String t; + private final String t; public I(String t) { this.t = t; @@ -42,6 +42,7 @@ public class PreprocessorTest { return t; } + @Override public String toString() { return getText(); } @@ -116,14 +117,31 @@ public class PreprocessorTest { testInput("one /* one */\n", NL, I("one"), WHITESPACE, CCOMMENT); /* Variadic macros. */ - testInput("#define var(x...) a x b\n", NL); + testInput("#define var(x...) a x __VA_ARGS__ b\n", NL); testInput("var(e, f, g)\n", NL, I("a"), WHITESPACE, I("e"), ',', WHITESPACE, I("f"), ',', WHITESPACE, I("g"), WHITESPACE, + I("__VA_ARGS__"), WHITESPACE, // __VA_ARGS__ is not expanded in this case. I("b") ); + /* Variadic macros with anonymous args. */ + testInput("#define var2(x, ...) a x __VA_ARGS__ e\n", NL); + testInput("var2(b, c, d)\n", NL, + I("a"), WHITESPACE, + I("b"), WHITESPACE, + I("c"), ',', WHITESPACE, + I("d"), WHITESPACE, + I("e") + ); + testInput("#define var3(...) a __VA_ARGS__ d\n", NL); + testInput("var3(b, c)\n", NL, + I("a"), WHITESPACE, + I("b"), ',', WHITESPACE, + I("c"), WHITESPACE, + I("d") + ); testInput("#define _Widen(x) L ## x\n", NL); testInput("#define Widen(x) _Widen(x)\n", NL); @@ -155,7 +173,7 @@ public class PreprocessorTest { } else if (v instanceof I) { if (t.getType() != IDENTIFIER) fail("Expected IDENTIFIER " + v + ", but got " + t); - assertEquals(((I) v).getText(), (String) t.getText()); + assertEquals(((I) v).getText(), t.getText()); } else if (v instanceof Character) assertEquals((int) ((Character) v).charValue(), t.getType()); else if (v instanceof Integer) diff --git a/src/test/resources/varargs.c b/src/test/resources/varargs.c new file mode 100644 index 0000000..5eac2fa --- /dev/null +++ b/src/test/resources/varargs.c @@ -0,0 +1,8 @@ +#define var(x...) a x __VA_ARGS__ b +var(b, c, d) + +#define var2(x, ...) a x __VA_ARGS__ b +var2(b, c, d) + +#define var3(...) a x __VA_ARGS__ b +var3(b, c, d) |