Attributes.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* Implementations of various class and method modifier attributes. */
  7. #ifndef mozilla_Attributes_h
  8. #define mozilla_Attributes_h
  9. #include "mozilla/Compiler.h"
  10. /*
  11. * MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the
  12. * method decorated with it must be inlined, even if the compiler thinks
  13. * otherwise. This is only a (much) stronger version of the inline hint:
  14. * compilers are not guaranteed to respect it (although they're much more likely
  15. * to do so).
  16. *
  17. * The MOZ_ALWAYS_INLINE_EVEN_DEBUG macro is yet stronger. It tells the
  18. * compiler to inline even in DEBUG builds. It should be used very rarely.
  19. */
  20. #if defined(_MSC_VER)
  21. # define MOZ_ALWAYS_INLINE_EVEN_DEBUG __forceinline
  22. #elif defined(__GNUC__)
  23. # define MOZ_ALWAYS_INLINE_EVEN_DEBUG __attribute__((always_inline)) inline
  24. #else
  25. # define MOZ_ALWAYS_INLINE_EVEN_DEBUG inline
  26. #endif
  27. #if !defined(DEBUG)
  28. # define MOZ_ALWAYS_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG
  29. #elif defined(_MSC_VER) && !defined(__cplusplus)
  30. # define MOZ_ALWAYS_INLINE __inline
  31. #else
  32. # define MOZ_ALWAYS_INLINE inline
  33. #endif
  34. #if defined(_MSC_VER)
  35. /*
  36. * g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality
  37. * without warnings (functionality used by the macros below). These modes are
  38. * detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more
  39. * standardly, by checking whether __cplusplus has a C++11 or greater value.
  40. * Current versions of g++ do not correctly set __cplusplus, so we check both
  41. * for forward compatibility.
  42. */
  43. # define MOZ_HAVE_NEVER_INLINE __declspec(noinline)
  44. # define MOZ_HAVE_NORETURN __declspec(noreturn)
  45. #elif defined(__clang__)
  46. /*
  47. * Per Clang documentation, "Note that marketing version numbers should not
  48. * be used to check for language features, as different vendors use different
  49. * numbering schemes. Instead, use the feature checking macros."
  50. */
  51. # ifndef __has_extension
  52. # define __has_extension __has_feature /* compatibility, for older versions of clang */
  53. # endif
  54. # if __has_attribute(noinline)
  55. # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
  56. # endif
  57. # if __has_attribute(noreturn)
  58. # define MOZ_HAVE_NORETURN __attribute__((noreturn))
  59. # endif
  60. #elif defined(__GNUC__)
  61. # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
  62. # define MOZ_HAVE_NORETURN __attribute__((noreturn))
  63. # define MOZ_HAVE_NORETURN_PTR __attribute__((noreturn))
  64. #endif
  65. /*
  66. * When built with clang analyzer (a.k.a scan-build), define MOZ_HAVE_NORETURN
  67. * to mark some false positives
  68. */
  69. #ifdef __clang_analyzer__
  70. # if __has_extension(attribute_analyzer_noreturn)
  71. # define MOZ_HAVE_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
  72. # endif
  73. #endif
  74. /*
  75. * MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the
  76. * method decorated with it must never be inlined, even if the compiler would
  77. * otherwise choose to inline the method. Compilers aren't absolutely
  78. * guaranteed to support this, but most do.
  79. */
  80. #if defined(MOZ_HAVE_NEVER_INLINE)
  81. # define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE
  82. #else
  83. # define MOZ_NEVER_INLINE /* no support */
  84. #endif
  85. /*
  86. * MOZ_NORETURN, specified at the start of a function declaration, indicates
  87. * that the given function does not return. (The function definition does not
  88. * need to be annotated.)
  89. *
  90. * MOZ_NORETURN void abort(const char* msg);
  91. *
  92. * This modifier permits the compiler to optimize code assuming a call to such a
  93. * function will never return. It also enables the compiler to avoid spurious
  94. * warnings about not initializing variables, or about any other seemingly-dodgy
  95. * operations performed after the function returns.
  96. *
  97. * There are two variants. The GCC version of NORETURN may be applied to a
  98. * function pointer, while for MSVC it may not.
  99. *
  100. * This modifier does not affect the corresponding function's linking behavior.
  101. */
  102. #if defined(MOZ_HAVE_NORETURN)
  103. # define MOZ_NORETURN MOZ_HAVE_NORETURN
  104. #else
  105. # define MOZ_NORETURN /* no support */
  106. #endif
  107. #if defined(MOZ_HAVE_NORETURN_PTR)
  108. # define MOZ_NORETURN_PTR MOZ_HAVE_NORETURN_PTR
  109. #else
  110. # define MOZ_NORETURN_PTR /* no support */
  111. #endif
  112. /**
  113. * MOZ_COLD tells the compiler that a function is "cold", meaning infrequently
  114. * executed. This may lead it to optimize for size more aggressively than speed,
  115. * or to allocate the body of the function in a distant part of the text segment
  116. * to help keep it from taking up unnecessary icache when it isn't in use.
  117. *
  118. * Place this attribute at the very beginning of a function definition. For
  119. * example, write
  120. *
  121. * MOZ_COLD int foo();
  122. *
  123. * or
  124. *
  125. * MOZ_COLD int foo() { return 42; }
  126. */
  127. #if defined(__GNUC__) || defined(__clang__)
  128. # define MOZ_COLD __attribute__ ((cold))
  129. #else
  130. # define MOZ_COLD
  131. #endif
  132. /**
  133. * MOZ_NONNULL tells the compiler that some of the arguments to a function are
  134. * known to be non-null. The arguments are a list of 1-based argument indexes
  135. * identifying arguments which are known to be non-null.
  136. *
  137. * Place this attribute at the very beginning of a function definition. For
  138. * example, write
  139. *
  140. * MOZ_NONNULL(1, 2) int foo(char *p, char *q);
  141. */
  142. #if defined(__GNUC__) || defined(__clang__)
  143. # define MOZ_NONNULL(...) __attribute__ ((nonnull(__VA_ARGS__)))
  144. #else
  145. # define MOZ_NONNULL(...)
  146. #endif
  147. /**
  148. * MOZ_NONNULL_RETURN tells the compiler that the function's return value is
  149. * guaranteed to be a non-null pointer, which may enable the compiler to
  150. * optimize better at call sites.
  151. *
  152. * Place this attribute at the end of a function declaration. For example,
  153. *
  154. * char* foo(char *p, char *q) MOZ_NONNULL_RETURN;
  155. */
  156. #if defined(__GNUC__) || defined(__clang__)
  157. # define MOZ_NONNULL_RETURN __attribute__ ((returns_nonnull))
  158. #else
  159. # define MOZ_NONNULL_RETURN
  160. #endif
  161. /*
  162. * MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS, specified at the end of a function
  163. * declaration, indicates that for the purposes of static analysis, this
  164. * function does not return. (The function definition does not need to be
  165. * annotated.)
  166. *
  167. * MOZ_ReportCrash(const char* s, const char* file, int ln)
  168. * MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
  169. *
  170. * Some static analyzers, like scan-build from clang, can use this information
  171. * to eliminate false positives. From the upstream documentation of scan-build:
  172. * "This attribute is useful for annotating assertion handlers that actually
  173. * can return, but for the purpose of using the analyzer we want to pretend
  174. * that such functions do not return."
  175. *
  176. */
  177. #if defined(MOZ_HAVE_ANALYZER_NORETURN)
  178. # define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS MOZ_HAVE_ANALYZER_NORETURN
  179. #else
  180. # define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS /* no support */
  181. #endif
  182. /*
  183. * MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
  184. * instrumentation shipped with Clang and GCC) to not instrument the annotated
  185. * function. Furthermore, it will prevent the compiler from inlining the
  186. * function because inlining currently breaks the blacklisting mechanism of
  187. * AddressSanitizer.
  188. */
  189. #if defined(__has_feature)
  190. # if __has_feature(address_sanitizer)
  191. # define MOZ_HAVE_ASAN_BLACKLIST
  192. # endif
  193. #elif defined(__GNUC__)
  194. # if defined(__SANITIZE_ADDRESS__)
  195. # define MOZ_HAVE_ASAN_BLACKLIST
  196. # endif
  197. #endif
  198. #if defined(MOZ_HAVE_ASAN_BLACKLIST)
  199. # define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_address))
  200. #else
  201. # define MOZ_ASAN_BLACKLIST /* nothing */
  202. #endif
  203. /*
  204. * MOZ_TSAN_BLACKLIST is a macro to tell ThreadSanitizer (a compile-time
  205. * instrumentation shipped with Clang) to not instrument the annotated function.
  206. * Furthermore, it will prevent the compiler from inlining the function because
  207. * inlining currently breaks the blacklisting mechanism of ThreadSanitizer.
  208. */
  209. #if defined(__has_feature)
  210. # if __has_feature(thread_sanitizer)
  211. # define MOZ_TSAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_thread))
  212. # else
  213. # define MOZ_TSAN_BLACKLIST /* nothing */
  214. # endif
  215. #else
  216. # define MOZ_TSAN_BLACKLIST /* nothing */
  217. #endif
  218. /*
  219. * The MOZ_NO_SANITIZE_* family of macros is an annotation based on a more recently
  220. * introduced Clang feature that allows disabling various sanitizer features for
  221. * the particular function, including those from UndefinedBehaviorSanitizer.
  222. */
  223. #if defined(__has_attribute)
  224. # if __has_attribute(no_sanitize)
  225. # define MOZ_HAVE_NO_SANITIZE_ATTR
  226. # endif
  227. #endif
  228. #if defined(MOZ_HAVE_NO_SANITIZE_ATTR)
  229. # define MOZ_NO_SANITIZE_UINT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
  230. # define MOZ_NO_SANITIZE_INT_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
  231. #else
  232. # define MOZ_NO_SANITIZE_UINT_OVERFLOW /* nothing */
  233. # define MOZ_NO_SANITIZE_INT_OVERFLOW /* nothing */
  234. #endif
  235. #undef MOZ_HAVE_NO_SANITIZE_ATTR
  236. /**
  237. * MOZ_ALLOCATOR tells the compiler that the function it marks returns either a
  238. * "fresh", "pointer-free" block of memory, or nullptr. "Fresh" means that the
  239. * block is not pointed to by any other reachable pointer in the program.
  240. * "Pointer-free" means that the block contains no pointers to any valid object
  241. * in the program. It may be initialized with other (non-pointer) values.
  242. *
  243. * Placing this attribute on appropriate functions helps GCC analyze pointer
  244. * aliasing more accurately in their callers.
  245. *
  246. * GCC warns if a caller ignores the value returned by a function marked with
  247. * MOZ_ALLOCATOR: it is hard to imagine cases where dropping the value returned
  248. * by a function that meets the criteria above would be intentional.
  249. *
  250. * Place this attribute after the argument list and 'this' qualifiers of a
  251. * function definition. For example, write
  252. *
  253. * void *my_allocator(size_t) MOZ_ALLOCATOR;
  254. *
  255. * or
  256. *
  257. * void *my_allocator(size_t bytes) MOZ_ALLOCATOR { ... }
  258. */
  259. #if defined(__GNUC__) || defined(__clang__)
  260. # define MOZ_ALLOCATOR __attribute__ ((malloc, warn_unused_result))
  261. #else
  262. # define MOZ_ALLOCATOR
  263. #endif
  264. /**
  265. * MOZ_MUST_USE tells the compiler to emit a warning if a function's
  266. * return value is not used by the caller.
  267. *
  268. * Place this attribute at the very beginning of a function declaration. For
  269. * example, write
  270. *
  271. * MOZ_MUST_USE int foo();
  272. * or
  273. * MOZ_MUST_USE int foo() { return 42; }
  274. *
  275. * MOZ_MUST_USE is most appropriate for functions where the return value is
  276. * some kind of success/failure indicator -- often |nsresult|, |bool| or |int|
  277. * -- because these functions are most commonly the ones that have missing
  278. * checks. There are three cases of note.
  279. *
  280. * - Fallible functions whose return values should always be checked. For
  281. * example, a function that opens a file should always be checked because any
  282. * subsequent operations on the file will fail if opening it fails. Such
  283. * functions should be given a MOZ_MUST_USE annotation.
  284. *
  285. * - Fallible functions whose return value need not always be checked. For
  286. * example, a function that closes a file might not be checked because it's
  287. * common that no further operations would be performed on the file. Such
  288. * functions do not need a MOZ_MUST_USE annotation.
  289. *
  290. * - Infallible functions, i.e. ones that always return a value indicating
  291. * success. These do not need a MOZ_MUST_USE annotation. Ideally, they would
  292. * be converted to not return a success/failure indicator, though sometimes
  293. * interface constraints prevent this.
  294. */
  295. #if defined(__GNUC__) || defined(__clang__)
  296. # define MOZ_MUST_USE __attribute__ ((warn_unused_result))
  297. #else
  298. # define MOZ_MUST_USE
  299. #endif
  300. /**
  301. * MOZ_MAYBE_UNUSED suppresses compiler warnings about functions that are
  302. * never called (in this build configuration, at least).
  303. *
  304. * Place this attribute at the very beginning of a function declaration. For
  305. * example, write
  306. *
  307. * MOZ_MAYBE_UNUSED int foo();
  308. *
  309. * or
  310. *
  311. * MOZ_MAYBE_UNUSED int foo() { return 42; }
  312. */
  313. #if defined(__GNUC__) || defined(__clang__)
  314. # define MOZ_MAYBE_UNUSED __attribute__ ((__unused__))
  315. #elif defined(_MSC_VER)
  316. # define MOZ_MAYBE_UNUSED __pragma(warning(suppress:4505))
  317. #else
  318. # define MOZ_MAYBE_UNUSED
  319. #endif
  320. #ifdef __cplusplus
  321. /**
  322. * MOZ_FALLTHROUGH is an annotation to suppress compiler warnings about switch
  323. * cases that fall through without a break or return statement. MOZ_FALLTHROUGH
  324. * is only needed on cases that have code.
  325. *
  326. * MOZ_FALLTHROUGH_ASSERT is an annotation to suppress compiler warnings about
  327. * switch cases that MOZ_ASSERT(false) (or its alias MOZ_ASSERT_UNREACHABLE) in
  328. * debug builds, but intentionally fall through in release builds. See comment
  329. * in Assertions.h for more details.
  330. *
  331. * switch (foo) {
  332. * case 1: // These cases have no code. No fallthrough annotations are needed.
  333. * case 2:
  334. * case 3: // This case has code, so a fallthrough annotation is needed!
  335. * foo++;
  336. * MOZ_FALLTHROUGH;
  337. * case 4:
  338. * return foo;
  339. *
  340. * default:
  341. * // This case asserts in debug builds, falls through in release.
  342. * MOZ_FALLTHROUGH_ASSERT("Unexpected foo value?!");
  343. * case 5:
  344. * return 5;
  345. * }
  346. */
  347. #ifndef __has_cpp_attribute
  348. # define __has_cpp_attribute(x) 0
  349. #endif
  350. #if __has_cpp_attribute(clang::fallthrough)
  351. # define MOZ_FALLTHROUGH [[clang::fallthrough]]
  352. #elif __has_cpp_attribute(gnu::fallthrough)
  353. # define MOZ_FALLTHROUGH [[gnu::fallthrough]]
  354. #elif defined(_MSC_VER)
  355. /*
  356. * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
  357. * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
  358. */
  359. # include <sal.h>
  360. # define MOZ_FALLTHROUGH __fallthrough
  361. #else
  362. # define MOZ_FALLTHROUGH /* FALLTHROUGH */
  363. #endif
  364. /*
  365. * The following macros are attributes that support the static analysis plugin
  366. * included with Mozilla, and will be implemented (when such support is enabled)
  367. * as C++11 attributes. Since such attributes are legal pretty much everywhere
  368. * and have subtly different semantics depending on their placement, the
  369. * following is a guide on where to place the attributes.
  370. *
  371. * Attributes that apply to a struct or class precede the name of the class:
  372. * (Note that this is different from the placement of final for classes!)
  373. *
  374. * class MOZ_CLASS_ATTRIBUTE SomeClass {};
  375. *
  376. * Attributes that apply to functions follow the parentheses and const
  377. * qualifiers but precede final, override and the function body:
  378. *
  379. * void DeclaredFunction() MOZ_FUNCTION_ATTRIBUTE;
  380. * void SomeFunction() MOZ_FUNCTION_ATTRIBUTE {}
  381. * void PureFunction() const MOZ_FUNCTION_ATTRIBUTE = 0;
  382. * void OverriddenFunction() MOZ_FUNCTION_ATTIRBUTE override;
  383. *
  384. * Attributes that apply to variables or parameters follow the variable's name:
  385. *
  386. * int variable MOZ_VARIABLE_ATTRIBUTE;
  387. *
  388. * Attributes that apply to types follow the type name:
  389. *
  390. * typedef int MOZ_TYPE_ATTRIBUTE MagicInt;
  391. * int MOZ_TYPE_ATTRIBUTE someVariable;
  392. * int* MOZ_TYPE_ATTRIBUTE magicPtrInt;
  393. * int MOZ_TYPE_ATTRIBUTE* ptrToMagicInt;
  394. *
  395. * Attributes that apply to statements precede the statement:
  396. *
  397. * MOZ_IF_ATTRIBUTE if (x == 0)
  398. * MOZ_DO_ATTRIBUTE do { } while (0);
  399. *
  400. * Attributes that apply to labels precede the label:
  401. *
  402. * MOZ_LABEL_ATTRIBUTE target:
  403. * goto target;
  404. * MOZ_CASE_ATTRIBUTE case 5:
  405. * MOZ_DEFAULT_ATTRIBUTE default:
  406. *
  407. * The static analyses that are performed by the plugin are as follows:
  408. *
  409. * MOZ_CAN_RUN_SCRIPT: Applies to functions which can run script. Callers of
  410. * this function must also be marked as MOZ_CAN_RUN_SCRIPT, and all refcounted
  411. * arguments must be strongly held in the caller.
  412. * MOZ_MUST_OVERRIDE: Applies to all C++ member functions. All immediate
  413. * subclasses must provide an exact override of this method; if a subclass
  414. * does not override this method, the compiler will emit an error. This
  415. * attribute is not limited to virtual methods, so if it is applied to a
  416. * nonvirtual method and the subclass does not provide an equivalent
  417. * definition, the compiler will emit an error.
  418. * MOZ_STACK_CLASS: Applies to all classes. Any class with this annotation is
  419. * expected to live on the stack, so it is a compile-time error to use it, or
  420. * an array of such objects, as a global or static variable, or as the type of
  421. * a new expression (unless placement new is being used). If a member of
  422. * another class uses this class, or if another class inherits from this
  423. * class, then it is considered to be a stack class as well, although this
  424. * attribute need not be provided in such cases.
  425. * MOZ_NONHEAP_CLASS: Applies to all classes. Any class with this annotation is
  426. * expected to live on the stack or in static storage, so it is a compile-time
  427. * error to use it, or an array of such objects, as the type of a new
  428. * expression. If a member of another class uses this class, or if another
  429. * class inherits from this class, then it is considered to be a non-heap class
  430. * as well, although this attribute need not be provided in such cases.
  431. * MOZ_HEAP_CLASS: Applies to all classes. Any class with this annotation is
  432. * expected to live on the heap, so it is a compile-time error to use it, or
  433. * an array of such objects, as the type of a variable declaration, or as a
  434. * temporary object. If a member of another class uses this class, or if
  435. * another class inherits from this class, then it is considered to be a heap
  436. * class as well, although this attribute need not be provided in such cases.
  437. * MOZ_NON_TEMPORARY_CLASS: Applies to all classes. Any class with this
  438. * annotation is expected not to live in a temporary. If a member of another
  439. * class uses this class or if another class inherits from this class, then it
  440. * is considered to be a non-temporary class as well, although this attribute
  441. * need not be provided in such cases.
  442. * MOZ_RAII: Applies to all classes. Any class with this annotation is assumed
  443. * to be a RAII guard, which is expected to live on the stack in an automatic
  444. * allocation. It is prohibited from being allocated in a temporary, static
  445. * storage, or on the heap. This is a combination of MOZ_STACK_CLASS and
  446. * MOZ_NON_TEMPORARY_CLASS.
  447. * MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS: Applies to all classes that are
  448. * intended to prevent introducing static initializers. This attribute
  449. * currently makes it a compile-time error to instantiate these classes
  450. * anywhere other than at the global scope, or as a static member of a class.
  451. * In non-debug mode, it also prohibits non-trivial constructors and
  452. * destructors.
  453. * MOZ_TRIVIAL_CTOR_DTOR: Applies to all classes that must have both a trivial
  454. * or constexpr constructor and a trivial destructor. Setting this attribute
  455. * on a class makes it a compile-time error for that class to get a
  456. * non-trivial constructor or destructor for any reason.
  457. * MOZ_HEAP_ALLOCATOR: Applies to any function. This indicates that the return
  458. * value is allocated on the heap, and will as a result check such allocations
  459. * during MOZ_STACK_CLASS and MOZ_NONHEAP_CLASS annotation checking.
  460. * MOZ_IMPLICIT: Applies to constructors. Implicit conversion constructors
  461. * are disallowed by default unless they are marked as MOZ_IMPLICIT. This
  462. * attribute must be used for constructors which intend to provide implicit
  463. * conversions.
  464. * MOZ_IS_REFPTR: Applies to class declarations of ref pointer to mark them as
  465. * such for use with static-analysis.
  466. * A ref pointer is an object wrapping a pointer and automatically taking care
  467. * of its refcounting upon construction/destruction/transfer of ownership.
  468. * This annotation implies MOZ_IS_SMARTPTR_TO_REFCOUNTED.
  469. * MOZ_IS_SMARTPTR_TO_REFCOUNTED: Applies to class declarations of smart
  470. * pointers to ref counted classes to mark them as such for use with
  471. * static-analysis.
  472. * MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT: Applies to functions. Makes it a compile
  473. * time error to pass arithmetic expressions on variables to the function.
  474. * MOZ_OWNING_REF: Applies to declarations of pointers to reference counted
  475. * types. This attribute tells the compiler that the raw pointer is a strong
  476. * reference, where ownership through methods such as AddRef and Release is
  477. * managed manually. This can make the compiler ignore these pointers when
  478. * validating the usage of pointers otherwise.
  479. *
  480. * Example uses include owned pointers inside of unions, and pointers stored
  481. * in POD types where a using a smart pointer class would make the object
  482. * non-POD.
  483. * MOZ_NON_OWNING_REF: Applies to declarations of pointers to reference counted
  484. * types. This attribute tells the compiler that the raw pointer is a weak
  485. * reference, which is ensured to be valid by a guarantee that the reference
  486. * will be nulled before the pointer becomes invalid. This can make the compiler
  487. * ignore these pointers when validating the usage of pointers otherwise.
  488. *
  489. * Examples include an mOwner pointer, which is nulled by the owning class's
  490. * destructor, and is null-checked before dereferencing.
  491. * MOZ_UNSAFE_REF: Applies to declarations of pointers to reference counted types.
  492. * Occasionally there are non-owning references which are valid, but do not take
  493. * the form of a MOZ_NON_OWNING_REF. Their safety may be dependent on the behaviour
  494. * of API consumers. The string argument passed to this macro documents the safety
  495. * conditions. This can make the compiler ignore these pointers when validating
  496. * the usage of pointers elsewhere.
  497. *
  498. * Examples include an nsIAtom* member which is known at compile time to point to a
  499. * static atom which is valid throughout the lifetime of the program, or an API which
  500. * stores a pointer, but doesn't take ownership over it, instead requiring the API
  501. * consumer to correctly null the value before it becomes invalid.
  502. *
  503. * Use of this annotation is discouraged when a strong reference or one of the above
  504. * two annotations can be used instead.
  505. * MOZ_NO_ADDREF_RELEASE_ON_RETURN: Applies to function declarations. Makes it
  506. * a compile time error to call AddRef or Release on the return value of a
  507. * function. This is intended to be used with operator->() of our smart
  508. * pointer classes to ensure that the refcount of an object wrapped in a
  509. * smart pointer is not manipulated directly.
  510. * MOZ_MUST_USE_TYPE: Applies to type declarations. Makes it a compile time
  511. * error to not use the return value of a function which has this type. This
  512. * is intended to be used with types which it is an error to not use.
  513. * MOZ_NEEDS_NO_VTABLE_TYPE: Applies to template class declarations. Makes it
  514. * a compile time error to instantiate this template with a type parameter which
  515. * has a VTable.
  516. * MOZ_NON_MEMMOVABLE: Applies to class declarations for types that are not safe
  517. * to be moved in memory using memmove().
  518. * MOZ_NEEDS_MEMMOVABLE_TYPE: Applies to template class declarations where the
  519. * template arguments are required to be safe to move in memory using
  520. * memmove(). Passing MOZ_NON_MEMMOVABLE types to these templates is a
  521. * compile time error.
  522. * MOZ_NEEDS_MEMMOVABLE_MEMBERS: Applies to class declarations where each member
  523. * must be safe to move in memory using memmove(). MOZ_NON_MEMMOVABLE types
  524. * used in members of these classes are compile time errors.
  525. * MOZ_NO_DANGLING_ON_TEMPORARIES: Applies to method declarations which return
  526. * a pointer that is freed when the destructor of the class is called. This
  527. * prevents these methods from being called on temporaries of the class,
  528. * reducing risks of use-after-free.
  529. * This attribute cannot be applied to && methods.
  530. * In some cases, adding a deleted &&-qualified overload is too restrictive as
  531. * this method should still be callable as a non-escaping argument to another
  532. * function. This annotation can be used in those cases.
  533. * MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS: Applies to template class
  534. * declarations where an instance of the template should be considered, for
  535. * static analysis purposes, to inherit any type annotations (such as
  536. * MOZ_MUST_USE_TYPE and MOZ_STACK_CLASS) from its template arguments.
  537. * MOZ_INIT_OUTSIDE_CTOR: Applies to class member declarations. Occasionally
  538. * there are class members that are not initialized in the constructor,
  539. * but logic elsewhere in the class ensures they are initialized prior to use.
  540. * Using this attribute on a member disables the check that this member must be
  541. * initialized in constructors via list-initialization, in the constructor body,
  542. * or via functions called from the constructor body.
  543. * MOZ_IS_CLASS_INIT: Applies to class method declarations. Occasionally the
  544. * constructor doesn't initialize all of the member variables and another function
  545. * is used to initialize the rest. This marker is used to make the static analysis
  546. * tool aware that the marked function is part of the initialization process
  547. * and to include the marked function in the scan mechanism that determines witch
  548. * member variables still remain uninitialized.
  549. * MOZ_NON_PARAM: Applies to types. Makes it compile time error to use the type
  550. * in parameter without pointer or reference.
  551. * MOZ_NON_AUTOABLE: Applies to class declarations. Makes it a compile time error to
  552. * use `auto` in place of this type in variable declarations. This is intended to
  553. * be used with types which are intended to be implicitly constructed into other
  554. * other types before being assigned to variables.
  555. * MOZ_REQUIRED_BASE_METHOD: Applies to virtual class method declarations.
  556. * Sometimes derived classes override methods that need to be called by their
  557. * overridden counterparts. This marker indicates that the marked method must
  558. * be called by the method that it overrides.
  559. * MOZ_MUST_RETURN_FROM_CALLER: Applies to function or method declarations.
  560. * Callers of the annotated function/method must return from that function
  561. * within the calling block using an explicit `return` statement.
  562. * Only calls to Constructors, references to local and member variables,
  563. * and calls to functions or methods marked as MOZ_MAY_CALL_AFTER_MUST_RETURN
  564. * may be made after the MUST_RETURN_FROM_CALLER call.
  565. * MOZ_MAY_CALL_AFTER_MUST_RETURN: Applies to function or method declarations.
  566. * Calls to these methods may be made in functions after calls a
  567. * MOZ_MUST_RETURN_FROM_CALLER function or method.
  568. */
  569. #ifdef MOZ_CLANG_PLUGIN
  570. # define MOZ_CAN_RUN_SCRIPT __attribute__((annotate("moz_can_run_script")))
  571. # define MOZ_MUST_OVERRIDE __attribute__((annotate("moz_must_override")))
  572. # define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class")))
  573. # define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
  574. # define MOZ_HEAP_CLASS __attribute__((annotate("moz_heap_class")))
  575. # define MOZ_NON_TEMPORARY_CLASS __attribute__((annotate("moz_non_temporary_class")))
  576. # define MOZ_TRIVIAL_CTOR_DTOR __attribute__((annotate("moz_trivial_ctor_dtor")))
  577. # ifdef DEBUG
  578. /* in debug builds, these classes do have non-trivial constructors. */
  579. # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS __attribute__((annotate("moz_global_class")))
  580. # else
  581. # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS __attribute__((annotate("moz_global_class"))) \
  582. MOZ_TRIVIAL_CTOR_DTOR
  583. # endif
  584. # define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
  585. # define MOZ_IS_SMARTPTR_TO_REFCOUNTED __attribute__((annotate("moz_is_smartptr_to_refcounted")))
  586. # define MOZ_IS_REFPTR __attribute__((annotate("moz_is_refptr"))) \
  587. MOZ_IS_SMARTPTR_TO_REFCOUNTED
  588. # define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT __attribute__((annotate("moz_no_arith_expr_in_arg")))
  589. # define MOZ_OWNING_REF __attribute__((annotate("moz_strong_ref")))
  590. # define MOZ_NON_OWNING_REF __attribute__((annotate("moz_weak_ref")))
  591. # define MOZ_UNSAFE_REF(reason) __attribute__((annotate("moz_weak_ref")))
  592. # define MOZ_NO_ADDREF_RELEASE_ON_RETURN __attribute__((annotate("moz_no_addref_release_on_return")))
  593. # define MOZ_MUST_USE_TYPE __attribute__((annotate("moz_must_use_type")))
  594. # define MOZ_NEEDS_NO_VTABLE_TYPE __attribute__((annotate("moz_needs_no_vtable_type")))
  595. # define MOZ_NON_MEMMOVABLE __attribute__((annotate("moz_non_memmovable")))
  596. # define MOZ_NEEDS_MEMMOVABLE_TYPE __attribute__((annotate("moz_needs_memmovable_type")))
  597. # define MOZ_NEEDS_MEMMOVABLE_MEMBERS __attribute__((annotate("moz_needs_memmovable_members")))
  598. # define MOZ_NO_DANGLING_ON_TEMPORARIES __attribute__((annotate("moz_no_dangling_on_temporaries")))
  599. # define MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS \
  600. __attribute__((annotate("moz_inherit_type_annotations_from_template_args")))
  601. # define MOZ_NON_AUTOABLE __attribute__((annotate("moz_non_autoable")))
  602. # define MOZ_INIT_OUTSIDE_CTOR \
  603. __attribute__((annotate("moz_ignore_ctor_initialization")))
  604. # define MOZ_IS_CLASS_INIT \
  605. __attribute__((annotate("moz_is_class_init")))
  606. # define MOZ_NON_PARAM \
  607. __attribute__((annotate("moz_non_param")))
  608. # define MOZ_REQUIRED_BASE_METHOD \
  609. __attribute__((annotate("moz_required_base_method")))
  610. # define MOZ_MUST_RETURN_FROM_CALLER \
  611. __attribute__((annotate("moz_must_return_from_caller")))
  612. # define MOZ_MAY_CALL_AFTER_MUST_RETURN \
  613. __attribute__((annotate("moz_may_call_after_must_return")))
  614. /*
  615. * It turns out that clang doesn't like void func() __attribute__ {} without a
  616. * warning, so use pragmas to disable the warning. This code won't work on GCC
  617. * anyways, so the warning is safe to ignore.
  618. */
  619. # define MOZ_HEAP_ALLOCATOR \
  620. _Pragma("clang diagnostic push") \
  621. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  622. __attribute__((annotate("moz_heap_allocator"))) \
  623. _Pragma("clang diagnostic pop")
  624. #else
  625. # define MOZ_CAN_RUN_SCRIPT /* nothing */
  626. # define MOZ_MUST_OVERRIDE /* nothing */
  627. # define MOZ_STACK_CLASS /* nothing */
  628. # define MOZ_NONHEAP_CLASS /* nothing */
  629. # define MOZ_HEAP_CLASS /* nothing */
  630. # define MOZ_NON_TEMPORARY_CLASS /* nothing */
  631. # define MOZ_TRIVIAL_CTOR_DTOR /* nothing */
  632. # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS /* nothing */
  633. # define MOZ_IMPLICIT /* nothing */
  634. # define MOZ_IS_SMARTPTR_TO_REFCOUNTED /* nothing */
  635. # define MOZ_IS_REFPTR /* nothing */
  636. # define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT /* nothing */
  637. # define MOZ_HEAP_ALLOCATOR /* nothing */
  638. # define MOZ_OWNING_REF /* nothing */
  639. # define MOZ_NON_OWNING_REF /* nothing */
  640. # define MOZ_UNSAFE_REF(reason) /* nothing */
  641. # define MOZ_NO_ADDREF_RELEASE_ON_RETURN /* nothing */
  642. # define MOZ_MUST_USE_TYPE /* nothing */
  643. # define MOZ_NEEDS_NO_VTABLE_TYPE /* nothing */
  644. # define MOZ_NON_MEMMOVABLE /* nothing */
  645. # define MOZ_NEEDS_MEMMOVABLE_TYPE /* nothing */
  646. # define MOZ_NEEDS_MEMMOVABLE_MEMBERS /* nothing */
  647. # define MOZ_NO_DANGLING_ON_TEMPORARIES /* nothing */
  648. # define MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS /* nothing */
  649. # define MOZ_INIT_OUTSIDE_CTOR /* nothing */
  650. # define MOZ_IS_CLASS_INIT /* nothing */
  651. # define MOZ_NON_PARAM /* nothing */
  652. # define MOZ_NON_AUTOABLE /* nothing */
  653. # define MOZ_REQUIRED_BASE_METHOD /* nothing */
  654. # define MOZ_MUST_RETURN_FROM_CALLER /* nothing */
  655. # define MOZ_MAY_CALL_AFTER_MUST_RETURN /* nothing */
  656. #endif /* MOZ_CLANG_PLUGIN */
  657. #define MOZ_RAII MOZ_NON_TEMPORARY_CLASS MOZ_STACK_CLASS
  658. #endif /* __cplusplus */
  659. /**
  660. * Printf style formats. MOZ_FORMAT_PRINTF can be used to annotate a
  661. * function or method that is "printf-like"; this will let (some)
  662. * compilers check that the arguments match the template string.
  663. *
  664. * This macro takes two arguments. The first argument is the argument
  665. * number of the template string. The second argument is the argument
  666. * number of the '...' argument holding the arguments.
  667. *
  668. * Argument numbers start at 1. Note that the implicit "this"
  669. * argument of a non-static member function counts as an argument.
  670. *
  671. * So, for a simple case like:
  672. * void print_something (int whatever, const char *fmt, ...);
  673. * The corresponding annotation would be
  674. * MOZ_FORMAT_PRINTF(2, 3)
  675. * However, if "print_something" were a non-static member function,
  676. * then the annotation would be:
  677. * MOZ_FORMAT_PRINTF(3, 4)
  678. *
  679. * The second argument should be 0 for vprintf-like functions; that
  680. * is, those taking a va_list argument.
  681. *
  682. * Note that the checking is limited to standards-conforming
  683. * printf-likes, and in particular this should not be used for
  684. * PR_snprintf and friends, which are "printf-like" but which assign
  685. * different meanings to the various formats.
  686. *
  687. * MinGW requires special handling due to different format specifiers
  688. * on different platforms. The macro __MINGW_PRINTF_FORMAT maps to
  689. * either gnu_printf or ms_printf depending on where we are compiling
  690. * to avoid warnings on format specifiers that are legal.
  691. */
  692. #ifdef __MINGW32__
  693. #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) \
  694. __attribute__ ((format (__MINGW_PRINTF_FORMAT, stringIndex, firstToCheck)))
  695. #elif __GNUC__
  696. #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) \
  697. __attribute__ ((format (printf, stringIndex, firstToCheck)))
  698. #else
  699. #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck)
  700. #endif
  701. /**
  702. * To manually declare an XPCOM ABI-compatible virtual function, the following
  703. * macros can be used to handle the non-standard ABI used on Windows for COM
  704. * compatibility. E.g.:
  705. *
  706. * virtual ReturnType MOZ_XPCOM_ABI foo();
  707. */
  708. #if defined(XP_WIN)
  709. # define MOZ_XPCOM_ABI __stdcall
  710. #else
  711. # define MOZ_XPCOM_ABI
  712. #endif
  713. #endif /* mozilla_Attributes_h */