1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99 ft=cpp:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at:
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Code.
19 * The Initial Developer of the Original Code is
20 * The Mozilla Foundation
21 * Portions created by the Initial Developer are Copyright (C) 2011
22 * the Initial Developer. All Rights Reserved.
25 * Jeff Walden <jwalden+code@mit.edu> (original author)
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 /* Implementations of various class and method modifier attributes. */
43 #ifndef mozilla_Attributes_h_
44 #define mozilla_Attributes_h_
47 * This header does not include any other headers so that it can be included by
48 * code that is (only currently) mfbt-incompatible.
52 * MOZ_INLINE is a macro which expands to tell the compiler that the method
53 * decorated with it should be inlined. This macro is usable from C and C++
54 * code, even though C89 does not support the |inline| keyword. The compiler
55 * may ignore this directive if it chooses.
57 #if defined(__cplusplus)
58 # define MOZ_INLINE inline
59 #elif defined(_MSC_VER)
60 # define MOZ_INLINE __inline
61 #elif defined(__GNUC__)
62 # define MOZ_INLINE __inline__
64 # define MOZ_INLINE inline
68 * MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the
69 * method decorated with it must be inlined, even if the compiler thinks
70 * otherwise. This is only a (much) stronger version of the MOZ_INLINE hint:
71 * compilers are not guaranteed to respect it (although they're much more likely
75 # define MOZ_ALWAYS_INLINE MOZ_INLINE
76 #elif defined(_MSC_VER)
77 # define MOZ_ALWAYS_INLINE __forceinline
78 #elif defined(__GNUC__)
79 # define MOZ_ALWAYS_INLINE __attribute__((always_inline)) MOZ_INLINE
81 # define MOZ_ALWAYS_INLINE MOZ_INLINE
85 * g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality
86 * without warnings (functionality used by the macros below). These modes are
87 * detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more
88 * standardly, by checking whether __cplusplus has a C++11 or greater value.
89 * Current versions of g++ do not correctly set __cplusplus, so we check both
90 * for forward compatibility.
92 #if defined(__clang__)
94 * Per Clang documentation, "Note that marketing version numbers should not
95 * be used to check for language features, as different vendors use different
96 * numbering schemes. Instead, use the feature checking macros."
98 # ifndef __has_extension
99 # define __has_extension __has_feature /* compatibility, for older versions of clang */
101 # if __has_extension(cxx_deleted_functions)
102 # define MOZ_HAVE_CXX11_DELETE
104 # if __has_extension(cxx_override_control)
105 # define MOZ_HAVE_CXX11_OVERRIDE
106 # define MOZ_HAVE_CXX11_FINAL final
108 # if __has_attribute(noinline)
109 # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
111 # if __has_attribute(noreturn)
112 # define MOZ_HAVE_NORETURN __attribute__((noreturn))
114 #elif defined(__GNUC__)
115 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
117 # define MOZ_HAVE_CXX11_DELETE
118 # define MOZ_HAVE_CXX11_OVERRIDE
119 # define MOZ_HAVE_CXX11_FINAL final
121 # if __GNUC_MINOR__ >= 7
122 # define MOZ_HAVE_CXX11_OVERRIDE
123 # define MOZ_HAVE_CXX11_FINAL final
125 # if __GNUC_MINOR__ >= 4
126 # define MOZ_HAVE_CXX11_DELETE
130 /* __final is a non-C++11 GCC synonym for 'final', per GCC r176655. */
132 # define MOZ_HAVE_CXX11_FINAL __final
134 # if __GNUC_MINOR__ >= 7
135 # define MOZ_HAVE_CXX11_FINAL __final
139 # define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
140 # define MOZ_HAVE_NORETURN __attribute__((noreturn))
141 #elif defined(_MSC_VER)
142 # if _MSC_VER >= 1400
143 # define MOZ_HAVE_CXX11_OVERRIDE
144 /* MSVC currently spells "final" as "sealed". */
145 # define MOZ_HAVE_CXX11_FINAL sealed
147 # define MOZ_HAVE_NEVER_INLINE __declspec(noinline)
148 # define MOZ_HAVE_NORETURN __declspec(noreturn)
152 * MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the
153 * method decorated with it must never be inlined, even if the compiler would
154 * otherwise choose to inline the method. Compilers aren't absolutely
155 * guaranteed to support this, but most do.
157 #if defined(MOZ_HAVE_NEVER_INLINE)
158 # define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE
160 # define MOZ_NEVER_INLINE /* no support */
164 * MOZ_NORETURN, specified at the start of a function declaration, indicates
165 * that the given function does not return. (The function definition does not
166 * need to be annotated.)
168 * MOZ_NORETURN void abort(const char* msg);
170 * This modifier permits the compiler to optimize code assuming a call to such a
171 * function will never return. It also enables the compiler to avoid spurious
172 * warnings about not initializing variables, or about any other seemingly-dodgy
173 * operations performed after the function returns.
175 * This modifier does not affect the corresponding function's linking behavior.
177 #if defined(MOZ_HAVE_NORETURN)
178 # define MOZ_NORETURN MOZ_HAVE_NORETURN
180 # define MOZ_NORETURN /* no support */
184 * MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
185 * instrumentation shipped with Clang) to not instrument the annotated function.
186 * Furthermore, it will prevent the compiler from inlining the function because
187 * inlining currently breaks the blacklisting mechanism of AddressSanitizer.
189 #if defined(MOZ_ASAN)
190 # define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_address_safety_analysis))
192 # define MOZ_ASAN_BLACKLIST
199 * MOZ_DELETE, specified immediately prior to the ';' terminating an undefined-
200 * method declaration, attempts to delete that method from the corresponding
201 * class. An attempt to use the method will always produce an error *at compile
202 * time* (instead of sometimes as late as link time) when this macro can be
203 * implemented. For example, you can use MOZ_DELETE to produce classes with no
204 * implicit copy constructor or assignment operator:
209 * NonCopyable(const NonCopyable& other) MOZ_DELETE;
210 * void operator=(const NonCopyable& other) MOZ_DELETE;
213 * If MOZ_DELETE can't be implemented for the current compiler, use of the
214 * annotated method will still cause an error, but the error might occur at link
215 * time in some cases rather than at compile time.
217 * MOZ_DELETE relies on C++11 functionality not universally implemented. As a
218 * backstop, method declarations using MOZ_DELETE should be private.
220 #if defined(MOZ_HAVE_CXX11_DELETE)
221 # define MOZ_DELETE = delete
223 # define MOZ_DELETE /* no support */
227 * MOZ_OVERRIDE explicitly indicates that a virtual member function in a class
228 * overrides a member function of a base class, rather than potentially being a
229 * new member function. MOZ_OVERRIDE should be placed immediately before the
230 * ';' terminating the member function's declaration, or before '= 0;' if the
231 * member function is pure. If the member function is defined in the class
232 * definition, it should appear before the opening brace of the function body.
237 * virtual void f() = 0;
239 * class Derived1 : public Base
242 * virtual void f() MOZ_OVERRIDE;
244 * class Derived2 : public Base
247 * virtual void f() MOZ_OVERRIDE = 0;
249 * class Derived3 : public Base
252 * virtual void f() MOZ_OVERRIDE { }
255 * In compilers supporting C++11 override controls, MOZ_OVERRIDE *requires* that
256 * the function marked with it override a member function of a base class: it
257 * is a compile error if it does not. Otherwise MOZ_OVERRIDE does not affect
258 * semantics and merely documents the override relationship to the reader (but
259 * of course must still be used correctly to not break C++11 compilers).
261 #if defined(MOZ_HAVE_CXX11_OVERRIDE)
262 # define MOZ_OVERRIDE override
264 # define MOZ_OVERRIDE /* no support */
268 * MOZ_FINAL indicates that some functionality cannot be overridden through
269 * inheritance. It can be used to annotate either classes/structs or virtual
272 * To annotate a class/struct with MOZ_FINAL, place MOZ_FINAL immediately after
273 * the name of the class, before the list of classes from which it derives (if
274 * any) and before its opening brace. MOZ_FINAL must not be used to annotate
275 * unnamed classes or structs. (With some compilers, and with C++11 proper, the
276 * underlying expansion is ambiguous with specifying a class name.)
278 * class Base MOZ_FINAL
283 * virtual void f() { }
285 * // This will be an error in some compilers:
286 * class Derived : public Base
292 * One particularly common reason to specify MOZ_FINAL upon a class is to tell
293 * the compiler that it's not dangerous for it to have a non-virtual destructor
294 * yet have one or more virtual functions, silencing the warning it might emit
295 * in this case. Suppose Base above weren't annotated with MOZ_FINAL. Because
296 * ~Base() is non-virtual, an attempt to delete a Derived* through a Base*
297 * wouldn't call ~Derived(), so any cleanup ~Derived() might do wouldn't happen.
298 * (Formally C++ says behavior is undefined, but compilers will likely just call
299 * ~Base() and not ~Derived().) Specifying MOZ_FINAL tells the compiler that
300 * it's safe for the destructor to be non-virtual.
302 * In compilers implementing final controls, it is an error to inherit from a
303 * class annotated with MOZ_FINAL. In other compilers it serves only as
306 * To annotate a virtual member function with MOZ_FINAL, place MOZ_FINAL
307 * immediately before the ';' terminating the member function's declaration, or
308 * before '= 0;' if the member function is pure. If the member function is
309 * defined in the class definition, it should appear before the opening brace of
310 * the function body. (This placement is identical to that for MOZ_OVERRIDE.
311 * If both are used, they should appear in the order 'MOZ_FINAL MOZ_OVERRIDE'
317 * virtual void f() MOZ_FINAL;
322 * // This will be an error in some compilers:
326 * In compilers implementing final controls, it is an error for a derived class
327 * to override a method annotated with MOZ_FINAL. In other compilers it serves
328 * only as documentation.
330 #if defined(MOZ_HAVE_CXX11_FINAL)
331 # define MOZ_FINAL MOZ_HAVE_CXX11_FINAL
333 # define MOZ_FINAL /* no support */
337 * MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's
338 * return value is not used by the caller.
340 * Place this attribute at the very beginning of a function definition. For
343 * MOZ_WARN_UNUSED_RESULT int foo();
347 * MOZ_WARN_UNUSED_RESULT int foo() { return 42; }
349 #if defined(__GNUC__) || defined(__clang__)
350 # define MOZ_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
352 # define MOZ_WARN_UNUSED_RESULT
355 #endif /* __cplusplus */
357 #endif /* mozilla_Attributes_h_ */