Fix handling of macro with empty argument list

This commit is contained in:
Olly Betts 2017-10-04 15:00:54 +13:00
commit 866840f791

View file

@ -817,11 +817,22 @@ static String *expand_macro(String *name, List *args, String *line_file) {
Delete(vararg);
}
}
if (args && margs && Len(margs) == 1 && Len(args) == 0) {
/* FOO() can invoke a macro defined as FOO(X) as well as one defined FOO().
* Handle this by adding an empty argument to args.
*
* We don't need to worry about varargs here - a varargs macro will always have
* Len(margs) >= 1, since the varargs are put in the final macro argument.
*/
Append(args, NewStringEmpty());
}
/* If there are arguments, see if they match what we were given */
if (args && (margs) && (Len(margs) != Len(args))) {
if (Len(margs) > (1 + isvarargs))
if (args && (!margs || Len(margs) != Len(args))) {
if (margs && Len(margs) > (1 + isvarargs))
Swig_error(macro_start_file, macro_start_line, "Macro '%s' expects %d arguments\n", name, Len(margs) - isvarargs);
else if (Len(margs) == (1 + isvarargs))
else if (margs && Len(margs) == (1 + isvarargs))
Swig_error(macro_start_file, macro_start_line, "Macro '%s' expects 1 argument\n", name);
else
Swig_error(macro_start_file, macro_start_line, "Macro '%s' expects no arguments\n", name);
@ -830,7 +841,7 @@ static String *expand_macro(String *name, List *args, String *line_file) {
}
/* If the macro expects arguments, but none were supplied, we leave it in place */
if (!args && (margs) && Len(margs) > 0) {
if (!args && margs) {
macro_level--;
return NewString(name);
}
@ -1156,10 +1167,6 @@ static DOH *Preprocessor_replace(DOH *s) {
args = find_args(s, 1, id);
macro_additional_lines = Getline(s) - line;
assert(macro_additional_lines >= 0);
if (!Len(args)) {
Delete(args);
args = 0;
}
} else {
args = 0;
}