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