diff --git a/Source/Preprocessor/cpp.c b/Source/Preprocessor/cpp.c index 491eb8a96..47dc53b6b 100644 --- a/Source/Preprocessor/cpp.c +++ b/Source/Preprocessor/cpp.c @@ -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; }