Use standard [[deprecated]] attribute if available

Add yet another wxDEPRECATED_XXX macro, this one simply expanding to
C++14 [[deprecated]] attribute if it's available and nothing otherwise.

It's a bit ridiculous to have so many different macros for deprecating
things, but the new one is useful because the standard attribute can be
used to deprecated enum elements, which is impossible with MSVC-specific
__declspec(deprecated).
This commit is contained in:
Vadim Zeitlin
2021-07-11 13:36:46 +01:00
parent 4f7f7a9ce1
commit 7a8f2dffbd
3 changed files with 46 additions and 6 deletions

View File

@@ -171,6 +171,8 @@ Currently the following symbols exist:
wxBitmapToggleButton class is available in addition to wxToggleButton.} wxBitmapToggleButton class is available in addition to wxToggleButton.}
@itemdef{wxHAS_CONFIG_TEMPLATE_RW, Defined if the currently used compiler @itemdef{wxHAS_CONFIG_TEMPLATE_RW, Defined if the currently used compiler
supports template Read() and Write() methods in wxConfig.} supports template Read() and Write() methods in wxConfig.}
@itemdef{wxHAS_DEPRECATED_ATTR, Defined if C++14 @c [[deprecated]] attribute is
supported (this symbol only exists in wxWidgets 3.1.6 or later).}
@itemdef{wxHAS_MEMBER_DEFAULT, Defined if the currently used compiler supports @itemdef{wxHAS_MEMBER_DEFAULT, Defined if the currently used compiler supports
C++11 @c =default.} C++11 @c =default.}
@itemdef{wxHAS_LARGE_FILES, Defined if wxFile supports files more than 4GB in @itemdef{wxHAS_LARGE_FILES, Defined if wxFile supports files more than 4GB in

View File

@@ -573,14 +573,26 @@ typedef short int WXTYPE;
/* /*
Macros for marking functions as being deprecated. Macros for marking functions as being deprecated.
The preferred macro in the new code is wxDEPRECATED_MSG() which allows to The preferred macro in the new code is wxDEPRECATED_ATTR() which expands to
explain why is the function deprecated. Almost all the existing code uses the standard [[deprecated]] attribute if supported and allows to explain
the older wxDEPRECATED() or its variants currently, but this will hopefully why is the function deprecated. If supporting older compilers is important,
change in the future. wxDEPRECATED_MSG() can be used as it's almost universally available and
still allows to explain the reason for the deprecation.
However almost all the existing code uses the older wxDEPRECATED() or its
variants currently, but this will hopefully change in the future.
*/ */
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(deprecated)
#define wxHAS_DEPRECATED_ATTR
#endif
#endif
/* The basic compiler-specific construct to generate a deprecation warning. */ /* The basic compiler-specific construct to generate a deprecation warning. */
#ifdef __clang__ #ifdef wxHAS_DEPRECATED_ATTR
#define wxDEPRECATED_DECL [[deprecated]]
#elif defined(__clang__)
#define wxDEPRECATED_DECL __attribute__((deprecated)) #define wxDEPRECATED_DECL __attribute__((deprecated))
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define wxDEPRECATED_DECL __attribute__((deprecated)) #define wxDEPRECATED_DECL __attribute__((deprecated))
@@ -590,13 +602,28 @@ typedef short int WXTYPE;
#define wxDEPRECATED_DECL #define wxDEPRECATED_DECL
#endif #endif
#ifdef wxHAS_DEPRECATED_ATTR
#define wxDEPRECATED_ATTR(msg) [[deprecated(msg)]]
#else
/*
Note that we can't fall back on wxDEPRECATED_DECL here, as the standard
attribute works in places where the compiler-specific one don't,
notably it can be used after enumerator declaration with MSVC, while
__declspec(deprecated) can't occur there as it can only be used before
the declaration.
*/
#define wxDEPRECATED_ATTR(msg)
#endif
/* /*
Macro taking the deprecation message. It applies to the next declaration. Macro taking the deprecation message. It applies to the next declaration.
If the compiler doesn't support showing the message, this degrades to a If the compiler doesn't support showing the message, this degrades to a
simple wxDEPRECATED(), i.e. at least gives a warning, if possible. simple wxDEPRECATED(), i.e. at least gives a warning, if possible.
*/ */
#if defined(__clang__) && defined(__has_extension) #ifdef wxHAS_DEPRECATED_ATTR
#define wxDEPRECATED_MSG(msg) [[deprecated(msg)]]
#elif defined(__clang__) && defined(__has_extension)
#if __has_extension(attribute_deprecated_with_message) #if __has_extension(attribute_deprecated_with_message)
#define wxDEPRECATED_MSG(msg) __attribute__((deprecated(msg))) #define wxDEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
#else #else

View File

@@ -1600,6 +1600,17 @@ template <typename T> void wxDELETE(T*& ptr);
*/ */
template <typename T> void wxDELETEA(T*& array); template <typename T> void wxDELETEA(T*& array);
/**
Expands to the standard C++14 [[deprecated]] attribute if supported.
If not supported by the compiler, expands to nothing. If support for such
compilers is important, use wxDEPRECATED_MSG() which is almost universally
available.
@since 3.1.6
*/
#define wxDEPRECATED_ATTR(msg) [[deprecated(msg)]]
/** /**
Generate deprecation warning with the given message when a function is Generate deprecation warning with the given message when a function is
used. used.