Handle C++14 auto return type function declaraction

Fix parse error for C++14 forward declaration of function with auto
return type and no trailing return type.

Fixes #3186
This commit is contained in:
Olly Betts 2025-05-28 14:10:03 +12:00
parent fde28cb9cb
commit 739997707c
4 changed files with 57 additions and 0 deletions

View File

@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.4.0 (in progress)
===========================
2025-05-28: olly
#3186 Fix parse error for C++14 forward declaration of function with
auto return type and no trailing return type.
2025-05-19: jschueller
[Python] #3059 Use new buffer protocol for PY_LIMITED_API for python-3.11
and later since this is when it was added to the stable ABI, noting that

View File

@ -112,6 +112,7 @@ struct X {
<p>
<b>Compatibility note:</b> SWIG-4.2.0 first introduced support for functions declared with an auto return without a trailing return type.
SWIG 4.4.0 added support for forward declarations of such functions.
</p>

View File

@ -2,6 +2,9 @@
// function with no trailing return type, introduced in C++14.
%module cpp14_auto_return_type
%warnfilter(SWIGWARN_CPP14_AUTO) forward_decl;
%warnfilter(SWIGWARN_CPP14_AUTO) operator==(const teca_variant_array_util::X*, const teca_variant_array_util::X&);
// SWIG can't deduce the return type, so we ignore the `auto`-using declaration
// (which would typically be in a header being wrapped) and provide a
// declaration with an explicit return type in the interface file.
@ -40,6 +43,12 @@ struct X {
static auto s() {
return true;
}
// Forward declaration (parse error with SWIG < 4.4):.
auto forward_decl() const;
};
}
// More forward declarations (parse error with SWIG < 4.4):.
auto operator==(const teca_variant_array_util::X*,const teca_variant_array_util::X&);
auto forward_decl();
%}

View File

@ -3570,6 +3570,49 @@ c_decl : storage_class type declarator cpp_const initializer c_decl_tail {
Swig_error(cparse_file, cparse_line, "Static function %s cannot have a qualifier.\n", Swig_name_decl($$));
Delete($storage_class);
}
// C++14. Like the previous case but a declaration rather than a
// definition. A C++ compiler will deduce the return type when it
// sees the corresponding definition, but SWIG may never see that
// definition.
| storage_class AUTO declarator cpp_const SEMI {
$$ = new_node("cdecl");
if ($cpp_const.qualifier) SwigType_push($declarator.type, $cpp_const.qualifier);
Setattr($$, "refqualifier", $cpp_const.refqualifier);
Setattr($$, "type", NewString("auto"));
Setattr($$, "storage", $storage_class);
Setattr($$, "name", $declarator.id);
Setattr($$, "decl", $declarator.type);
Setattr($$, "parms", $declarator.parms);
Setattr($$, "throws", $cpp_const.throws);
Setattr($$, "throw", $cpp_const.throwf);
Setattr($$, "noexcept", $cpp_const.nexcept);
Setattr($$, "final", $cpp_const.final);
if ($declarator.id) {
/* Ignore all scoped declarations, could be 1. out of class function definition 2. friend function declaration 3. ... */
String *p = Swig_scopename_prefix($declarator.id);
if (p) {
if ((Namespaceprefix && Strcmp(p, Namespaceprefix) == 0) ||
(Classprefix && Strcmp(p, Classprefix) == 0)) {
String *lstr = Swig_scopename_last($declarator.id);
Setattr($$, "name", lstr);
Delete(lstr);
} else {
Delete($$);
$$ = 0;
}
Delete(p);
} else if (Strncmp($declarator.id, "::", 2) == 0) {
/* global scope declaration/definition ignored */
Delete($$);
$$ = 0;
}
}
if ($cpp_const.qualifier && $storage_class && Strstr($storage_class, "static"))
Swig_error(cparse_file, cparse_line, "Static function %s cannot have a qualifier.\n", Swig_name_decl($$));
Delete($storage_class);
}
/* C++11 auto variable declaration. */
| storage_class AUTO idcolon EQUAL definetype SEMI {
SwigType *type = deduce_type(&$definetype);