Skip to content

Commit

Permalink
Simplify some mojo macros by assuming C++11 or C11 are used.
Browse files Browse the repository at this point in the history
No intended behavior change.

Bug: none
Change-Id: I6a3f8b5c56a760920a7d21fc88b348b3168f8846
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1537997
Auto-Submit: Nico Weber <thakis@chromium.org>
Commit-Queue: Ken Rockot <rockot@google.com>
Reviewed-by: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#643872}
  • Loading branch information
nico authored and Commit Bot committed Mar 25, 2019
1 parent 5cc60a2 commit c882973
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions mojo/public/c/system/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,37 @@

#include <stddef.h>

#if !defined(__cplusplus)
#include <assert.h> // Defines static_assert() in C11.
#include <stdalign.h> // Defines alignof() in C11.
#endif

// Assert things at compile time. (|msg| should be a valid identifier name.)
// Use like:
// MOJO_STATIC_ASSERT(sizeof(struct Foo) == 12, "Foo has invalid size");
#if defined(__cplusplus)
#define MOJO_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
#elif defined(__clang__)
// TODO(thakis): Use #include <assert.h> and static_assert() in C11 mode
// (__STDC_VERSION__>= 201112L) once https://reviews.llvm.org/D17444 made its
// way into Chromium.
#define MOJO_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
#else
#define MOJO_STATIC_ASSERT(expr, msg)
#endif

// Defines a pointer-sized struct field of the given type. This ensures that the
// field has an 8-byte footprint on both 32-bit and 64-bit systems, using an
// anonymous bitfield of either 32 or 0 bits, depending on pointer size. Weird
// formatting here courtesy of clang-format.
// anonymous bitfield of either 32 or 0 bits, depending on pointer size.
// clang-format off
#define MOJO_POINTER_FIELD(type, name) \
type name; \
uint32_t: \
(sizeof(void*) == 4 ? 32 : 0)
uint32_t : (sizeof(void*) == 4 ? 32 : 0)
// clang-format on

// Like the C++11 |alignof| operator.
#if __cplusplus >= 201103L
#define MOJO_ALIGNOF(type) alignof(type)
#elif defined(__GNUC__)
#define MOJO_ALIGNOF(type) __alignof__(type)
#elif defined(_MSC_VER)
// The use of |sizeof| is to work around a bug in MSVC 2010 (see
// http://goo.gl/isH0C; supposedly fixed since then).
#define MOJO_ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type))
#else
#error "Please define MOJO_ALIGNOF() for your compiler."
#endif

// Specify the alignment of a |struct|, etc.
// Use like:
// struct MOJO_ALIGNAS(8) Foo { ... };
// Unlike the C++11 |alignas()|, |alignment| must be an integer. It may not be a
// type, nor can it be an expression like |MOJO_ALIGNOF(type)| (due to the
// non-C++11 MSVS version).
#if __cplusplus >= 201103L
// This can't use alignas() in C11 mode because unlike the C++11 version the
// C11 version can't be used on struct declarations.
#if defined(__cplusplus)
#define MOJO_ALIGNAS(alignment) alignas(alignment)
#elif defined(__GNUC__)
#define MOJO_ALIGNAS(alignment) __attribute__((aligned(alignment)))
Expand Down

0 comments on commit c882973

Please sign in to comment.