Merge pull request #1111 from ojwb/empty-arglist-macros

Fix handling of macro with empty argument list
This commit is contained in:
Olly Betts 2017-10-08 15:34:37 +13:00 • committed by GitHub
commit 59ebe27a95
6 changed files with 68 additions and 24 deletions

View file

@ -640,13 +640,8 @@ static List *find_args(String *s, int ismacro, String *macro_name) {
goto unterm;
}
Chop(str);
if (Len(args) || Len(str))
Append(args, str);
Append(args, str);
Delete(str);
/* if (Len(str) && (c != ')'))
Append(args,str); */
if (c == ')')
return args;
c = Getc(s);
@ -817,11 +812,24 @@ static String *expand_macro(String *name, List *args, String *line_file) {
Delete(vararg);
}
}
if (args && margs && Len(margs) == 0 && Len(args) == 1 && Len(Getitem(args, 0)) == 0) {
/* FOO() can invoke a macro defined as FOO(X) as well as one defined FOO().
*
* Handle this by removing the only argument if it's empty and the macro
* expects no arguments.
*
* 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.
*/
Delitem(args, 0);
}
/* 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 +838,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 +1164,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;
}