michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Implementations of various class and method modifier attributes. */ michael@0: michael@0: #ifndef mozilla_Attributes_h michael@0: #define mozilla_Attributes_h michael@0: michael@0: #include "mozilla/Compiler.h" michael@0: michael@0: /* michael@0: * MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the michael@0: * method decorated with it must be inlined, even if the compiler thinks michael@0: * otherwise. This is only a (much) stronger version of the inline hint: michael@0: * compilers are not guaranteed to respect it (although they're much more likely michael@0: * to do so). michael@0: * michael@0: * The MOZ_ALWAYS_INLINE_EVEN_DEBUG macro is yet stronger. It tells the michael@0: * compiler to inline even in DEBUG builds. It should be used very rarely. michael@0: */ michael@0: #if defined(_MSC_VER) michael@0: # define MOZ_ALWAYS_INLINE_EVEN_DEBUG __forceinline michael@0: #elif defined(__GNUC__) michael@0: # define MOZ_ALWAYS_INLINE_EVEN_DEBUG __attribute__((always_inline)) inline michael@0: #else michael@0: # define MOZ_ALWAYS_INLINE_EVEN_DEBUG inline michael@0: #endif michael@0: michael@0: #if !defined(DEBUG) michael@0: # define MOZ_ALWAYS_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG michael@0: #elif defined(_MSC_VER) && !defined(__cplusplus) michael@0: # define MOZ_ALWAYS_INLINE __inline michael@0: #else michael@0: # define MOZ_ALWAYS_INLINE inline michael@0: #endif michael@0: michael@0: /* michael@0: * g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality michael@0: * without warnings (functionality used by the macros below). These modes are michael@0: * detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more michael@0: * standardly, by checking whether __cplusplus has a C++11 or greater value. michael@0: * Current versions of g++ do not correctly set __cplusplus, so we check both michael@0: * for forward compatibility. michael@0: */ michael@0: #if defined(__clang__) michael@0: /* michael@0: * Per Clang documentation, "Note that marketing version numbers should not michael@0: * be used to check for language features, as different vendors use different michael@0: * numbering schemes. Instead, use the feature checking macros." michael@0: */ michael@0: # ifndef __has_extension michael@0: # define __has_extension __has_feature /* compatibility, for older versions of clang */ michael@0: # endif michael@0: # if __has_extension(cxx_constexpr) michael@0: # define MOZ_HAVE_CXX11_CONSTEXPR michael@0: # endif michael@0: # if __has_extension(cxx_explicit_conversions) michael@0: # define MOZ_HAVE_EXPLICIT_CONVERSION michael@0: # endif michael@0: # if __has_extension(cxx_deleted_functions) michael@0: # define MOZ_HAVE_CXX11_DELETE michael@0: # endif michael@0: # if __has_extension(cxx_override_control) michael@0: # define MOZ_HAVE_CXX11_OVERRIDE michael@0: # define MOZ_HAVE_CXX11_FINAL final michael@0: # endif michael@0: # if __has_attribute(noinline) michael@0: # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline)) michael@0: # endif michael@0: # if __has_attribute(noreturn) michael@0: # define MOZ_HAVE_NORETURN __attribute__((noreturn)) michael@0: # endif michael@0: #elif defined(__GNUC__) michael@0: # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L michael@0: # if MOZ_GCC_VERSION_AT_LEAST(4, 7, 0) michael@0: # define MOZ_HAVE_CXX11_OVERRIDE michael@0: # define MOZ_HAVE_CXX11_FINAL final michael@0: # endif michael@0: # if MOZ_GCC_VERSION_AT_LEAST(4, 6, 0) michael@0: # define MOZ_HAVE_CXX11_CONSTEXPR michael@0: # endif michael@0: # if MOZ_GCC_VERSION_AT_LEAST(4, 5, 0) michael@0: # define MOZ_HAVE_EXPLICIT_CONVERSION michael@0: # endif michael@0: # define MOZ_HAVE_CXX11_DELETE michael@0: # else michael@0: /* __final is a non-C++11 GCC synonym for 'final', per GCC r176655. */ michael@0: # if MOZ_GCC_VERSION_AT_LEAST(4, 7, 0) michael@0: # define MOZ_HAVE_CXX11_FINAL __final michael@0: # endif michael@0: # endif michael@0: # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline)) michael@0: # define MOZ_HAVE_NORETURN __attribute__((noreturn)) michael@0: #elif defined(_MSC_VER) michael@0: # if _MSC_VER >= 1800 michael@0: # define MOZ_HAVE_CXX11_DELETE michael@0: # endif michael@0: # if _MSC_VER >= 1700 michael@0: # define MOZ_HAVE_CXX11_FINAL final michael@0: # else michael@0: /* MSVC <= 10 used to spell "final" as "sealed". */ michael@0: # define MOZ_HAVE_CXX11_FINAL sealed michael@0: # endif michael@0: # define MOZ_HAVE_CXX11_OVERRIDE michael@0: # define MOZ_HAVE_NEVER_INLINE __declspec(noinline) michael@0: # define MOZ_HAVE_NORETURN __declspec(noreturn) michael@0: // Staying away from explicit conversion operators in MSVC for now, see michael@0: // http://stackoverflow.com/questions/20498142/visual-studio-2013-explicit-keyword-bug michael@0: #endif michael@0: michael@0: /* michael@0: * The MOZ_CONSTEXPR specifier declares that a C++11 compiler can evaluate a michael@0: * function at compile time. A constexpr function cannot examine any values michael@0: * except its arguments and can have no side effects except its return value. michael@0: * The MOZ_CONSTEXPR_VAR specifier tells a C++11 compiler that a variable's michael@0: * value may be computed at compile time. It should be prefered to just michael@0: * marking variables as MOZ_CONSTEXPR because if the compiler does not support michael@0: * constexpr it will fall back to making the variable const, and some compilers michael@0: * do not accept variables being marked both const and constexpr. michael@0: */ michael@0: #ifdef MOZ_HAVE_CXX11_CONSTEXPR michael@0: # define MOZ_CONSTEXPR constexpr michael@0: # define MOZ_CONSTEXPR_VAR constexpr michael@0: #else michael@0: # define MOZ_CONSTEXPR /* no support */ michael@0: # define MOZ_CONSTEXPR_VAR const michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_EXPLICIT_CONVERSION is a specifier on a type conversion michael@0: * overloaded operator that declares that a C++11 compiler should restrict michael@0: * this operator to allow only explicit type conversions, disallowing michael@0: * implicit conversions. michael@0: * michael@0: * Example: michael@0: * michael@0: * template michael@0: * class Ptr michael@0: * { michael@0: * T* ptr; michael@0: * MOZ_EXPLICIT_CONVERSION operator bool() const { michael@0: * return ptr != nullptr; michael@0: * } michael@0: * }; michael@0: * michael@0: */ michael@0: #ifdef MOZ_HAVE_EXPLICIT_CONVERSION michael@0: # define MOZ_EXPLICIT_CONVERSION explicit michael@0: #else michael@0: # define MOZ_EXPLICIT_CONVERSION /* no support */ michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the michael@0: * method decorated with it must never be inlined, even if the compiler would michael@0: * otherwise choose to inline the method. Compilers aren't absolutely michael@0: * guaranteed to support this, but most do. michael@0: */ michael@0: #if defined(MOZ_HAVE_NEVER_INLINE) michael@0: # define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE michael@0: #else michael@0: # define MOZ_NEVER_INLINE /* no support */ michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_NORETURN, specified at the start of a function declaration, indicates michael@0: * that the given function does not return. (The function definition does not michael@0: * need to be annotated.) michael@0: * michael@0: * MOZ_NORETURN void abort(const char* msg); michael@0: * michael@0: * This modifier permits the compiler to optimize code assuming a call to such a michael@0: * function will never return. It also enables the compiler to avoid spurious michael@0: * warnings about not initializing variables, or about any other seemingly-dodgy michael@0: * operations performed after the function returns. michael@0: * michael@0: * This modifier does not affect the corresponding function's linking behavior. michael@0: */ michael@0: #if defined(MOZ_HAVE_NORETURN) michael@0: # define MOZ_NORETURN MOZ_HAVE_NORETURN michael@0: #else michael@0: # define MOZ_NORETURN /* no support */ michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time michael@0: * instrumentation shipped with Clang and GCC) to not instrument the annotated michael@0: * function. Furthermore, it will prevent the compiler from inlining the michael@0: * function because inlining currently breaks the blacklisting mechanism of michael@0: * AddressSanitizer. michael@0: */ michael@0: #if defined(__has_feature) michael@0: # if __has_feature(address_sanitizer) michael@0: # define MOZ_HAVE_ASAN_BLACKLIST michael@0: # endif michael@0: #elif defined(__GNUC__) michael@0: # if defined(__SANITIZE_ADDRESS__) michael@0: # define MOZ_HAVE_ASAN_BLACKLIST michael@0: # endif michael@0: #endif michael@0: michael@0: #if defined(MOZ_HAVE_ASAN_BLACKLIST) michael@0: # define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_address)) michael@0: #else michael@0: # define MOZ_ASAN_BLACKLIST /* nothing */ michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_TSAN_BLACKLIST is a macro to tell ThreadSanitizer (a compile-time michael@0: * instrumentation shipped with Clang) to not instrument the annotated function. michael@0: * Furthermore, it will prevent the compiler from inlining the function because michael@0: * inlining currently breaks the blacklisting mechanism of ThreadSanitizer. michael@0: */ michael@0: #if defined(__has_feature) michael@0: # if __has_feature(thread_sanitizer) michael@0: # define MOZ_TSAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_thread)) michael@0: # else michael@0: # define MOZ_TSAN_BLACKLIST /* nothing */ michael@0: # endif michael@0: #else michael@0: # define MOZ_TSAN_BLACKLIST /* nothing */ michael@0: #endif michael@0: michael@0: #ifdef __cplusplus michael@0: michael@0: /* michael@0: * MOZ_DELETE, specified immediately prior to the ';' terminating an undefined- michael@0: * method declaration, attempts to delete that method from the corresponding michael@0: * class. An attempt to use the method will always produce an error *at compile michael@0: * time* (instead of sometimes as late as link time) when this macro can be michael@0: * implemented. For example, you can use MOZ_DELETE to produce classes with no michael@0: * implicit copy constructor or assignment operator: michael@0: * michael@0: * struct NonCopyable michael@0: * { michael@0: * private: michael@0: * NonCopyable(const NonCopyable& other) MOZ_DELETE; michael@0: * void operator=(const NonCopyable& other) MOZ_DELETE; michael@0: * }; michael@0: * michael@0: * If MOZ_DELETE can't be implemented for the current compiler, use of the michael@0: * annotated method will still cause an error, but the error might occur at link michael@0: * time in some cases rather than at compile time. michael@0: * michael@0: * MOZ_DELETE relies on C++11 functionality not universally implemented. As a michael@0: * backstop, method declarations using MOZ_DELETE should be private. michael@0: */ michael@0: #if defined(MOZ_HAVE_CXX11_DELETE) michael@0: # define MOZ_DELETE = delete michael@0: #else michael@0: # define MOZ_DELETE /* no support */ michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_OVERRIDE explicitly indicates that a virtual member function in a class michael@0: * overrides a member function of a base class, rather than potentially being a michael@0: * new member function. MOZ_OVERRIDE should be placed immediately before the michael@0: * ';' terminating the member function's declaration, or before '= 0;' if the michael@0: * member function is pure. If the member function is defined in the class michael@0: * definition, it should appear before the opening brace of the function body. michael@0: * michael@0: * class Base michael@0: * { michael@0: * public: michael@0: * virtual void f() = 0; michael@0: * }; michael@0: * class Derived1 : public Base michael@0: * { michael@0: * public: michael@0: * virtual void f() MOZ_OVERRIDE; michael@0: * }; michael@0: * class Derived2 : public Base michael@0: * { michael@0: * public: michael@0: * virtual void f() MOZ_OVERRIDE = 0; michael@0: * }; michael@0: * class Derived3 : public Base michael@0: * { michael@0: * public: michael@0: * virtual void f() MOZ_OVERRIDE { } michael@0: * }; michael@0: * michael@0: * In compilers supporting C++11 override controls, MOZ_OVERRIDE *requires* that michael@0: * the function marked with it override a member function of a base class: it michael@0: * is a compile error if it does not. Otherwise MOZ_OVERRIDE does not affect michael@0: * semantics and merely documents the override relationship to the reader (but michael@0: * of course must still be used correctly to not break C++11 compilers). michael@0: */ michael@0: #if defined(MOZ_HAVE_CXX11_OVERRIDE) michael@0: # define MOZ_OVERRIDE override michael@0: #else michael@0: # define MOZ_OVERRIDE /* no support */ michael@0: #endif michael@0: michael@0: /* michael@0: * MOZ_FINAL indicates that some functionality cannot be overridden through michael@0: * inheritance. It can be used to annotate either classes/structs or virtual michael@0: * member functions. michael@0: * michael@0: * To annotate a class/struct with MOZ_FINAL, place MOZ_FINAL immediately after michael@0: * the name of the class, before the list of classes from which it derives (if michael@0: * any) and before its opening brace. MOZ_FINAL must not be used to annotate michael@0: * unnamed classes or structs. (With some compilers, and with C++11 proper, the michael@0: * underlying expansion is ambiguous with specifying a class name.) michael@0: * michael@0: * class Base MOZ_FINAL michael@0: * { michael@0: * public: michael@0: * Base(); michael@0: * ~Base(); michael@0: * virtual void f() { } michael@0: * }; michael@0: * // This will be an error in some compilers: michael@0: * class Derived : public Base michael@0: * { michael@0: * public: michael@0: * ~Derived() { } michael@0: * }; michael@0: * michael@0: * One particularly common reason to specify MOZ_FINAL upon a class is to tell michael@0: * the compiler that it's not dangerous for it to have a non-virtual destructor michael@0: * yet have one or more virtual functions, silencing the warning it might emit michael@0: * in this case. Suppose Base above weren't annotated with MOZ_FINAL. Because michael@0: * ~Base() is non-virtual, an attempt to delete a Derived* through a Base* michael@0: * wouldn't call ~Derived(), so any cleanup ~Derived() might do wouldn't happen. michael@0: * (Formally C++ says behavior is undefined, but compilers will likely just call michael@0: * ~Base() and not ~Derived().) Specifying MOZ_FINAL tells the compiler that michael@0: * it's safe for the destructor to be non-virtual. michael@0: * michael@0: * In compilers implementing final controls, it is an error to inherit from a michael@0: * class annotated with MOZ_FINAL. In other compilers it serves only as michael@0: * documentation. michael@0: * michael@0: * To annotate a virtual member function with MOZ_FINAL, place MOZ_FINAL michael@0: * immediately before the ';' terminating the member function's declaration, or michael@0: * before '= 0;' if the member function is pure. If the member function is michael@0: * defined in the class definition, it should appear before the opening brace of michael@0: * the function body. (This placement is identical to that for MOZ_OVERRIDE. michael@0: * If both are used, they should appear in the order 'MOZ_FINAL MOZ_OVERRIDE' michael@0: * for consistency.) michael@0: * michael@0: * class Base michael@0: * { michael@0: * public: michael@0: * virtual void f() MOZ_FINAL; michael@0: * }; michael@0: * class Derived michael@0: * { michael@0: * public: michael@0: * // This will be an error in some compilers: michael@0: * virtual void f(); michael@0: * }; michael@0: * michael@0: * In compilers implementing final controls, it is an error for a derived class michael@0: * to override a method annotated with MOZ_FINAL. In other compilers it serves michael@0: * only as documentation. michael@0: */ michael@0: #if defined(MOZ_HAVE_CXX11_FINAL) michael@0: # define MOZ_FINAL MOZ_HAVE_CXX11_FINAL michael@0: #else michael@0: # define MOZ_FINAL /* no support */ michael@0: #endif michael@0: michael@0: /** michael@0: * MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's michael@0: * return value is not used by the caller. michael@0: * michael@0: * Place this attribute at the very beginning of a function definition. For michael@0: * example, write michael@0: * michael@0: * MOZ_WARN_UNUSED_RESULT int foo(); michael@0: * michael@0: * or michael@0: * michael@0: * MOZ_WARN_UNUSED_RESULT int foo() { return 42; } michael@0: */ michael@0: #if defined(__GNUC__) || defined(__clang__) michael@0: # define MOZ_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result)) michael@0: #else michael@0: # define MOZ_WARN_UNUSED_RESULT michael@0: #endif michael@0: michael@0: /* michael@0: * The following macros are attributes that support the static analysis plugin michael@0: * included with Mozilla, and will be implemented (when such support is enabled) michael@0: * as C++11 attributes. Since such attributes are legal pretty much everywhere michael@0: * and have subtly different semantics depending on their placement, the michael@0: * following is a guide on where to place the attributes. michael@0: * michael@0: * Attributes that apply to a struct or class precede the name of the class: michael@0: * (Note that this is different from the placement of MOZ_FINAL for classes!) michael@0: * michael@0: * class MOZ_CLASS_ATTRIBUTE SomeClass {}; michael@0: * michael@0: * Attributes that apply to functions follow the parentheses and const michael@0: * qualifiers but precede MOZ_FINAL, MOZ_OVERRIDE and the function body: michael@0: * michael@0: * void DeclaredFunction() MOZ_FUNCTION_ATTRIBUTE; michael@0: * void SomeFunction() MOZ_FUNCTION_ATTRIBUTE {} michael@0: * void PureFunction() const MOZ_FUNCTION_ATTRIBUTE = 0; michael@0: * void OverriddenFunction() MOZ_FUNCTION_ATTIRBUTE MOZ_OVERRIDE; michael@0: * michael@0: * Attributes that apply to variables or parameters follow the variable's name: michael@0: * michael@0: * int variable MOZ_VARIABLE_ATTRIBUTE; michael@0: * michael@0: * Attributes that apply to types follow the type name: michael@0: * michael@0: * typedef int MOZ_TYPE_ATTRIBUTE MagicInt; michael@0: * int MOZ_TYPE_ATTRIBUTE someVariable; michael@0: * int * MOZ_TYPE_ATTRIBUTE magicPtrInt; michael@0: * int MOZ_TYPE_ATTRIBUTE * ptrToMagicInt; michael@0: * michael@0: * Attributes that apply to statements precede the statement: michael@0: * michael@0: * MOZ_IF_ATTRIBUTE if (x == 0) michael@0: * MOZ_DO_ATTRIBUTE do { } while(0); michael@0: * michael@0: * Attributes that apply to labels precede the label: michael@0: * michael@0: * MOZ_LABEL_ATTRIBUTE target: michael@0: * goto target; michael@0: * MOZ_CASE_ATTRIBUTE case 5: michael@0: * MOZ_DEFAULT_ATTRIBUTE default: michael@0: * michael@0: * The static analyses that are performed by the plugin are as follows: michael@0: * michael@0: * MOZ_MUST_OVERRIDE: Applies to all C++ member functions. All immediate michael@0: * subclasses must provide an exact override of this method; if a subclass michael@0: * does not override this method, the compiler will emit an error. This michael@0: * attribute is not limited to virtual methods, so if it is applied to a michael@0: * nonvirtual method and the subclass does not provide an equivalent michael@0: * definition, the compiler will emit an error. michael@0: * MOZ_STACK_CLASS: Applies to all classes. Any class with this annotation is michael@0: * expected to live on the stack, so it is a compile-time error to use it, or michael@0: * an array of such objects, as a global or static variable, or as the type of michael@0: * a new expression (unless placement new is being used). If a member of michael@0: * another class uses this class, or if another class inherits from this michael@0: * class, then it is considered to be a stack class as well, although this michael@0: * attribute need not be provided in such cases. michael@0: * MOZ_NONHEAP_CLASS: Applies to all classes. Any class with this annotation is michael@0: * expected to live on the stack or in static storage, so it is a compile-time michael@0: * error to use it, or an array of such objects, as the type of a new michael@0: * expression (unless placement new is being used). If a member of another michael@0: * class uses this class, or if another class inherits from this class, then michael@0: * it is considered to be a non-heap class as well, although this attribute michael@0: * need not be provided in such cases. michael@0: * MOZ_HEAP_ALLOCATOR: Applies to any function. This indicates that the return michael@0: * value is allocated on the heap, and will as a result check such allocations michael@0: * during MOZ_STACK_CLASS and MOZ_NONHEAP_CLASS annotation checking. michael@0: */ michael@0: #ifdef MOZ_CLANG_PLUGIN michael@0: # define MOZ_MUST_OVERRIDE __attribute__((annotate("moz_must_override"))) michael@0: # define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class"))) michael@0: # define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class"))) michael@0: /* michael@0: * It turns out that clang doesn't like void func() __attribute__ {} without a michael@0: * warning, so use pragmas to disable the warning. This code won't work on GCC michael@0: * anyways, so the warning is safe to ignore. michael@0: */ michael@0: # define MOZ_HEAP_ALLOCATOR \ michael@0: _Pragma("clang diagnostic push") \ michael@0: _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ michael@0: __attribute__((annotate("moz_heap_allocator"))) \ michael@0: _Pragma("clang diagnostic pop") michael@0: #else michael@0: # define MOZ_MUST_OVERRIDE /* nothing */ michael@0: # define MOZ_STACK_CLASS /* nothing */ michael@0: # define MOZ_NONHEAP_CLASS /* nothing */ michael@0: # define MOZ_HEAP_ALLOCATOR /* nothing */ michael@0: #endif /* MOZ_CLANG_PLUGIN */ michael@0: michael@0: /* michael@0: * MOZ_THIS_IN_INITIALIZER_LIST is used to avoid a warning when we know that michael@0: * it's safe to use 'this' in an initializer list. michael@0: */ michael@0: #ifdef _MSC_VER michael@0: # define MOZ_THIS_IN_INITIALIZER_LIST() \ michael@0: __pragma(warning(push)) \ michael@0: __pragma(warning(disable:4355)) \ michael@0: this \ michael@0: __pragma(warning(pop)) michael@0: #else michael@0: # define MOZ_THIS_IN_INITIALIZER_LIST() this michael@0: #endif michael@0: michael@0: #endif /* __cplusplus */ michael@0: michael@0: #endif /* mozilla_Attributes_h */