Skip to content

Commit

Permalink
Added support for anonymous args in variadic macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ochafik committed Nov 5, 2013
1 parent 5182afb commit 444a57c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/velocity/org/anarres/cpp/Preprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,11 @@ private Token define()
error(tok,
"Unterminated macro parameter list");
return tok;
case ELLIPSIS:
// Unnamed Variadic Macros
args.add("__VA_ARGS__");
source_untoken(tok);
break;
default:
error(tok,
"error in macro parameters: " +
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/org/anarres/cpp/PreprocessorTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,31 @@ public void testPreprocessor() throws Exception {
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);
Expand Down
8 changes: 8 additions & 0 deletions t.c
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 444a57c

Please sign in to comment.