Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variadic macros: no argument for ellipsis creates preprocessor parser error #701

Merged
merged 1 commit into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

import static org.sonar.cxx.api.CppKeyword.IFDEF;
import static org.sonar.cxx.api.CppKeyword.IFNDEF;
import static org.sonar.cxx.api.CppPunctuator.COMMA;
import static org.sonar.cxx.api.CppPunctuator.LT;
import static org.sonar.cxx.api.CxxTokenType.NUMBER;
import static org.sonar.cxx.api.CxxTokenType.PREPROCESSOR;
Expand Down Expand Up @@ -869,14 +870,29 @@ private List<Token> replaceParams(List<Token> body, List<Token> parameters, List
}
}

newTokens.add(Token.builder()
.setLine(replacement.getLine())
.setColumn(replacement.getColumn())
.setURI(replacement.getURI())
.setValueAndOriginalValue(newValue)
.setType(replacement.getType())
.setGeneratedCode(true)
.build());
if (newValue.isEmpty() && "__VA_ARGS__".equals(curr.getValue())) {
// the Visual C++ implementation will suppress a trailing comma
// if no arguments are passed to the ellipsis
for (int n = newTokens.size() - 1; n != 0; n = newTokens.size() - 1) {
if (newTokens.get(n).getType() == WS) {
newTokens.remove(n);
} else if (newTokens.get(n).getType() == COMMA) {
newTokens.remove(n);
break;
} else {
break;
}
}
} else {
newTokens.add(Token.builder()
.setLine(replacement.getLine())
.setColumn(replacement.getColumn())
.setURI(replacement.getURI())
.setValueAndOriginalValue(newValue)
.setType(replacement.getType())
.setGeneratedCode(true)
.build());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void macro_arguments() {
+ "string s = foo(bar);"))
.equals("string s = bar + \"x\" ; EOF"));
}

@Test
public void variadic_macros() {
assert (serialize(p.parse(
Expand All @@ -198,13 +198,21 @@ public void variadic_macros() {
+ "eprintf(\"%s:%d: \", input_file, lineno);"))
.equals("fprintf ( stderr , \"%s:%d: \" , input_file , lineno ) ; EOF"));

//without whitespace after the parameter list
// the Visual C++ implementation will suppress a trailing comma
// if no arguments are passed to the ellipsis
assert (serialize(p.parse(
"#define EMPTY\n"
+ "#define MACRO(s, ...) printf(s, __VA_ARGS__)\n"
+ "MACRO(\"error\", EMPTY);"))
.equals("printf ( \"error\" ) ; EOF"));

// without whitespace after the parameter list
assert (serialize(p.parse(
"#define foo(a...);\n"
+ "foo(a, b)"))
.equals("; EOF"));

//with more params and without whitespace after the parameter list
// with more params and without whitespace after the parameter list
assert (serialize(p.parse(
"#define foo(a, b...);\n"
+ "foo(a, b, c)"))
Expand Down