CppAttributes.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * GCC compatible wrappers around clang attributes.
  18. *
  19. * @author Dominik Gabi
  20. */
  21. #pragma once
  22. #ifndef __has_attribute
  23. #define FOLLY_HAS_ATTRIBUTE(x) 0
  24. #else
  25. #define FOLLY_HAS_ATTRIBUTE(x) __has_attribute(x)
  26. #endif
  27. #ifndef __has_cpp_attribute
  28. #define FOLLY_HAS_CPP_ATTRIBUTE(x) 0
  29. #else
  30. #define FOLLY_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
  31. #endif
  32. #ifndef __has_extension
  33. #define FOLLY_HAS_EXTENSION(x) 0
  34. #else
  35. #define FOLLY_HAS_EXTENSION(x) __has_extension(x)
  36. #endif
  37. /**
  38. * Fallthrough to indicate that `break` was left out on purpose in a switch
  39. * statement, e.g.
  40. *
  41. * switch (n) {
  42. * case 22:
  43. * case 33: // no warning: no statements between case labels
  44. * f();
  45. * case 44: // warning: unannotated fall-through
  46. * g();
  47. * FOLLY_FALLTHROUGH; // no warning: annotated fall-through
  48. * }
  49. */
  50. #if FOLLY_HAS_CPP_ATTRIBUTE(fallthrough)
  51. #define FOLLY_FALLTHROUGH [[fallthrough]]
  52. #elif FOLLY_HAS_CPP_ATTRIBUTE(clang::fallthrough)
  53. #define FOLLY_FALLTHROUGH [[clang::fallthrough]]
  54. #elif FOLLY_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
  55. #define FOLLY_FALLTHROUGH [[gnu::fallthrough]]
  56. #else
  57. #define FOLLY_FALLTHROUGH
  58. #endif
  59. /**
  60. * Maybe_unused indicates that a function, variable or parameter might or
  61. * might not be used, e.g.
  62. *
  63. * int foo(FOLLY_MAYBE_UNUSED int x) {
  64. * #ifdef USE_X
  65. * return x;
  66. * #else
  67. * return 0;
  68. * #endif
  69. * }
  70. */
  71. #if FOLLY_HAS_CPP_ATTRIBUTE(maybe_unused)
  72. #define FOLLY_MAYBE_UNUSED [[maybe_unused]]
  73. #elif FOLLY_HAS_ATTRIBUTE(__unused__) || __GNUC__
  74. #define FOLLY_MAYBE_UNUSED __attribute__((__unused__))
  75. #else
  76. #define FOLLY_MAYBE_UNUSED
  77. #endif
  78. /**
  79. * Nullable indicates that a return value or a parameter may be a `nullptr`,
  80. * e.g.
  81. *
  82. * int* FOLLY_NULLABLE foo(int* a, int* FOLLY_NULLABLE b) {
  83. * if (*a > 0) { // safe dereference
  84. * return nullptr;
  85. * }
  86. * if (*b < 0) { // unsafe dereference
  87. * return *a;
  88. * }
  89. * if (b != nullptr && *b == 1) { // safe checked dereference
  90. * return new int(1);
  91. * }
  92. * return nullptr;
  93. * }
  94. */
  95. #if FOLLY_HAS_EXTENSION(nullability)
  96. #define FOLLY_NULLABLE _Nullable
  97. #define FOLLY_NONNULL _Nonnull
  98. #else
  99. #define FOLLY_NULLABLE
  100. #define FOLLY_NONNULL
  101. #endif
  102. /**
  103. * "Cold" indicates to the compiler that a function is only expected to be
  104. * called from unlikely code paths. It can affect decisions made by the
  105. * optimizer both when processing the function body and when analyzing
  106. * call-sites.
  107. */
  108. #if __GNUC__
  109. #define FOLLY_COLD __attribute__((__cold__))
  110. #else
  111. #define FOLLY_COLD
  112. #endif