[Support] Workaround a GCC 4.8 bug on constant expression evaluation.
authorMichael Liao <michael.hliao@gmail.com>
Tue, 30 Jul 2019 16:11:48 +0000 (16:11 +0000)
committerMichael Liao <michael.hliao@gmail.com>
Tue, 30 Jul 2019 16:11:48 +0000 (16:11 +0000)
commit0d6615cc1911baaa53fb0141a288b2cd7c072a13
tree19ebaa652633bc028fab72ef8c96dc6684f87652
parent5ed3d146f87e3103ea5bbdb27bca8d6b1b1f714a
[Support] Workaround a GCC 4.8 bug on constant expression evaluation.

Summary:
- The following stripped code trigger a gcc-4.8 bug. To work that
  around, move the alignment evaluation into template parameter.

```
// https://godbolt.org/z/58p5_X
//

enum { aligned = 0, unaligned = 1 };

template <typename T, int alignment> struct PickAlignment {
  enum { value = alignment == 0 ? alignof(T) : alignment };
};

template <typename ValueType, std::size_t Alignment> struct packed {
private:
  struct {
    alignas(
        PickAlignment<ValueType, Alignment>::value) char buffer[sizeof(int)];
  } Value;
};

using ule16_t = packed<uint16_t, unaligned>;

ule16_t x;
```

- Also, replace `alignas` with `LLVMALIGN_AS` to improve the compiler
  compatibility.

Reviewers: jfb

Subscribers: dexonsmith, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65452

llvm-svn: 367329
llvm/include/llvm/Support/Endian.h