Attributes.h 27 KB

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