Use SWIG_NOEXCEPT for deprecated dynamic exception specifications

SWIG_NOEXCEPT is generated instead of throw() which is deprecated in
c++11. If c++11 or later is being used, then this macro expands to
noexcept instead of throw().

Affects director code only.

Also fix up some testcase to not use throw() when using c++11 or later.
Tested with clang and -Wdeprecated-dynamic-exception-spec as gcc
doesn't seem to warn for this deprecation.

Closes #3027
This commit is contained in:
William S Fulton 2025-04-29 22:23:23 +01:00
parent deadb05bd6
commit f91a9cb8e4
13 changed files with 36 additions and 25 deletions

View File

@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.4.0 (in progress)
===========================
2025-04-29: r-barnes, wsfulton
#3027 Remove generation of empty dynamic exception specifications - throw(),
which are deprecated in c++11. This was only when using the director feature.
A new macro SWIG_NOEXCEPT is used which ensures that throw() or noexcept is
used for the appropriate C++ standard that the compiler is compiling for.
2025-04-28: jschueller, henryiii
[Python] #2967 Use __spec__.parent as the first approach to obtaining
a module's package name when importing the low-level C/C++ module as the

View File

@ -107,11 +107,11 @@ struct moveonly {
};
struct ConstructorThrow {
ConstructorThrow() throw() = default;
ConstructorThrow(const ConstructorThrow&) throw() = delete;
ConstructorThrow(ConstructorThrow&&) throw() = delete;
ConstructorThrow& operator=(const ConstructorThrow&) throw() = delete;
~ConstructorThrow() throw() = default;
ConstructorThrow() noexcept = default;
ConstructorThrow(const ConstructorThrow&) noexcept = delete;
ConstructorThrow(ConstructorThrow&&) noexcept = delete;
ConstructorThrow& operator=(const ConstructorThrow&) noexcept = delete;
~ConstructorThrow() noexcept = default;
};
%}

View File

@ -5,8 +5,8 @@
%feature("director") Bar;
%{
#if defined(_MSC_VER)
#pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
#if defined(__cplusplus) && __cplusplus >=201103L
#define throw() noexcept
#endif
%}

View File

@ -28,10 +28,10 @@ namespace Swig {
DirectorException(const std::string &msg) : swig_msg(msg) {
}
virtual ~DirectorException() throw() {
virtual ~DirectorException() SWIG_NOEXCEPT {
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return swig_msg.c_str();
}
};

View File

@ -26,10 +26,10 @@ namespace Swig {
DirectorException(const std::string &msg) : swig_msg(msg) {
}
virtual ~DirectorException() throw() {
virtual ~DirectorException() SWIG_NOEXCEPT {
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return swig_msg.c_str();
}
};

View File

@ -428,12 +428,12 @@ namespace Swig {
DirectorException(const char *msg) : jenv_(SWIG_NULLPTR), throwable_(SWIG_NULLPTR), classname_(SWIG_NULLPTR), msg_(msg ? copystr(msg) : SWIG_NULLPTR) {
}
~DirectorException() throw() {
~DirectorException() SWIG_NOEXCEPT {
delete[] classname_;
delete[] msg_;
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return msg_ ? msg_ : "Unspecified DirectorException message";
}

View File

@ -20,10 +20,10 @@ namespace Swig {
DirectorException(const char *msg="") : swig_msg(msg) {
}
virtual ~DirectorException() throw() {
virtual ~DirectorException() SWIG_NOEXCEPT {
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return swig_msg.c_str();
}
};

View File

@ -151,7 +151,7 @@ namespace Swig {
SvREFCNT_inc(err);
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return SvPV_nolen(err);
}
@ -172,10 +172,10 @@ namespace Swig {
}
public:
virtual ~DirectorWrapException() throw() {
virtual ~DirectorWrapException() SWIG_NOEXCEPT {
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return msg.c_str();
}

View File

@ -136,10 +136,10 @@ namespace Swig {
if (!EG(exception)) zend_throw_exception(NULL, swig_msg.c_str(), code);
}
virtual ~DirectorException() throw() {
virtual ~DirectorException() SWIG_NOEXCEPT {
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return swig_msg.c_str();
}

View File

@ -177,7 +177,7 @@ namespace Swig {
SWIG_PYTHON_THREAD_END_BLOCK;
}
virtual ~DirectorException() throw() {
virtual ~DirectorException() SWIG_NOEXCEPT {
}
/* Deprecated, use what() instead */
@ -185,7 +185,7 @@ namespace Swig {
return what();
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return swig_msg.c_str();
}

View File

@ -132,7 +132,7 @@ namespace Swig {
}
public:
virtual ~DirectorException() throw() {
virtual ~DirectorException() SWIG_NOEXCEPT {
}
VALUE getType() const {
@ -148,7 +148,7 @@ namespace Swig {
return swig_msg;
}
const char *what() const throw() {
const char *what() const SWIG_NOEXCEPT {
return swig_msg.c_str();
}
};

View File

@ -124,6 +124,8 @@
#if defined(__cplusplus) && __cplusplus >=201103L
# define SWIG_NULLPTR nullptr
# define SWIG_NOEXCEPT noexcept
#else
# define SWIG_NULLPTR NULL
# define SWIG_NOEXCEPT throw()
#endif

View File

@ -2191,7 +2191,10 @@ int Language::classDirectorDestructor(Node *n) {
*/
File *f_directors = Swig_filebyname("director");
File *f_directors_h = Swig_filebyname("director_h");
if (Getattr(n, "throw")) {
if (Getattr(n, "noexcept")) {
Printf(f_directors_h, " virtual ~%s() noexcept;\n", DirectorClassName);
Printf(f_directors, "%s::~%s() noexcept {\n}\n\n", DirectorClassName, DirectorClassName);
} else if (Getattr(n, "throw")) {
Printf(f_directors_h, " virtual ~%s() throw();\n", DirectorClassName);
Printf(f_directors, "%s::~%s() throw() {\n}\n\n", DirectorClassName, DirectorClassName);
} else {