1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_C_SYSTEM_MACROS_H_ 6 #define MOJO_PUBLIC_C_SYSTEM_MACROS_H_ 7 8 #include <stddef.h> 9 10 // Annotate a variable indicating it's okay if it's unused. 11 // Use like: 12 // int x MOJO_ALLOW_UNUSED = ...; 13 #if defined(__GNUC__) 14 #define MOJO_ALLOW_UNUSED __attribute__((unused)) 15 #else 16 #define MOJO_ALLOW_UNUSED 17 #endif 18 19 // Annotate a function indicating that the caller must examine the return value. 20 // Use like: 21 // int foo() MOJO_WARN_UNUSED_RESULT; 22 // Note that it can only be used on the prototype, and not the definition. 23 #if defined(__GNUC__) 24 #define MOJO_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 25 #else 26 #define MOJO_WARN_UNUSED_RESULT 27 #endif 28 29 #ifdef __cplusplus 30 // Used to explicitly mark the return value of a function as unused. If you are 31 // really sure you don't want to do anything with the return value of a function 32 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: 33 // 34 // scoped_ptr<MyType> my_var = ...; 35 // if (TakeOwnership(my_var.get()) == SUCCESS) 36 // mojo_ignore_result(my_var.release()); 37 // 38 template <typename T> 39 inline void mojo_ignore_result(const T&) { 40 } 41 #endif 42 43 // Assert things at compile time. (|msg| should be a valid identifier name.) 44 // This macro is currently C++-only, but we want to use it in the C core.h. 45 // Use like: 46 // MOJO_COMPILE_ASSERT(sizeof(Foo) == 12, Foo_has_invalid_size); 47 #if __cplusplus >= 201103L 48 #define MOJO_COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) 49 #elif defined(__cplusplus) 50 namespace mojo { 51 template <bool> 52 struct CompileAssert {}; 53 } 54 #define MOJO_COMPILE_ASSERT(expr, msg) \ 55 typedef ::mojo::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 56 #else 57 #define MOJO_COMPILE_ASSERT(expr, msg) 58 #endif 59 60 // Like the C++11 |alignof| operator. 61 #if __cplusplus >= 201103L 62 #define MOJO_ALIGNOF(type) alignof(type) 63 #elif defined(__GNUC__) 64 #define MOJO_ALIGNOF(type) __alignof__(type) 65 #elif defined(_MSC_VER) 66 // The use of |sizeof| is to work around a bug in MSVC 2010 (see 67 // http://goo.gl/isH0C; supposedly fixed since then). 68 #define MOJO_ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) 69 #else 70 #error "Please define MOJO_ALIGNOF() for your compiler." 71 #endif 72 73 // Specify the alignment of a |struct|, etc. 74 // Use like: 75 // struct MOJO_ALIGNAS(8) Foo { ... }; 76 // Unlike the C++11 |alignas()|, |alignment| must be an integer. It may not be a 77 // type, nor can it be an expression like |MOJO_ALIGNOF(type)| (due to the 78 // non-C++11 MSVS version). 79 #if __cplusplus >= 201103L 80 #define MOJO_ALIGNAS(alignment) alignas(alignment) 81 #elif defined(__GNUC__) 82 #define MOJO_ALIGNAS(alignment) __attribute__((aligned(alignment))) 83 #elif defined(_MSC_VER) 84 #define MOJO_ALIGNAS(alignment) __declspec(align(alignment)) 85 #else 86 #error "Please define MOJO_ALIGNAS() for your compiler." 87 #endif 88 89 #endif // MOJO_PUBLIC_C_SYSTEM_MACROS_H_ 90