Bumping gaia.json for 7 gaia revision(s) a=gaia-bump
[gecko.git] / mfbt / Attributes.h
blobce536038ca5739c1b24134d0a559a684e6a3f6d7
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/. */
7 /* Implementations of various class and method modifier attributes. */
9 #ifndef mozilla_Attributes_h
10 #define mozilla_Attributes_h
12 #include "mozilla/Compiler.h"
15 * MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the
16 * method decorated with it must be inlined, even if the compiler thinks
17 * otherwise. This is only a (much) stronger version of the inline hint:
18 * compilers are not guaranteed to respect it (although they're much more likely
19 * to do so).
21 * The MOZ_ALWAYS_INLINE_EVEN_DEBUG macro is yet stronger. It tells the
22 * compiler to inline even in DEBUG builds. It should be used very rarely.
24 #if defined(_MSC_VER)
25 # define MOZ_ALWAYS_INLINE_EVEN_DEBUG __forceinline
26 #elif defined(__GNUC__)
27 # define MOZ_ALWAYS_INLINE_EVEN_DEBUG __attribute__((always_inline)) inline
28 #else
29 # define MOZ_ALWAYS_INLINE_EVEN_DEBUG inline
30 #endif
32 #if !defined(DEBUG)
33 # define MOZ_ALWAYS_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG
34 #elif defined(_MSC_VER) && !defined(__cplusplus)
35 # define MOZ_ALWAYS_INLINE __inline
36 #else
37 # define MOZ_ALWAYS_INLINE inline
38 #endif
40 #if defined(_MSC_VER)
42 * g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality
43 * without warnings (functionality used by the macros below). These modes are
44 * detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more
45 * standardly, by checking whether __cplusplus has a C++11 or greater value.
46 * Current versions of g++ do not correctly set __cplusplus, so we check both
47 * for forward compatibility.
49 * Even though some versions of MSVC support explicit conversion operators, we
50 * don't indicate support for them here, due to
51 * http://stackoverflow.com/questions/20498142/visual-studio-2013-explicit-keyword-bug
53 # define MOZ_HAVE_CXX11_FINAL final
54 # define MOZ_HAVE_CXX11_OVERRIDE
55 # define MOZ_HAVE_NEVER_INLINE __declspec(noinline)
56 # define MOZ_HAVE_NORETURN __declspec(noreturn)
57 # ifdef __clang__
58 /* clang-cl probably supports constexpr and explicit conversions. */
59 # if __has_extension(cxx_constexpr)
60 # define MOZ_HAVE_CXX11_CONSTEXPR
61 # endif
62 # if __has_extension(cxx_explicit_conversions)
63 # define MOZ_HAVE_EXPLICIT_CONVERSION
64 # endif
65 # endif
66 #elif defined(__clang__)
68 * Per Clang documentation, "Note that marketing version numbers should not
69 * be used to check for language features, as different vendors use different
70 * numbering schemes. Instead, use the feature checking macros."
72 # ifndef __has_extension
73 # define __has_extension __has_feature /* compatibility, for older versions of clang */
74 # endif
75 # if __has_extension(cxx_constexpr)
76 # define MOZ_HAVE_CXX11_CONSTEXPR
77 # endif
78 # if __has_extension(cxx_explicit_conversions)
79 # define MOZ_HAVE_EXPLICIT_CONVERSION
80 # endif
81 # if __has_extension(cxx_override_control)
82 # define MOZ_HAVE_CXX11_OVERRIDE
83 # define MOZ_HAVE_CXX11_FINAL final
84 # endif
85 # if __has_attribute(noinline)
86 # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
87 # endif
88 # if __has_attribute(noreturn)
89 # define MOZ_HAVE_NORETURN __attribute__((noreturn))
90 # endif
91 #elif defined(__GNUC__)
92 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
93 # if MOZ_GCC_VERSION_AT_LEAST(4, 7, 0)
94 # define MOZ_HAVE_CXX11_OVERRIDE
95 # define MOZ_HAVE_CXX11_FINAL final
96 # endif
97 # if MOZ_GCC_VERSION_AT_LEAST(4, 6, 0)
98 # define MOZ_HAVE_CXX11_CONSTEXPR
99 # endif
100 # if MOZ_GCC_VERSION_AT_LEAST(4, 5, 0)
101 # define MOZ_HAVE_EXPLICIT_CONVERSION
102 # endif
103 # else
104 /* __final is a non-C++11 GCC synonym for 'final', per GCC r176655. */
105 # if MOZ_GCC_VERSION_AT_LEAST(4, 7, 0)
106 # define MOZ_HAVE_CXX11_FINAL __final
107 # endif
108 # endif
109 # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
110 # define MOZ_HAVE_NORETURN __attribute__((noreturn))
111 #endif
114 * When built with clang analyzer (a.k.a scan-build), define MOZ_HAVE_NORETURN
115 * to mark some false positives
117 #ifdef __clang_analyzer__
118 # if __has_extension(attribute_analyzer_noreturn)
119 # define MOZ_HAVE_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
120 # endif
121 #endif
124 * The MOZ_CONSTEXPR specifier declares that a C++11 compiler can evaluate a
125 * function at compile time. A constexpr function cannot examine any values
126 * except its arguments and can have no side effects except its return value.
127 * The MOZ_CONSTEXPR_VAR specifier tells a C++11 compiler that a variable's
128 * value may be computed at compile time. It should be prefered to just
129 * marking variables as MOZ_CONSTEXPR because if the compiler does not support
130 * constexpr it will fall back to making the variable const, and some compilers
131 * do not accept variables being marked both const and constexpr.
133 #ifdef MOZ_HAVE_CXX11_CONSTEXPR
134 # define MOZ_CONSTEXPR constexpr
135 # define MOZ_CONSTEXPR_VAR constexpr
136 #else
137 # define MOZ_CONSTEXPR /* no support */
138 # define MOZ_CONSTEXPR_VAR const
139 #endif
142 * MOZ_EXPLICIT_CONVERSION is a specifier on a type conversion
143 * overloaded operator that declares that a C++11 compiler should restrict
144 * this operator to allow only explicit type conversions, disallowing
145 * implicit conversions.
147 * Example:
149 * template<typename T>
150 * class Ptr
152 * T* mPtr;
153 * MOZ_EXPLICIT_CONVERSION operator bool() const
155 * return mPtr != nullptr;
157 * };
160 #ifdef MOZ_HAVE_EXPLICIT_CONVERSION
161 # define MOZ_EXPLICIT_CONVERSION explicit
162 #else
163 # define MOZ_EXPLICIT_CONVERSION /* no support */
164 #endif
167 * MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the
168 * method decorated with it must never be inlined, even if the compiler would
169 * otherwise choose to inline the method. Compilers aren't absolutely
170 * guaranteed to support this, but most do.
172 #if defined(MOZ_HAVE_NEVER_INLINE)
173 # define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE
174 #else
175 # define MOZ_NEVER_INLINE /* no support */
176 #endif
179 * MOZ_NORETURN, specified at the start of a function declaration, indicates
180 * that the given function does not return. (The function definition does not
181 * need to be annotated.)
183 * MOZ_NORETURN void abort(const char* msg);
185 * This modifier permits the compiler to optimize code assuming a call to such a
186 * function will never return. It also enables the compiler to avoid spurious
187 * warnings about not initializing variables, or about any other seemingly-dodgy
188 * operations performed after the function returns.
190 * This modifier does not affect the corresponding function's linking behavior.
192 #if defined(MOZ_HAVE_NORETURN)
193 # define MOZ_NORETURN MOZ_HAVE_NORETURN
194 #else
195 # define MOZ_NORETURN /* no support */
196 #endif
199 * MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS, specified at the end of a function
200 * declaration, indicates that for the purposes of static analysis, this
201 * function does not return. (The function definition does not need to be
202 * annotated.)
204 * MOZ_ReportCrash(const char* s, const char* file, int ln)
205 * MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
207 * Some static analyzers, like scan-build from clang, can use this information
208 * to eliminate false positives. From the upstream documentation of scan-build:
209 * "This attribute is useful for annotating assertion handlers that actually
210 * can return, but for the purpose of using the analyzer we want to pretend
211 * that such functions do not return."
214 #if defined(MOZ_HAVE_ANALYZER_NORETURN)
215 # define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS MOZ_HAVE_ANALYZER_NORETURN
216 #else
217 # define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS /* no support */
218 #endif
221 * MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
222 * instrumentation shipped with Clang and GCC) to not instrument the annotated
223 * function. Furthermore, it will prevent the compiler from inlining the
224 * function because inlining currently breaks the blacklisting mechanism of
225 * AddressSanitizer.
227 #if defined(__has_feature)
228 # if __has_feature(address_sanitizer)
229 # define MOZ_HAVE_ASAN_BLACKLIST
230 # endif
231 #elif defined(__GNUC__)
232 # if defined(__SANITIZE_ADDRESS__)
233 # define MOZ_HAVE_ASAN_BLACKLIST
234 # endif
235 #endif
237 #if defined(MOZ_HAVE_ASAN_BLACKLIST)
238 # define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_address))
239 #else
240 # define MOZ_ASAN_BLACKLIST /* nothing */
241 #endif
244 * MOZ_TSAN_BLACKLIST is a macro to tell ThreadSanitizer (a compile-time
245 * instrumentation shipped with Clang) to not instrument the annotated function.
246 * Furthermore, it will prevent the compiler from inlining the function because
247 * inlining currently breaks the blacklisting mechanism of ThreadSanitizer.
249 #if defined(__has_feature)
250 # if __has_feature(thread_sanitizer)
251 # define MOZ_TSAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_thread))
252 # else
253 # define MOZ_TSAN_BLACKLIST /* nothing */
254 # endif
255 #else
256 # define MOZ_TSAN_BLACKLIST /* nothing */
257 #endif
259 #ifdef __cplusplus
262 * MOZ_OVERRIDE explicitly indicates that a virtual member function in a class
263 * overrides a member function of a base class, rather than potentially being a
264 * new member function. MOZ_OVERRIDE should be placed immediately before the
265 * ';' terminating the member function's declaration, or before '= 0;' if the
266 * member function is pure. If the member function is defined in the class
267 * definition, it should appear before the opening brace of the function body.
269 * class Base
271 * public:
272 * virtual void f() = 0;
273 * };
274 * class Derived1 : public Base
276 * public:
277 * virtual void f() MOZ_OVERRIDE;
278 * };
279 * class Derived2 : public Base
281 * public:
282 * virtual void f() MOZ_OVERRIDE = 0;
283 * };
284 * class Derived3 : public Base
286 * public:
287 * virtual void f() MOZ_OVERRIDE { }
288 * };
290 * In compilers supporting C++11 override controls, MOZ_OVERRIDE *requires* that
291 * the function marked with it override a member function of a base class: it
292 * is a compile error if it does not. Otherwise MOZ_OVERRIDE does not affect
293 * semantics and merely documents the override relationship to the reader (but
294 * of course must still be used correctly to not break C++11 compilers).
296 #if defined(MOZ_HAVE_CXX11_OVERRIDE)
297 # define MOZ_OVERRIDE override
298 #else
299 # define MOZ_OVERRIDE /* no support */
300 #endif
303 * MOZ_FINAL indicates that some functionality cannot be overridden through
304 * inheritance. It can be used to annotate either classes/structs or virtual
305 * member functions.
307 * To annotate a class/struct with MOZ_FINAL, place MOZ_FINAL immediately after
308 * the name of the class, before the list of classes from which it derives (if
309 * any) and before its opening brace. MOZ_FINAL must not be used to annotate
310 * unnamed classes or structs. (With some compilers, and with C++11 proper, the
311 * underlying expansion is ambiguous with specifying a class name.)
313 * class Base MOZ_FINAL
315 * public:
316 * Base();
317 * ~Base();
318 * virtual void f() { }
319 * };
320 * // This will be an error in some compilers:
321 * class Derived : public Base
323 * public:
324 * ~Derived() { }
325 * };
327 * One particularly common reason to specify MOZ_FINAL upon a class is to tell
328 * the compiler that it's not dangerous for it to have a non-virtual destructor
329 * yet have one or more virtual functions, silencing the warning it might emit
330 * in this case. Suppose Base above weren't annotated with MOZ_FINAL. Because
331 * ~Base() is non-virtual, an attempt to delete a Derived* through a Base*
332 * wouldn't call ~Derived(), so any cleanup ~Derived() might do wouldn't happen.
333 * (Formally C++ says behavior is undefined, but compilers will likely just call
334 * ~Base() and not ~Derived().) Specifying MOZ_FINAL tells the compiler that
335 * it's safe for the destructor to be non-virtual.
337 * In compilers implementing final controls, it is an error to inherit from a
338 * class annotated with MOZ_FINAL. In other compilers it serves only as
339 * documentation.
341 * To annotate a virtual member function with MOZ_FINAL, place MOZ_FINAL
342 * immediately before the ';' terminating the member function's declaration, or
343 * before '= 0;' if the member function is pure. If the member function is
344 * defined in the class definition, it should appear before the opening brace of
345 * the function body. (This placement is identical to that for MOZ_OVERRIDE.
346 * If both are used, they should appear in the order 'MOZ_FINAL MOZ_OVERRIDE'
347 * for consistency.)
349 * class Base
351 * public:
352 * virtual void f() MOZ_FINAL;
353 * };
354 * class Derived
356 * public:
357 * // This will be an error in some compilers:
358 * virtual void f();
359 * };
361 * In compilers implementing final controls, it is an error for a derived class
362 * to override a method annotated with MOZ_FINAL. In other compilers it serves
363 * only as documentation.
365 #if defined(MOZ_HAVE_CXX11_FINAL)
366 # define MOZ_FINAL MOZ_HAVE_CXX11_FINAL
367 #else
368 # define MOZ_FINAL /* no support */
369 #endif
372 * MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's
373 * return value is not used by the caller.
375 * Place this attribute at the very beginning of a function definition. For
376 * example, write
378 * MOZ_WARN_UNUSED_RESULT int foo();
380 * or
382 * MOZ_WARN_UNUSED_RESULT int foo() { return 42; }
384 #if defined(__GNUC__) || defined(__clang__)
385 # define MOZ_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
386 #else
387 # define MOZ_WARN_UNUSED_RESULT
388 #endif
391 * The following macros are attributes that support the static analysis plugin
392 * included with Mozilla, and will be implemented (when such support is enabled)
393 * as C++11 attributes. Since such attributes are legal pretty much everywhere
394 * and have subtly different semantics depending on their placement, the
395 * following is a guide on where to place the attributes.
397 * Attributes that apply to a struct or class precede the name of the class:
398 * (Note that this is different from the placement of MOZ_FINAL for classes!)
400 * class MOZ_CLASS_ATTRIBUTE SomeClass {};
402 * Attributes that apply to functions follow the parentheses and const
403 * qualifiers but precede MOZ_FINAL, MOZ_OVERRIDE and the function body:
405 * void DeclaredFunction() MOZ_FUNCTION_ATTRIBUTE;
406 * void SomeFunction() MOZ_FUNCTION_ATTRIBUTE {}
407 * void PureFunction() const MOZ_FUNCTION_ATTRIBUTE = 0;
408 * void OverriddenFunction() MOZ_FUNCTION_ATTIRBUTE MOZ_OVERRIDE;
410 * Attributes that apply to variables or parameters follow the variable's name:
412 * int variable MOZ_VARIABLE_ATTRIBUTE;
414 * Attributes that apply to types follow the type name:
416 * typedef int MOZ_TYPE_ATTRIBUTE MagicInt;
417 * int MOZ_TYPE_ATTRIBUTE someVariable;
418 * int* MOZ_TYPE_ATTRIBUTE magicPtrInt;
419 * int MOZ_TYPE_ATTRIBUTE* ptrToMagicInt;
421 * Attributes that apply to statements precede the statement:
423 * MOZ_IF_ATTRIBUTE if (x == 0)
424 * MOZ_DO_ATTRIBUTE do { } while (0);
426 * Attributes that apply to labels precede the label:
428 * MOZ_LABEL_ATTRIBUTE target:
429 * goto target;
430 * MOZ_CASE_ATTRIBUTE case 5:
431 * MOZ_DEFAULT_ATTRIBUTE default:
433 * The static analyses that are performed by the plugin are as follows:
435 * MOZ_MUST_OVERRIDE: Applies to all C++ member functions. All immediate
436 * subclasses must provide an exact override of this method; if a subclass
437 * does not override this method, the compiler will emit an error. This
438 * attribute is not limited to virtual methods, so if it is applied to a
439 * nonvirtual method and the subclass does not provide an equivalent
440 * definition, the compiler will emit an error.
441 * MOZ_STACK_CLASS: Applies to all classes. Any class with this annotation is
442 * expected to live on the stack, so it is a compile-time error to use it, or
443 * an array of such objects, as a global or static variable, or as the type of
444 * a new expression (unless placement new is being used). If a member of
445 * another class uses this class, or if another class inherits from this
446 * class, then it is considered to be a stack class as well, although this
447 * attribute need not be provided in such cases.
448 * MOZ_NONHEAP_CLASS: Applies to all classes. Any class with this annotation is
449 * expected to live on the stack or in static storage, so it is a compile-time
450 * error to use it, or an array of such objects, as the type of a new
451 * expression (unless placement new is being used). If a member of another
452 * class uses this class, or if another class inherits from this class, then
453 * it is considered to be a non-heap class as well, although this attribute
454 * need not be provided in such cases.
455 * MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS: Applies to all classes that are
456 * intended to prevent introducing static initializers. This attribute
457 * currently makes it a compile-time error to instantiate these classes
458 * anywhere other than at the global scope, or as a static member of a class.
459 * MOZ_TRIVIAL_CTOR_DTOR: Applies to all classes that must have both a trivial
460 * constructor and a trivial destructor. Setting this attribute on a class
461 * makes it a compile-time error for that class to get a non-trivial
462 * constructor or destructor for any reason.
463 * MOZ_HEAP_ALLOCATOR: Applies to any function. This indicates that the return
464 * value is allocated on the heap, and will as a result check such allocations
465 * during MOZ_STACK_CLASS and MOZ_NONHEAP_CLASS annotation checking.
466 * MOZ_IMPLICIT: Applies to constructors. Implicit conversion constructors
467 * are disallowed by default unless they are marked as MOZ_IMPLICIT. This
468 * attribute must be used for constructors which intend to provide implicit
469 * conversions.
470 * MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT: Applies to functions. Makes it a compile
471 * time error to pass arithmetic expressions on variables to the function.
472 * MOZ_OWNING_REF: Applies to declarations of pointer types. This attribute
473 * tells the compiler that the raw pointer is a strong reference, and that
474 * property is somehow enforced by the code. This can make the compiler
475 * ignore these pointers when validating the usage of pointers otherwise.
476 * MOZ_NON_OWNING_REF: Applies to declarations of pointer types. This attribute
477 * tells the compiler that the raw pointer is a weak reference, and that
478 * property is somehow enforced by the code. This can make the compiler
479 * ignore these pointers when validating the usage of pointers otherwise.
480 * MOZ_UNSAFE_REF: Applies to declarations of pointer types. This attribute
481 * should be used for non-owning references that can be unsafe, and their
482 * safety needs to be validated through code inspection. The string argument
483 * passed to this macro documents the safety conditions.
484 * MOZ_NO_ADDREF_RELEASE_ON_RETURN: Applies to function declarations. Makes it
485 * a compile time error to call AddRef or Release on the return value of a
486 * function. This is intended to be used with operator->() of our smart
487 * pointer classes to ensure that the refcount of an object wrapped in a
488 * smart pointer is not manipulated directly.
490 #ifdef MOZ_CLANG_PLUGIN
491 # define MOZ_MUST_OVERRIDE __attribute__((annotate("moz_must_override")))
492 # define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class")))
493 # define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
494 # define MOZ_TRIVIAL_CTOR_DTOR __attribute__((annotate("moz_trivial_ctor_dtor")))
495 # ifdef DEBUG
496 /* in debug builds, these classes do have non-trivial constructors. */
497 # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS __attribute__((annotate("moz_global_class")))
498 # else
499 # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS __attribute__((annotate("moz_global_class"))) \
500 MOZ_TRIVIAL_CTOR_DTOR
501 # endif
502 # define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
503 # define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT __attribute__((annotate("moz_no_arith_expr_in_arg")))
504 # define MOZ_OWNING_REF __attribute__((annotate("moz_strong_ref")))
505 # define MOZ_NON_OWNING_REF __attribute__((annotate("moz_weak_ref")))
506 # define MOZ_UNSAFE_REF(reason) __attribute__((annotate("moz_strong_ref")))
507 # define MOZ_NO_ADDREF_RELEASE_ON_RETURN __attribute__((annotate("moz_no_addref_release_on_return")))
509 * It turns out that clang doesn't like void func() __attribute__ {} without a
510 * warning, so use pragmas to disable the warning. This code won't work on GCC
511 * anyways, so the warning is safe to ignore.
513 # define MOZ_HEAP_ALLOCATOR \
514 _Pragma("clang diagnostic push") \
515 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
516 __attribute__((annotate("moz_heap_allocator"))) \
517 _Pragma("clang diagnostic pop")
518 #else
519 # define MOZ_MUST_OVERRIDE /* nothing */
520 # define MOZ_STACK_CLASS /* nothing */
521 # define MOZ_NONHEAP_CLASS /* nothing */
522 # define MOZ_TRIVIAL_CTOR_DTOR /* nothing */
523 # define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS /* nothing */
524 # define MOZ_IMPLICIT /* nothing */
525 # define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT /* nothing */
526 # define MOZ_HEAP_ALLOCATOR /* nothing */
527 # define MOZ_OWNING_REF /* nothing */
528 # define MOZ_NON_OWNING_REF /* nothing */
529 # define MOZ_UNSAFE_REF(reason) /* nothing */
530 # define MOZ_NO_ADDREF_RELEASE_ON_RETURN /* nothing */
531 #endif /* MOZ_CLANG_PLUGIN */
533 #endif /* __cplusplus */
535 #endif /* mozilla_Attributes_h */