Attributes.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. #endif
  64. /*
  65. * When built with clang analyzer (a.k.a scan-build), define MOZ_HAVE_NORETURN
  66. * to mark some false positives
  67. */
  68. #ifdef __clang_analyzer__
  69. # if __has_extension(attribute_analyzer_noreturn)
  70. # define MOZ_HAVE_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
  71. # endif
  72. #endif
  73. /*
  74. * MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the
  75. * method decorated with it must never be inlined, even if the compiler would
  76. * otherwise choose to inline the method. Compilers aren't absolutely
  77. * guaranteed to support this, but most do.
  78. */
  79. #if defined(MOZ_HAVE_NEVER_INLINE)
  80. # define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE
  81. #else
  82. # define MOZ_NEVER_INLINE /* no support */
  83. #endif
  84. /*
  85. * MOZ_NORETURN, specified at the start of a function declaration, indicates
  86. * that the given function does not return. (The function definition does not
  87. * need to be annotated.)
  88. *
  89. * MOZ_NORETURN void abort(const char* msg);
  90. *
  91. * This modifier permits the compiler to optimize code assuming a call to such a
  92. * function will never return. It also enables the compiler to avoid spurious
  93. * warnings about not initializing variables, or about any other seemingly-dodgy
  94. * operations performed after the function returns.
  95. *
  96. * This modifier does not affect the corresponding function's linking behavior.
  97. */
  98. #if defined(MOZ_HAVE_NORETURN)
  99. # define MOZ_NORETURN MOZ_HAVE_NORETURN
  100. #else
  101. # define MOZ_NORETURN /* no support */
  102. #endif
  103. /**
  104. * MOZ_COLD tells the compiler that a function is "cold", meaning infrequently
  105. * executed. This may lead it to optimize for size more aggressively than speed,
  106. * or to allocate the body of the function in a distant part of the text segment
  107. * to help keep it from taking up unnecessary icache when it isn't in use.
  108. *
  109. * Place this attribute at the very beginning of a function definition. For
  110. * example, write
  111. *
  112. * MOZ_COLD int foo();
  113. *
  114. * or
  115. *
  116. * MOZ_COLD int foo() { return 42; }
  117. */
  118. #if defined(__GNUC__) || defined(__clang__)
  119. # define MOZ_COLD __attribute__ ((cold))
  120. #else
  121. # define MOZ_COLD
  122. #endif
  123. /**
  124. * MOZ_NONNULL tells the compiler that some of the arguments to a function are
  125. * known to be non-null. The arguments are a list of 1-based argument indexes
  126. * identifying arguments which are known to be non-null.
  127. *
  128. * Place this attribute at the very beginning of a function definition. For
  129. * example, write
  130. *
  131. * MOZ_NONNULL(1, 2) int foo(char *p, char *q);
  132. */
  133. #if defined(__GNUC__) || defined(__clang__)
  134. # define MOZ_NONNULL(...) __attribute__ ((nonnull(__VA_ARGS__)))
  135. #else
  136. # define MOZ_NONNULL(...)
  137. #endif
  138. /*
  139. * MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS, specified at the end of a function
  140. * declaration, indicates that for the purposes of static analysis, this
  141. * function does not return. (The function definition does not need to be
  142. * annotated.)
  143. *
  144. * MOZ_ReportCrash(const char* s, const char* file, int ln)
  145. * MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
  146. *
  147. * Some static analyzers, like scan-build from clang, can use this information
  148. * to eliminate false positives. From the upstream documentation of scan-build:
  149. * "This attribute is useful for annotating assertion handlers that actually
  150. * can return, but for the purpose of using the analyzer we want to pretend
  151. * that such functions do not return."
  152. *
  153. */
  154. #if defined(MOZ_HAVE_ANALYZER_NORETURN)
  155. # define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS MOZ_HAVE_ANALYZER_NORETURN
  156. #else
  157. # define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS /* no support */
  158. #endif
  159. /*
  160. * MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
  161. * instrumentation shipped with Clang and GCC) to not instrument the annotated
  162. * function. Furthermore, it will prevent the compiler from inlining the
  163. * function because inlining currently breaks the blacklisting mechanism of
  164. * AddressSanitizer.
  165. */
  166. #if defined(__has_feature)
  167. # if __has_feature(address_sanitizer)
  168. # define MOZ_HAVE_ASAN_BLACKLIST
  169. # endif
  170. #elif defined(__GNUC__)
  171. # if defined(__SANITIZE_ADDRESS__)
  172. # define MOZ_HAVE_ASAN_BLACKLIST
  173. # endif
  174. #endif
  175. #if defined(MOZ_HAVE_ASAN_BLACKLIST)
  176. # define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_address))
  177. #else
  178. # define MOZ_ASAN_BLACKLIST /* nothing */
  179. #endif
  180. /*
  181. * MOZ_TSAN_BLACKLIST is a macro to tell ThreadSanitizer (a compile-time
  182. * instrumentation shipped with Clang) to not instrument the annotated function.
  183. * Furthermore, it will prevent the compiler from inlining the function because
  184. * inlining currently breaks the blacklisting mechanism of ThreadSanitizer.
  185. */
  186. #if defined(__has_feature)
  187. # if __has_feature(thread_sanitizer)
  188. # define MOZ_TSAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_thread))
  189. # else
  190. # define MOZ_TSAN_BLACKLIST /* nothing */
  191. # endif
  192. #else
  193. # define MOZ_TSAN_BLACKLIST /* nothing */
  194. #endif
  195. /**
  196. * MOZ_ALLOCATOR tells the compiler that the function it marks returns either a
  197. * "fresh", "pointer-free" block of memory, or nullptr. "Fresh" means that the
  198. * block is not pointed to by any other reachable pointer in the program.
  199. * "Pointer-free" means that the block contains no pointers to any valid object
  200. * in the program. It may be initialized with other (non-pointer) values.
  201. *
  202. * Placing this attribute on appropriate functions helps GCC analyze pointer
  203. * aliasing more accurately in their callers.
  204. *
  205. * GCC warns if a caller ignores the value returned by a function marked with
  206. * MOZ_ALLOCATOR: it is hard to imagine cases where dropping the value returned
  207. * by a function that meets the criteria above would be intentional.
  208. *
  209. * Place this attribute after the argument list and 'this' qualifiers of a
  210. * function definition. For example, write
  211. *
  212. * void *my_allocator(size_t) MOZ_ALLOCATOR;
  213. *
  214. * or
  215. *
  216. * void *my_allocator(size_t bytes) MOZ_ALLOCATOR { ... }
  217. */
  218. #if defined(__GNUC__) || defined(__clang__)
  219. # define MOZ_ALLOCATOR __attribute__ ((malloc, warn_unused_result))
  220. #else
  221. # define MOZ_ALLOCATOR
  222. #endif
  223. /**
  224. * MOZ_MUST_USE tells the compiler to emit a warning if a function's
  225. * return value is not used by the caller.
  226. *
  227. * Place this attribute at the very beginning of a function declaration. For
  228. * example, write
  229. *
  230. * MOZ_MUST_USE int foo();
  231. *
  232. * or
  233. *
  234. * MOZ_MUST_USE int foo() { return 42; }
  235. */
  236. #if defined(__GNUC__) || defined(__clang__)
  237. # define MOZ_MUST_USE __attribute__ ((warn_unused_result))
  238. #else
  239. # define MOZ_MUST_USE
  240. #endif
  241. /**
  242. * MOZ_FALLTHROUGH is an annotation to suppress compiler warnings about switch
  243. * cases that fall through without a break or return statement. MOZ_FALLTHROUGH
  244. * is only needed on cases that have code.
  245. *
  246. * MOZ_FALLTHROUGH_ASSERT is an annotation to suppress compiler warnings about
  247. * switch cases that MOZ_ASSERT(false) (or its alias MOZ_ASSERT_UNREACHABLE) in
  248. * debug builds, but intentionally fall through in release builds. See comment
  249. * in Assertions.h for more details.
  250. *
  251. * switch (foo) {
  252. * case 1: // These cases have no code. No fallthrough annotations are needed.
  253. * case 2:
  254. * case 3: // This case has code, so a fallthrough annotation is needed!
  255. * foo++;
  256. * MOZ_FALLTHROUGH;
  257. * case 4:
  258. * return foo;
  259. *
  260. * default:
  261. * // This case asserts in debug builds, falls through in release.
  262. * MOZ_FALLTHROUGH_ASSERT("Unexpected foo value?!");
  263. * case 5:
  264. * return 5;
  265. * }
  266. */
  267. #if defined(__clang__) && __cplusplus >= 201103L
  268. /* clang's fallthrough annotations are only available starting in C++11. */
  269. # define MOZ_FALLTHROUGH [[clang::fallthrough]]
  270. #elif defined(_MSC_VER)
  271. /*
  272. * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
  273. * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
  274. */
  275. # include <sal.h>
  276. # define MOZ_FALLTHROUGH __fallthrough
  277. #else
  278. # define MOZ_FALLTHROUGH /* FALLTHROUGH */
  279. #endif
  280. #ifdef __cplusplus
  281. /*
  282. * The following macros are attributes that support the static analysis plugin
  283. * included with Mozilla, and will be implemented (when such support is enabled)
  284. * as C++11 attributes. Since such attributes are legal pretty much everywhere
  285. * and have subtly different semantics depending on their placement, the
  286. * following is a guide on where to place the attributes.
  287. *
  288. * Attributes that apply to a struct or class precede the name of the class:
  289. * (Note that this is different from the placement of final for classes!)
  290. *
  291. * class MOZ_CLASS_ATTRIBUTE SomeClass {};
  292. *
  293. * Attributes that apply to functions follow the parentheses and const
  294. * qualifiers but precede final, override and the function body:
  295. *
  296. * void DeclaredFunction() MOZ_FUNCTION_ATTRIBUTE;
  297. * void SomeFunction() MOZ_FUNCTION_ATTRIBUTE {}
  298. * void PureFunction() const MOZ_FUNCTION_ATTRIBUTE = 0;
  299. * void OverriddenFunction() MOZ_FUNCTION_ATTIRBUTE override;
  300. *
  301. * Attributes that apply to variables or parameters follow the variable's name:
  302. *
  303. * int variable MOZ_VARIABLE_ATTRIBUTE;
  304. *
  305. * Attributes that apply to types follow the type name:
  306. *
  307. * typedef int MOZ_TYPE_ATTRIBUTE MagicInt;
  308. * int MOZ_TYPE_ATTRIBUTE someVariable;
  309. * int* MOZ_TYPE_ATTRIBUTE magicPtrInt;
  310. * int MOZ_TYPE_ATTRIBUTE* ptrToMagicInt;
  311. *
  312. * Attributes that apply to statements precede the statement:
  313. *
  314. * MOZ_IF_ATTRIBUTE if (x == 0)
  315. * MOZ_DO_ATTRIBUTE do { } while (0);
  316. *
  317. * Attributes that apply to labels precede the label:
  318. *
  319. * MOZ_LABEL_ATTRIBUTE target:
  320. * goto target;
  321. * MOZ_CASE_ATTRIBUTE case 5:
  322. * MOZ_DEFAULT_ATTRIBUTE default:
  323. *
  324. * The static analyses that are performed by the plugin are as follows:
  325. *
  326. * MOZ_MUST_OVERRIDE: Applies to all C++ member functions. All immediate
  327. * subclasses must provide an exact override of this method; if a subclass
  328. * does not override this method, the compiler will emit an error. This
  329. * attribute is not limited to virtual methods, so if it is applied to a
  330. * nonvirtual method and the subclass does not provide an equivalent
  331. * definition, the compiler will emit an error.
  332. * MOZ_STACK_CLASS: Applies to all classes. Any class with this annotation is
  333. * expected to live on the stack, so it is a compile-time error to use it, or
  334. * an array of such objects, as a global or static variable, or as the type of
  335. * a new expression (unless placement new is being used). If a member of
  336. * another class uses this class, or if another class inherits from this
  337. * class, then it is considered to be a stack class as well, although this
  338. * attribute need not be provided in such cases.
  339. * MOZ_NONHEAP_CLASS: Applies to all classes. Any class with this annotation is
  340. * expected to live on the stack or in static storage, so it is a compile-time
  341. * error to use it, or an array of such objects, as the type of a new
  342. * expression. If a member of another class uses this class, or if another
  343. * class inherits from this class, then it is considered to be a non-heap class
  344. * as well, although this attribute need not be provided in such cases.
  345. * MOZ_HEAP_CLASS: Applies to all classes. Any class with this annotation is
  346. * expected to live on the heap, so it is a compile-time error to use it, or
  347. * an array of such objects, as the type of a variable declaration, or as a
  348. * temporary object. If a member of another class uses this class, or if
  349. * another class inherits from this class, then it is considered to be a heap
  350. * class as well, although this attribute need not be provided in such cases.
  351. * MOZ_NON_TEMPORARY_CLASS: Applies to all classes. Any class with this
  352. * annotation is expected not to live in a temporary. If a member of another
  353. * class uses this class or if another class inherits from this class, then it
  354. * is considered to be a non-temporary class as well, although this attribute
  355. * need not be provided in such cases.
  356. * MOZ_RAII: Applies to all classes. Any class with this annotation is assumed
  357. * to be a RAII guard, which is expected to live on the stack in an automatic
  358. * allocation. It is prohibited from being allocated in a temporary, static
  359. * storage, or on the heap. This is a combination of MOZ_STACK_CLASS and
  360. * MOZ_NON_TEMPORARY_CLASS.
  361. * MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS: Applies to all classes that are
  362. * intended to prevent introducing static initializers. This attribute
  363. * currently makes it a compile-time error to instantiate these classes
  364. * anywhere other than at the global scope, or as a static member of a class.
  365. * In non-debug mode, it also prohibits non-trivial constructors and
  366. * destructors.
  367. * MOZ_TRIVIAL_CTOR_DTOR: Applies to all classes that must have both a trivial
  368. * or constexpr constructor and a trivial destructor. Setting this attribute
  369. * on a class makes it a compile-time error for that class to get a
  370. * non-trivial constructor or destructor for any reason.
  371. * MOZ_HEAP_ALLOCATOR: Applies to any function. This indicates that the return
  372. * value is allocated on the heap, and will as a result check such allocations
  373. * during MOZ_STACK_CLASS and MOZ_NONHEAP_CLASS annotation checking.
  374. * MOZ_IMPLICIT: Applies to constructors. Implicit conversion constructors
  375. * are disallowed by default unless they are marked as MOZ_IMPLICIT. This
  376. * attribute must be used for constructors which intend to provide implicit
  377. * conversions.
  378. * MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT: Applies to functions. Makes it a compile
  379. * time error to pass arithmetic expressions on variables to the function.
  380. * MOZ_OWNING_REF: Applies to declarations of pointers to reference counted
  381. * types. This attribute tells the compiler that the raw pointer is a strong
  382. * reference, where ownership through methods such as AddRef and Release is
  383. * managed manually. This can make the compiler ignore these pointers when
  384. * validating the usage of pointers otherwise.
  385. *
  386. * Example uses include owned pointers inside of unions, and pointers stored
  387. * in POD types where a using a smart pointer class would make the object
  388. * non-POD.
  389. * MOZ_NON_OWNING_REF: Applies to declarations of pointers to reference counted
  390. * types. This attribute tells the compiler that the raw pointer is a weak
  391. * reference, which is ensured to be valid by a guarantee that the reference
  392. * will be nulled before the pointer becomes invalid. This can make the compiler
  393. * ignore these pointers when validating the usage of pointers otherwise.
  394. *
  395. * Examples include an mOwner pointer, which is nulled by the owning class's
  396. * destructor, and is null-checked before dereferencing.
  397. * MOZ_UNSAFE_REF: Applies to declarations of pointers to reference counted types.
  398. * Occasionally there are non-owning references which are valid, but do not take
  399. * the form of a MOZ_NON_OWNING_REF. Their safety may be dependent on the behaviour
  400. * of API consumers. The string argument passed to this macro documents the safety
  401. * conditions. This can make the compiler ignore these pointers when validating
  402. * the usage of pointers elsewhere.
  403. *
  404. * Examples include an nsIAtom* member which is known at compile time to point to a
  405. * static atom which is valid throughout the lifetime of the program, or an API which
  406. * stores a pointer, but doesn't take ownership over it, instead requiring the API
  407. * consumer to correctly null the value before it becomes invalid.
  408. *
  409. * Use of this annotation is discouraged when a strong reference or one of the above
  410. * two annotations can be used instead.
  411. * MOZ_NO_ADDREF_RELEASE_ON_RETURN: Applies to function declarations. Makes it
  412. * a compile time error to call AddRef or Release on the return value of a
  413. * function. This is intended to be used with operator->() of our smart
  414. * pointer classes to ensure that the refcount of an object wrapped in a
  415. * smart pointer is not manipulated directly.
  416. * MOZ_MUST_USE_TYPE: Applies to type declarations. Makes it a compile time
  417. * error to not use the return value of a function which has this type. This
  418. * is intended to be used with types which it is an error to not use.
  419. * MOZ_NEEDS_NO_VTABLE_TYPE: Applies to template class declarations. Makes it
  420. * a compile time error to instantiate this template with a type parameter which
  421. * has a VTable.
  422. * MOZ_NON_MEMMOVABLE: Applies to class declarations for types that are not safe
  423. * to be moved in memory using memmove().
  424. * MOZ_NEEDS_MEMMOVABLE_TYPE: Applies to template class declarations where the
  425. * template arguments are required to be safe to move in memory using
  426. * memmove(). Passing MOZ_NON_MEMMOVABLE types to these templates is a
  427. * compile time error.
  428. * MOZ_NEEDS_MEMMOVABLE_MEMBERS: Applies to class declarations where each member
  429. * must be safe to move in memory using memmove(). MOZ_NON_MEMMOVABLE types
  430. * used in members of these classes are compile time errors.
  431. * MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS: Applies to template class
  432. * declarations where an instance of the template should be considered, for
  433. * static analysis purposes, to inherit any type annotations (such as
  434. * MOZ_MUST_USE_TYPE and MOZ_STACK_CLASS) from its template arguments.
  435. * MOZ_INIT_OUTSIDE_CTOR: Applies to class member declarations. Occasionally
  436. * there are class members that are not initialized in the constructor,
  437. * but logic elsewhere in the class ensures they are initialized prior to use.
  438. * Using this attribute on a member disables the check that this member must be
  439. * initialized in constructors via list-initialization, in the constructor body,
  440. * or via functions called from the constructor body.
  441. * MOZ_IS_CLASS_INIT: Applies to class method declarations. Occasionally the
  442. * constructor doesn't initialize all of the member variables and another function
  443. * is used to initialize the rest. This marker is used to make the static analysis
  444. * tool aware that the marked function is part of the initialization process
  445. * and to include the marked function in the scan mechanism that determines witch
  446. * member variables still remain uninitialized.
  447. * MOZ_NON_PARAM: Applies to types. Makes it compile time error to use the type
  448. * in parameter without pointer or reference.
  449. * MOZ_NON_AUTOABLE: Applies to class declarations. Makes it a compile time error to
  450. * use `auto` in place of this type in variable declarations. This is intended to
  451. * be used with types which are intended to be implicitly constructed into other
  452. * other types before being assigned to variables.
  453. * MOZ_REQUIRED_BASE_METHOD: Applies to virtual class method declarations.
  454. * Sometimes derived classes override methods that need to be called by their
  455. * overridden counterparts. This marker indicates that the marked method must
  456. * be called by the method that it overrides.
  457. */
  458. #ifdef MOZ_CLANG_PLUGIN
  459. # define MOZ_MUST_OVERRIDE __attribute__((annotate("moz_must_override")))
  460. # define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class")))
  461. # define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
  462. # define MOZ_HEAP_CLASS __attribute__((annotate("moz_heap_class")))
  463. # define MOZ_NON_TEMPORARY_CLASS __attribute__((annotate("moz_non_temporary_class")))
  464. # define MOZ_TRIVIAL_CTOR_DTOR __attribute__((annotate("moz_trivial_ctor_dtor")))
  465. # ifdef DEBUG
  466. /* in debug builds, these classes do have non-trivial constructors. */
  467. # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS __attribute__((annotate("moz_global_class")))
  468. # else
  469. # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS __attribute__((annotate("moz_global_class"))) \
  470. MOZ_TRIVIAL_CTOR_DTOR
  471. # endif
  472. # define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
  473. # define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT __attribute__((annotate("moz_no_arith_expr_in_arg")))
  474. # define MOZ_OWNING_REF __attribute__((annotate("moz_strong_ref")))
  475. # define MOZ_NON_OWNING_REF __attribute__((annotate("moz_weak_ref")))
  476. # define MOZ_UNSAFE_REF(reason) __attribute__((annotate("moz_weak_ref")))
  477. # define MOZ_NO_ADDREF_RELEASE_ON_RETURN __attribute__((annotate("moz_no_addref_release_on_return")))
  478. # define MOZ_MUST_USE_TYPE __attribute__((annotate("moz_must_use_type")))
  479. # define MOZ_NEEDS_NO_VTABLE_TYPE __attribute__((annotate("moz_needs_no_vtable_type")))
  480. # define MOZ_NON_MEMMOVABLE __attribute__((annotate("moz_non_memmovable")))
  481. # define MOZ_NEEDS_MEMMOVABLE_TYPE __attribute__((annotate("moz_needs_memmovable_type")))
  482. # define MOZ_NEEDS_MEMMOVABLE_MEMBERS __attribute__((annotate("moz_needs_memmovable_members")))
  483. # define MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS \
  484. __attribute__((annotate("moz_inherit_type_annotations_from_template_args")))
  485. # define MOZ_NON_AUTOABLE __attribute__((annotate("moz_non_autoable")))
  486. # define MOZ_INIT_OUTSIDE_CTOR \
  487. __attribute__((annotate("moz_ignore_ctor_initialization")))
  488. # define MOZ_IS_CLASS_INIT \
  489. __attribute__((annotate("moz_is_class_init")))
  490. # define MOZ_NON_PARAM \
  491. __attribute__((annotate("moz_non_param")))
  492. # define MOZ_REQUIRED_BASE_METHOD \
  493. __attribute__((annotate("moz_required_base_method")))
  494. /*
  495. * It turns out that clang doesn't like void func() __attribute__ {} without a
  496. * warning, so use pragmas to disable the warning. This code won't work on GCC
  497. * anyways, so the warning is safe to ignore.
  498. */
  499. # define MOZ_HEAP_ALLOCATOR \
  500. _Pragma("clang diagnostic push") \
  501. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  502. __attribute__((annotate("moz_heap_allocator"))) \
  503. _Pragma("clang diagnostic pop")
  504. #else
  505. # define MOZ_MUST_OVERRIDE /* nothing */
  506. # define MOZ_STACK_CLASS /* nothing */
  507. # define MOZ_NONHEAP_CLASS /* nothing */
  508. # define MOZ_HEAP_CLASS /* nothing */
  509. # define MOZ_NON_TEMPORARY_CLASS /* nothing */
  510. # define MOZ_TRIVIAL_CTOR_DTOR /* nothing */
  511. # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS /* nothing */
  512. # define MOZ_IMPLICIT /* nothing */
  513. # define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT /* nothing */
  514. # define MOZ_HEAP_ALLOCATOR /* nothing */
  515. # define MOZ_OWNING_REF /* nothing */
  516. # define MOZ_NON_OWNING_REF /* nothing */
  517. # define MOZ_UNSAFE_REF(reason) /* nothing */
  518. # define MOZ_NO_ADDREF_RELEASE_ON_RETURN /* nothing */
  519. # define MOZ_MUST_USE_TYPE /* nothing */
  520. # define MOZ_NEEDS_NO_VTABLE_TYPE /* nothing */
  521. # define MOZ_NON_MEMMOVABLE /* nothing */
  522. # define MOZ_NEEDS_MEMMOVABLE_TYPE /* nothing */
  523. # define MOZ_NEEDS_MEMMOVABLE_MEMBERS /* nothing */
  524. # define MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS /* nothing */
  525. # define MOZ_INIT_OUTSIDE_CTOR /* nothing */
  526. # define MOZ_IS_CLASS_INIT /* nothing */
  527. # define MOZ_NON_PARAM /* nothing */
  528. # define MOZ_NON_AUTOABLE /* nothing */
  529. # define MOZ_REQUIRED_BASE_METHOD /* nothing */
  530. #endif /* MOZ_CLANG_PLUGIN */
  531. #define MOZ_RAII MOZ_NON_TEMPORARY_CLASS MOZ_STACK_CLASS
  532. /*
  533. * MOZ_HAVE_REF_QUALIFIERS is defined for compilers that support C++11's rvalue
  534. * qualifier, "&&".
  535. */
  536. #if defined(_MSC_VER) && _MSC_VER >= 1900
  537. # define MOZ_HAVE_REF_QUALIFIERS
  538. #elif defined(__clang__)
  539. // All supported Clang versions
  540. # define MOZ_HAVE_REF_QUALIFIERS
  541. #elif defined(__GNUC__)
  542. # include "mozilla/Compiler.h"
  543. # if MOZ_GCC_VERSION_AT_LEAST(4, 8, 1)
  544. # define MOZ_HAVE_REF_QUALIFIERS
  545. # endif
  546. #endif
  547. #endif /* __cplusplus */
  548. /**
  549. * Printf style formats. MOZ_FORMAT_PRINTF can be used to annotate a
  550. * function or method that is "printf-like"; this will let (some)
  551. * compilers check that the arguments match the template string.
  552. *
  553. * This macro takes two arguments. The first argument is the argument
  554. * number of the template string. The second argument is the argument
  555. * number of the '...' argument holding the arguments.
  556. *
  557. * Argument numbers start at 1. Note that the implicit "this"
  558. * argument of a non-static member function counts as an argument.
  559. *
  560. * So, for a simple case like:
  561. * void print_something (int whatever, const char *fmt, ...);
  562. * The corresponding annotation would be
  563. * MOZ_FORMAT_PRINTF(2, 3)
  564. * However, if "print_something" were a non-static member function,
  565. * then the annotation would be:
  566. * MOZ_FORMAT_PRINTF(3, 4)
  567. *
  568. * Note that the checking is limited to standards-conforming
  569. * printf-likes, and in particular this should not be used for
  570. * PR_snprintf and friends, which are "printf-like" but which assign
  571. * different meanings to the various formats.
  572. */
  573. #ifdef __GNUC__
  574. #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) \
  575. __attribute__ ((format (printf, stringIndex, firstToCheck)))
  576. #else
  577. #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck)
  578. #endif
  579. #endif /* mozilla_Attributes_h */