compiler_specific.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_COMPILER_SPECIFIC_H_
  5. #define BASE_COMPILER_SPECIFIC_H_
  6. #include "build/build_config.h"
  7. #if defined(COMPILER_MSVC) && !defined(__clang__)
  8. #error "Only clang-cl is supported on Windows, see https://crbug.com/988071"
  9. #endif
  10. // Annotate a variable indicating it's ok if the variable is not used.
  11. // (Typically used to silence a compiler warning when the assignment
  12. // is important for some other reason.)
  13. // Use like:
  14. // int x = ...;
  15. // ALLOW_UNUSED_LOCAL(x);
  16. #define ALLOW_UNUSED_LOCAL(x) (void)x
  17. // Annotate a typedef or function indicating it's ok if it's not used.
  18. // Use like:
  19. // typedef Foo Bar ALLOW_UNUSED_TYPE;
  20. #if defined(COMPILER_GCC) || defined(__clang__)
  21. #define ALLOW_UNUSED_TYPE __attribute__((unused))
  22. #else
  23. #define ALLOW_UNUSED_TYPE
  24. #endif
  25. // Annotate a function indicating it should not be inlined.
  26. // Use like:
  27. // NOINLINE void DoStuff() { ... }
  28. #if defined(COMPILER_GCC)
  29. #define NOINLINE __attribute__((noinline))
  30. #elif defined(COMPILER_MSVC)
  31. #define NOINLINE __declspec(noinline)
  32. #else
  33. #define NOINLINE
  34. #endif
  35. #if defined(COMPILER_GCC) && defined(NDEBUG)
  36. #define ALWAYS_INLINE inline __attribute__((__always_inline__))
  37. #elif defined(COMPILER_MSVC) && defined(NDEBUG)
  38. #define ALWAYS_INLINE __forceinline
  39. #else
  40. #define ALWAYS_INLINE inline
  41. #endif
  42. // Annotate a function indicating it should never be tail called. Useful to make
  43. // sure callers of the annotated function are never omitted from call-stacks.
  44. // To provide the complementary behavior (prevent the annotated function from
  45. // being omitted) look at NOINLINE. Also note that this doesn't prevent code
  46. // folding of multiple identical caller functions into a single signature. To
  47. // prevent code folding, see base::debug::Alias.
  48. // Use like:
  49. // void NOT_TAIL_CALLED FooBar();
  50. #if defined(__clang__) && __has_attribute(not_tail_called)
  51. #define NOT_TAIL_CALLED __attribute__((not_tail_called))
  52. #else
  53. #define NOT_TAIL_CALLED
  54. #endif
  55. // Specify memory alignment for structs, classes, etc.
  56. // Use like:
  57. // class ALIGNAS(16) MyClass { ... }
  58. // ALIGNAS(16) int array[4];
  59. //
  60. // In most places you can use the C++11 keyword "alignas", which is preferred.
  61. //
  62. // But compilers have trouble mixing __attribute__((...)) syntax with
  63. // alignas(...) syntax.
  64. //
  65. // Doesn't work in clang or gcc:
  66. // struct alignas(16) __attribute__((packed)) S { char c; };
  67. // Works in clang but not gcc:
  68. // struct __attribute__((packed)) alignas(16) S2 { char c; };
  69. // Works in clang and gcc:
  70. // struct alignas(16) S3 { char c; } __attribute__((packed));
  71. //
  72. // There are also some attributes that must be specified *before* a class
  73. // definition: visibility (used for exporting functions/classes) is one of
  74. // these attributes. This means that it is not possible to use alignas() with a
  75. // class that is marked as exported.
  76. #if defined(COMPILER_MSVC)
  77. #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
  78. #elif defined(COMPILER_GCC)
  79. #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
  80. #endif
  81. // Annotate a function indicating the caller must examine the return value.
  82. // Use like:
  83. // int foo() WARN_UNUSED_RESULT;
  84. // To explicitly ignore a result, see |ignore_result()| in base/macros.h.
  85. #undef WARN_UNUSED_RESULT
  86. #if defined(COMPILER_GCC) || defined(__clang__)
  87. #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  88. #else
  89. #define WARN_UNUSED_RESULT
  90. #endif
  91. // Tell the compiler a function is using a printf-style format string.
  92. // |format_param| is the one-based index of the format string parameter;
  93. // |dots_param| is the one-based index of the "..." parameter.
  94. // For v*printf functions (which take a va_list), pass 0 for dots_param.
  95. // (This is undocumented but matches what the system C headers do.)
  96. // For member functions, the implicit this parameter counts as index 1.
  97. #if defined(COMPILER_GCC) || defined(__clang__)
  98. #define PRINTF_FORMAT(format_param, dots_param) \
  99. __attribute__((format(printf, format_param, dots_param)))
  100. #else
  101. #define PRINTF_FORMAT(format_param, dots_param)
  102. #endif
  103. // WPRINTF_FORMAT is the same, but for wide format strings.
  104. // This doesn't appear to yet be implemented in any compiler.
  105. // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
  106. #define WPRINTF_FORMAT(format_param, dots_param)
  107. // If available, it would look like:
  108. // __attribute__((format(wprintf, format_param, dots_param)))
  109. // Sanitizers annotations.
  110. #if defined(__has_attribute)
  111. #if __has_attribute(no_sanitize)
  112. #define NO_SANITIZE(what) __attribute__((no_sanitize(what)))
  113. #endif
  114. #endif
  115. #if !defined(NO_SANITIZE)
  116. #define NO_SANITIZE(what)
  117. #endif
  118. // MemorySanitizer annotations.
  119. #if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
  120. #include <sanitizer/msan_interface.h>
  121. // Mark a memory region fully initialized.
  122. // Use this to annotate code that deliberately reads uninitialized data, for
  123. // example a GC scavenging root set pointers from the stack.
  124. #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
  125. // Check a memory region for initializedness, as if it was being used here.
  126. // If any bits are uninitialized, crash with an MSan report.
  127. // Use this to sanitize data which MSan won't be able to track, e.g. before
  128. // passing data to another process via shared memory.
  129. #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
  130. __msan_check_mem_is_initialized(p, size)
  131. #else // MEMORY_SANITIZER
  132. #define MSAN_UNPOISON(p, size)
  133. #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
  134. #endif // MEMORY_SANITIZER
  135. // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons.
  136. #if !defined(DISABLE_CFI_PERF)
  137. #if defined(__clang__) && defined(OFFICIAL_BUILD)
  138. #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi")))
  139. #else
  140. #define DISABLE_CFI_PERF
  141. #endif
  142. #endif
  143. // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks.
  144. #if !defined(DISABLE_CFI_ICALL)
  145. #if defined(OS_WIN)
  146. // Windows also needs __declspec(guard(nocf)).
  147. #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf))
  148. #else
  149. #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall")
  150. #endif
  151. #endif
  152. #if !defined(DISABLE_CFI_ICALL)
  153. #define DISABLE_CFI_ICALL
  154. #endif
  155. // Macro useful for writing cross-platform function pointers.
  156. #if !defined(CDECL)
  157. #if defined(OS_WIN)
  158. #define CDECL __cdecl
  159. #else // defined(OS_WIN)
  160. #define CDECL
  161. #endif // defined(OS_WIN)
  162. #endif // !defined(CDECL)
  163. // Macro for hinting that an expression is likely to be false.
  164. #if !defined(UNLIKELY)
  165. #if defined(COMPILER_GCC) || defined(__clang__)
  166. #define UNLIKELY(x) __builtin_expect(!!(x), 0)
  167. #else
  168. #define UNLIKELY(x) (x)
  169. #endif // defined(COMPILER_GCC)
  170. #endif // !defined(UNLIKELY)
  171. #if !defined(LIKELY)
  172. #if defined(COMPILER_GCC) || defined(__clang__)
  173. #define LIKELY(x) __builtin_expect(!!(x), 1)
  174. #else
  175. #define LIKELY(x) (x)
  176. #endif // defined(COMPILER_GCC)
  177. #endif // !defined(LIKELY)
  178. // Compiler feature-detection.
  179. // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
  180. #if defined(__has_feature)
  181. #define HAS_FEATURE(FEATURE) __has_feature(FEATURE)
  182. #else
  183. #define HAS_FEATURE(FEATURE) 0
  184. #endif
  185. // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
  186. #if defined(__clang__)
  187. #define FALLTHROUGH [[clang::fallthrough]]
  188. #else
  189. #define FALLTHROUGH
  190. #endif
  191. #if defined(COMPILER_GCC)
  192. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  193. #elif defined(COMPILER_MSVC)
  194. #define PRETTY_FUNCTION __FUNCSIG__
  195. #else
  196. // See https://en.cppreference.com/w/c/language/function_definition#func
  197. #define PRETTY_FUNCTION __func__
  198. #endif
  199. #if !defined(CPU_ARM_NEON)
  200. #if defined(__arm__)
  201. #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \
  202. !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID)
  203. #error Chromium does not support middle endian architecture
  204. #endif
  205. #if defined(__ARM_NEON__)
  206. #define CPU_ARM_NEON 1
  207. #endif
  208. #endif // defined(__arm__)
  209. #endif // !defined(CPU_ARM_NEON)
  210. #if !defined(HAVE_MIPS_MSA_INTRINSICS)
  211. #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
  212. #define HAVE_MIPS_MSA_INTRINSICS 1
  213. #endif
  214. #endif
  215. #if defined(__clang__) && __has_attribute(uninitialized)
  216. // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for
  217. // the specified variable.
  218. // Library-wide alternative is
  219. // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn
  220. // file.
  221. //
  222. // See "init_stack_vars" in build/config/compiler/BUILD.gn and
  223. // http://crbug.com/977230
  224. // "init_stack_vars" is enabled for non-official builds and we hope to enable it
  225. // in official build in 2020 as well. The flag writes fixed pattern into
  226. // uninitialized parts of all local variables. In rare cases such initialization
  227. // is undesirable and attribute can be used:
  228. // 1. Degraded performance
  229. // In most cases compiler is able to remove additional stores. E.g. if memory is
  230. // never accessed or properly initialized later. Preserved stores mostly will
  231. // not affect program performance. However if compiler failed on some
  232. // performance critical code we can get a visible regression in a benchmark.
  233. // 2. memset, memcpy calls
  234. // Compiler may replaces some memory writes with memset or memcpy calls. This is
  235. // not -ftrivial-auto-var-init specific, but it can happen more likely with the
  236. // flag. It can be a problem if code is not linked with C run-time library.
  237. //
  238. // Note: The flag is security risk mitigation feature. So in future the
  239. // attribute uses should be avoided when possible. However to enable this
  240. // mitigation on the most of the code we need to be less strict now and minimize
  241. // number of exceptions later. So if in doubt feel free to use attribute, but
  242. // please document the problem for someone who is going to cleanup it later.
  243. // E.g. platform, bot, benchmark or test name in patch description or next to
  244. // the attribute.
  245. #define STACK_UNINITIALIZED __attribute__((uninitialized))
  246. #else
  247. #define STACK_UNINITIALIZED
  248. #endif
  249. // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints
  250. // to Clang which control what code paths are statically analyzed,
  251. // and is meant to be used in conjunction with assert & assert-like functions.
  252. // The expression is passed straight through if analysis isn't enabled.
  253. //
  254. // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current
  255. // codepath and any other branching codepaths that might follow.
  256. #if defined(__clang_analyzer__)
  257. inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
  258. return false;
  259. }
  260. inline constexpr bool AnalyzerAssumeTrue(bool arg) {
  261. // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is
  262. // false.
  263. return arg || AnalyzerNoReturn();
  264. }
  265. #define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg))
  266. #define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn())
  267. #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var);
  268. #else // !defined(__clang_analyzer__)
  269. #define ANALYZER_ASSUME_TRUE(arg) (arg)
  270. #define ANALYZER_SKIP_THIS_PATH()
  271. #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var);
  272. #endif // defined(__clang_analyzer__)
  273. #endif // BASE_COMPILER_SPECIFIC_H_