Deprecated.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. /**
  3. * This file provides portable macros for marking declarations
  4. * as deprecated. You should generally use C10_DEPRECATED,
  5. * except when marking 'using' declarations as deprecated,
  6. * in which case you should use C10_DEFINE_DEPRECATED_USING
  7. * (due to portability concerns).
  8. */
  9. // Sample usage:
  10. //
  11. // C10_DEPRECATED void bad_func();
  12. // struct C10_DEPRECATED BadStruct {
  13. // ...
  14. // };
  15. // NB: __cplusplus doesn't work for MSVC, so for now MSVC always uses
  16. // the "__declspec(deprecated)" implementation and not the C++14
  17. // "[[deprecated]]" attribute. We tried enabling "[[deprecated]]" for C++14 on
  18. // MSVC, but ran into issues with some older MSVC versions.
  19. #if (defined(__cplusplus) && __cplusplus >= 201402L)
  20. #define C10_DEPRECATED [[deprecated]]
  21. #define C10_DEPRECATED_MESSAGE(message) [[deprecated(message)]]
  22. #elif defined(__GNUC__)
  23. #define C10_DEPRECATED __attribute__((deprecated))
  24. // TODO Is there some way to implement this?
  25. #define C10_DEPRECATED_MESSAGE(message) __attribute__((deprecated))
  26. #elif defined(_MSC_VER)
  27. #define C10_DEPRECATED __declspec(deprecated)
  28. #define C10_DEPRECATED_MESSAGE(message) __declspec(deprecated(message))
  29. #else
  30. #warning "You need to implement C10_DEPRECATED for this compiler"
  31. #define C10_DEPRECATED
  32. #endif
  33. // Sample usage:
  34. //
  35. // C10_DEFINE_DEPRECATED_USING(BadType, int)
  36. //
  37. // which is the portable version of
  38. //
  39. // using BadType [[deprecated]] = int;
  40. // technically [[deprecated]] syntax is from c++14 standard, but it works in
  41. // many compilers.
  42. #if defined(__has_cpp_attribute)
  43. #if __has_cpp_attribute(deprecated) && !defined(__CUDACC__)
  44. #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
  45. using TypeName [[deprecated]] = TypeThingy;
  46. #endif
  47. #endif
  48. #if defined(_MSC_VER)
  49. #if defined(__CUDACC__)
  50. // neither [[deprecated]] nor __declspec(deprecated) work on nvcc on Windows;
  51. // you get the error:
  52. //
  53. // error: attribute does not apply to any entity
  54. //
  55. // So we just turn the macro off in this case.
  56. #if defined(C10_DEFINE_DEPRECATED_USING)
  57. #undef C10_DEFINE_DEPRECATED_USING
  58. #endif
  59. #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
  60. using TypeName = TypeThingy;
  61. #else
  62. // [[deprecated]] does work in windows without nvcc, though msc doesn't support
  63. // `__has_cpp_attribute` when c++14 is supported, otherwise
  64. // __declspec(deprecated) is used as the alternative.
  65. #ifndef C10_DEFINE_DEPRECATED_USING
  66. #if defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
  67. #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
  68. using TypeName [[deprecated]] = TypeThingy;
  69. #else
  70. #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
  71. using TypeName = __declspec(deprecated) TypeThingy;
  72. #endif
  73. #endif
  74. #endif
  75. #endif
  76. #if !defined(C10_DEFINE_DEPRECATED_USING) && defined(__GNUC__)
  77. // nvcc has a bug where it doesn't understand __attribute__((deprecated))
  78. // declarations even when the host compiler supports it. We'll only use this gcc
  79. // attribute when not cuda, and when using a GCC compiler that doesn't support
  80. // the c++14 syntax we checked for above (available in __GNUC__ >= 5)
  81. #if !defined(__CUDACC__)
  82. #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
  83. using TypeName __attribute__((deprecated)) = TypeThingy;
  84. #else
  85. // using cuda + gcc < 5, neither deprecated syntax is available so turning off.
  86. #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
  87. using TypeName = TypeThingy;
  88. #endif
  89. #endif
  90. #if !defined(C10_DEFINE_DEPRECATED_USING)
  91. #warning "You need to implement C10_DEFINE_DEPRECATED_USING for this compiler"
  92. #define C10_DEFINE_DEPRECATED_USING
  93. #endif