Define portable macros for packed struct definitions:
authorXinliang David Li <davidxl@google.com>
Wed, 4 Nov 2015 23:42:56 +0000 (23:42 +0000)
committerXinliang David Li <davidxl@google.com>
Wed, 4 Nov 2015 23:42:56 +0000 (23:42 +0000)
 1. A macro with argument: LLVM_PACKED(StructDefinition)
 2. A pair of macros defining scope of region with packing:
    LLVM_PACKED_START
     struct A { ... };
     struct B { ... };
    LLVM_PACKED_END

Differential Revision: http://reviews.llvm.org/D14337

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252099 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Compiler.h

index f744237e583fb5572d0f632fe5f1b24736b2059e..99a02f7ada03b6542d7c6333d10bc385b2510ad4 100644 (file)
 # define LLVM_ALIGNAS(x) alignas(x)
 #endif
 
+/// \macro LLVM_PACKED
+/// \brief Used to specify a packed structure.
+/// LLVM_PACKED(
+///    struct A {
+///      int i;
+///      int j;
+///      int k;
+///      long long l;
+///   });
+///
+/// LLVM_PACKED_START
+/// struct B {
+///   int i;
+///   int j;
+///   int k;
+///   long long l;
+/// };
+/// LLVM_PACKED_END 
+#ifdef _MSC_VER
+# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
+# define LLVM_PACKED_START __pragma(pack(push, 1))
+# define LLVM_PACKED_END   __pragma(pack(pop))
+#else
+# define LLVM_PACKED(d) d __attribute__((packed))
+# define LLVM_PACKED_START _Pragma("pack(push, 1)")
+# define LLVM_PACKED_END   _Pragma("pack(pop)")
+#endif
+
 /// \macro LLVM_PTR_SIZE
 /// \brief A constant integer equivalent to the value of sizeof(void*).
 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in