OSGeo / gdal

GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
https://gdal.org
Other
4.81k stars 2.52k forks source link

Convert CPL_WARN_DEPRECATED macro to support [[deprecated]] and __declspec(deprecated) #4183

Open VirtualTim opened 3 years ago

VirtualTim commented 3 years ago

The macro CPL_WARN_DEPRECATED is defined like this: https://github.com/OSGeo/gdal/blob/4f9ae4b2304e9c7eb7070b4bab680080e4b461fd/gdal/port/cpl_port.h#L1010-L1021

C++14 added the attributes [[deprecated]] and [[deprecated(string-literal)]]. MSVC has declspec(deprecated) and declspec(deprecated(string-literal))

I think it would be a good idea to update CPL_WARNDEPRECATED to support both the C++14 attributes and the MSVC declspec deceleration. This is not as simple as updating the macro; attributes and declspec must come before the function deceleration, where gcc __attribute_\'s must come after.

I propose updating CPL_WARN_DEPRECATED to something like this, and then modifying every use of that macro to conform to the new format.

#if defined(__cplusplus) && __cplusplus >= 201402L
#   define CPL_WARN_DEPRECATED(func, msg)   [[deprecated(msg)]] func
#elif defined(__clang__) || defined(__GNUC__)
//deprecated(msg) supported since gcc 4.5 https://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Function-Attributes.html
#   define CPL_WARN_DEPRECATED(func, msg)   func __attribute__ ((deprecated(msg)))
#elif defined(_MSC_VER)
#   define CPL_WARN_DEPRECATED(func, msg)   __declspec(deprecated(msg)) func
#endif

Calls to CPL_WARN_DEPRECATED would then be changed from:

int vsnprintf(char *str, size_t size, const char* fmt, va_list args) CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");

to

CPL_WARN_DEPRECATED(int vsnprintf(char *str, size_t size, const char* fmt, va_list args), "Use CPLvsnprintf() instead")
rouault commented 3 years ago

Sounds like a good idea. Do you want to propose a pull request for that ?