Commit d4475816 by Geoff Lang Committed by Jamie Madill

Always use static_assert for compile-time assertions and remove META_ASSERT.

BUG=468139 Change-Id: I696ef307f2faa54bb72df66784bc79a055499987 Reviewed-on: https://chromium-review.googlesource.com/260776Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent fc1806e1
...@@ -162,23 +162,4 @@ bool DebugAnnotationsActive(); ...@@ -162,23 +162,4 @@ bool DebugAnnotationsActive();
#define HAS_DYNAMIC_TYPE(type, obj) true #define HAS_DYNAMIC_TYPE(type, obj) true
#endif #endif
// A macro functioning as a compile-time assert to validate constant conditions
#if (defined(_MSC_VER) && _MSC_VER >= 1600)
#define ANGLE_HAS_STATIC_ASSERT
#elif (defined(__GNUC__) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3))
#define ANGLE_HAS_STATIC_ASSERT
#elif defined(__clang__) && __has_feature(cxx_static_assert)
#define ANGLE_HAS_STATIC_ASSERT
#endif
#if defined(ANGLE_HAS_STATIC_ASSERT)
#define META_ASSERT_MSG(condition, msg) static_assert(condition, msg)
#else
#define META_ASSERT_CONCAT(a, b) a ## b
#define META_ASSERT_CONCAT2(a, b) META_ASSERT_CONCAT(a, b)
#define META_ASSERT_MSG(condition, msg) typedef int META_ASSERT_CONCAT2(COMPILE_TIME_ASSERT_, __LINE__)[static_cast<bool>(condition)?1:-1]
#endif
#define META_ASSERT(condition) META_ASSERT_MSG(condition, "compile time assertion failed.")
#endif // COMMON_DEBUG_H_ #endif // COMMON_DEBUG_H_
...@@ -403,7 +403,7 @@ inline float float10ToFloat32(unsigned short fp11) ...@@ -403,7 +403,7 @@ inline float float10ToFloat32(unsigned short fp11)
template <typename T> template <typename T>
inline float normalizedToFloat(T input) inline float normalizedToFloat(T input)
{ {
META_ASSERT(std::numeric_limits<T>::is_integer); static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
const float inverseMax = 1.0f / std::numeric_limits<T>::max(); const float inverseMax = 1.0f / std::numeric_limits<T>::max();
return input * inverseMax; return input * inverseMax;
...@@ -412,8 +412,8 @@ inline float normalizedToFloat(T input) ...@@ -412,8 +412,8 @@ inline float normalizedToFloat(T input)
template <unsigned int inputBitCount, typename T> template <unsigned int inputBitCount, typename T>
inline float normalizedToFloat(T input) inline float normalizedToFloat(T input)
{ {
META_ASSERT(std::numeric_limits<T>::is_integer); static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
META_ASSERT(inputBitCount < (sizeof(T) * 8)); static_assert(inputBitCount < (sizeof(T) * 8), "T must have more bits than inputBitCount.");
const float inverseMax = 1.0f / ((1 << inputBitCount) - 1); const float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
return input * inverseMax; return input * inverseMax;
...@@ -428,14 +428,15 @@ inline T floatToNormalized(float input) ...@@ -428,14 +428,15 @@ inline T floatToNormalized(float input)
template <unsigned int outputBitCount, typename T> template <unsigned int outputBitCount, typename T>
inline T floatToNormalized(float input) inline T floatToNormalized(float input)
{ {
META_ASSERT(outputBitCount < (sizeof(T) * 8)); static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount.");
return ((1 << outputBitCount) - 1) * input + 0.5f; return ((1 << outputBitCount) - 1) * input + 0.5f;
} }
template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
inline T getShiftedData(T input) inline T getShiftedData(T input)
{ {
META_ASSERT(inputBitCount + inputBitStart <= (sizeof(T) * 8)); static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
"T must have at least as many bits as inputBitCount + inputBitStart.");
const T mask = (1 << inputBitCount) - 1; const T mask = (1 << inputBitCount) - 1;
return (input >> inputBitStart) & mask; return (input >> inputBitStart) & mask;
} }
...@@ -443,7 +444,8 @@ inline T getShiftedData(T input) ...@@ -443,7 +444,8 @@ inline T getShiftedData(T input)
template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
inline T shiftData(T input) inline T shiftData(T input)
{ {
META_ASSERT(inputBitCount + inputBitStart <= (sizeof(T) * 8)); static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
"T must have at least as many bits as inputBitCount + inputBitStart.");
const T mask = (1 << inputBitCount) - 1; const T mask = (1 << inputBitCount) - 1;
return (input & mask) << inputBitStart; return (input & mask) << inputBitStart;
} }
...@@ -547,14 +549,14 @@ inline unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor) ...@@ -547,14 +549,14 @@ inline unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
template <class T> template <class T>
inline bool IsUnsignedAdditionSafe(T lhs, T rhs) inline bool IsUnsignedAdditionSafe(T lhs, T rhs)
{ {
META_ASSERT(!std::numeric_limits<T>::is_signed); static_assert(!std::numeric_limits<T>::is_signed, "T must be unsigned.");
return (rhs <= std::numeric_limits<T>::max() - lhs); return (rhs <= std::numeric_limits<T>::max() - lhs);
} }
template <class T> template <class T>
inline bool IsUnsignedMultiplicationSafe(T lhs, T rhs) inline bool IsUnsignedMultiplicationSafe(T lhs, T rhs)
{ {
META_ASSERT(!std::numeric_limits<T>::is_signed); static_assert(!std::numeric_limits<T>::is_signed, "T must be unsigned.");
return (lhs == T(0) || rhs == T(0) || (rhs <= std::numeric_limits<T>::max() / lhs)); return (lhs == T(0) || rhs == T(0) || (rhs <= std::numeric_limits<T>::max() / lhs));
} }
......
...@@ -343,11 +343,11 @@ int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsig ...@@ -343,11 +343,11 @@ int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsig
return -1; return -1;
} }
META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1); static_assert(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1, "Unexpected GL cube map enum value.");
META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2); static_assert(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2, "Unexpected GL cube map enum value.");
META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3); static_assert(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3, "Unexpected GL cube map enum value.");
META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4); static_assert(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4, "Unexpected GL cube map enum value.");
META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5); static_assert(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5, "Unexpected GL cube map enum value.");
bool IsCubeMapTextureTarget(GLenum target) bool IsCubeMapTextureTarget(GLenum target)
{ {
......
...@@ -22,7 +22,7 @@ void StaticAssertIsFundamental() ...@@ -22,7 +22,7 @@ void StaticAssertIsFundamental()
{ {
// c++11 STL is not available on OSX or Android // c++11 STL is not available on OSX or Android
#if !defined(ANGLE_PLATFORM_APPLE) && !defined(ANGLE_PLATFORM_ANDROID) #if !defined(ANGLE_PLATFORM_APPLE) && !defined(ANGLE_PLATFORM_ANDROID)
META_ASSERT(std::is_fundamental<T>::value); static_assert(std::is_fundamental<T>::value, "T must be a fundamental type.");
#else #else
union { T dummy; } dummy; union { T dummy; } dummy;
static_cast<void>(dummy); static_cast<void>(dummy);
......
...@@ -125,10 +125,10 @@ class ConfigSorter ...@@ -125,10 +125,10 @@ class ConfigSorter
return x.attribute < y.attribute; \ return x.attribute < y.attribute; \
} }
META_ASSERT(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG); static_assert(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG, "Unexpected EGL enum value.");
SORT(configCaveat); SORT(configCaveat);
META_ASSERT(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER); static_assert(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER, "Unexpected EGL enum value.");
SORT(colorBufferType); SORT(colorBufferType);
// By larger total number of color bits, only considering those that are requested to be > 0. // By larger total number of color bits, only considering those that are requested to be > 0.
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "libANGLE/formatutils.h" #include "libANGLE/formatutils.h"
// For use with ArrayString, see angleutils.h // For use with ArrayString, see angleutils.h
META_ASSERT(GL_INVALID_INDEX == UINT_MAX); static_assert(GL_INVALID_INDEX == UINT_MAX, "GL_INVALID_INDEX must be equal to the max unsigned int.");
using namespace gl; using namespace gl;
......
...@@ -928,8 +928,8 @@ gl::Error Renderer11::setDepthStencilState(const gl::DepthStencilState &depthSte ...@@ -928,8 +928,8 @@ gl::Error Renderer11::setDepthStencilState(const gl::DepthStencilState &depthSte
// Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
// GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF); static_assert(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF, "Unexpected value of D3D11_DEFAULT_STENCIL_READ_MASK");
META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF); static_assert(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF, "Unexpected value of D3D11_DEFAULT_STENCIL_WRITE_MASK");
UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu); UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef); mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
...@@ -2170,7 +2170,7 @@ GUID Renderer11::getAdapterIdentifier() const ...@@ -2170,7 +2170,7 @@ GUID Renderer11::getAdapterIdentifier() const
{ {
// Use the adapter LUID as our adapter ID // Use the adapter LUID as our adapter ID
// This number is local to a machine is only guaranteed to be unique between restarts // This number is local to a machine is only guaranteed to be unique between restarts
META_ASSERT(sizeof(LUID) <= sizeof(GUID)); static_assert(sizeof(LUID) <= sizeof(GUID), "Size of GUID must be at least as large as LUID.");
GUID adapterId = {0}; GUID adapterId = {0};
memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID)); memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
return adapterId; return adapterId;
...@@ -3143,9 +3143,10 @@ gl::Error Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsP ...@@ -3143,9 +3143,10 @@ gl::Error Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsP
ColorWriteFunction colorWriteFunction = GetColorWriteFunction(params.format, params.type); ColorWriteFunction colorWriteFunction = GetColorWriteFunction(params.format, params.type);
uint8_t temp[16]; // Maximum size of any Color<T> type used. uint8_t temp[16]; // Maximum size of any Color<T> type used.
META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) && static_assert(sizeof(temp) >= sizeof(gl::ColorF) &&
sizeof(temp) >= sizeof(gl::ColorUI) && sizeof(temp) >= sizeof(gl::ColorUI) &&
sizeof(temp) >= sizeof(gl::ColorI)); sizeof(temp) >= sizeof(gl::ColorI),
"Unexpected size of gl::Color struct.");
for (int y = 0; y < params.area.height; y++) for (int y = 0; y < params.area.height; y++)
{ {
......
...@@ -127,7 +127,8 @@ inline void Copy32FixedTo32FVertexData(const uint8_t *input, size_t stride, size ...@@ -127,7 +127,8 @@ inline void Copy32FixedTo32FVertexData(const uint8_t *input, size_t stride, size
} }
// 4-component output formats would need special padding in the alpha channel. // 4-component output formats would need special padding in the alpha channel.
META_ASSERT(!(inputComponentCount < 4 && outputComponentCount == 4)); static_assert(!(inputComponentCount < 4 && outputComponentCount == 4),
"An inputComponentCount less than 4 and an outputComponentCount equal to 4 is not supported.");
for (size_t j = inputComponentCount; j < outputComponentCount; j++) for (size_t j = inputComponentCount; j < outputComponentCount; j++)
{ {
...@@ -167,7 +168,8 @@ inline void CopyTo32FVertexData(const uint8_t *input, size_t stride, size_t coun ...@@ -167,7 +168,8 @@ inline void CopyTo32FVertexData(const uint8_t *input, size_t stride, size_t coun
} }
// This would require special padding. // This would require special padding.
META_ASSERT(!(inputComponentCount < 4 && outputComponentCount == 4)); static_assert(!(inputComponentCount < 4 && outputComponentCount == 4),
"An inputComponentCount less than 4 and an outputComponentCount equal to 4 is not supported.");
for (size_t j = inputComponentCount; j < outputComponentCount; j++) for (size_t j = inputComponentCount; j < outputComponentCount; j++)
{ {
......
...@@ -581,8 +581,8 @@ static size_t GetMaximumDrawIndexedIndexCount(D3D_FEATURE_LEVEL featureLevel) ...@@ -581,8 +581,8 @@ static size_t GetMaximumDrawIndexedIndexCount(D3D_FEATURE_LEVEL featureLevel)
{ {
// D3D11 allows up to 2^32 elements, but we report max signed int for convenience since that's what's // D3D11 allows up to 2^32 elements, but we report max signed int for convenience since that's what's
// returned from glGetInteger // returned from glGetInteger
META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32); static_assert(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32, "Unexpected D3D11 constant value.");
META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32); static_assert(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32, "Unexpected D3D11 constant value.");
switch (featureLevel) switch (featureLevel)
{ {
...@@ -603,8 +603,8 @@ static size_t GetMaximumDrawVertexCount(D3D_FEATURE_LEVEL featureLevel) ...@@ -603,8 +603,8 @@ static size_t GetMaximumDrawVertexCount(D3D_FEATURE_LEVEL featureLevel)
{ {
// D3D11 allows up to 2^32 elements, but we report max signed int for convenience since that's what's // D3D11 allows up to 2^32 elements, but we report max signed int for convenience since that's what's
// returned from glGetInteger // returned from glGetInteger
META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32); static_assert(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32, "Unexpected D3D11 constant value.");
META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32); static_assert(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32, "Unexpected D3D11 constant value.");
switch (featureLevel) switch (featureLevel)
{ {
...@@ -693,7 +693,7 @@ static size_t GetReservedVertexOutputVectors() ...@@ -693,7 +693,7 @@ static size_t GetReservedVertexOutputVectors()
static size_t GetMaximumVertexOutputVectors(D3D_FEATURE_LEVEL featureLevel) static size_t GetMaximumVertexOutputVectors(D3D_FEATURE_LEVEL featureLevel)
{ {
META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT); static_assert(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT, "Unexpected D3D11 constant value.");
switch (featureLevel) switch (featureLevel)
{ {
......
...@@ -132,8 +132,8 @@ Error ValidateCreateContext(Display *display, Config *configuration, gl::Context ...@@ -132,8 +132,8 @@ Error ValidateCreateContext(Display *display, Config *configuration, gl::Context
break; break;
case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR: case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR:
META_ASSERT(EGL_LOSE_CONTEXT_ON_RESET_EXT == EGL_LOSE_CONTEXT_ON_RESET_KHR); static_assert(EGL_LOSE_CONTEXT_ON_RESET_EXT == EGL_LOSE_CONTEXT_ON_RESET_KHR, "EGL extension enums not equal.");
META_ASSERT(EGL_NO_RESET_NOTIFICATION_EXT == EGL_NO_RESET_NOTIFICATION_KHR); static_assert(EGL_NO_RESET_NOTIFICATION_EXT == EGL_NO_RESET_NOTIFICATION_KHR, "EGL extension enums not equal.");
// same as EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT, fall through // same as EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT, fall through
case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT: case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT:
if (!display->getExtensions().createContextRobustness) if (!display->getExtensions().createContextRobustness)
......
...@@ -92,7 +92,8 @@ bool ValidTexture2DDestinationTarget(const Context *context, GLenum target) ...@@ -92,7 +92,8 @@ bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
bool ValidFramebufferTarget(GLenum target) bool ValidFramebufferTarget(GLenum target)
{ {
META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER); static_assert(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER,
"ANGLE framebuffer enums must equal the ES3 framebuffer enums.");
switch (target) switch (target)
{ {
...@@ -209,8 +210,8 @@ bool ValidCompressedImageSize(const Context *context, GLenum internalFormat, GLs ...@@ -209,8 +210,8 @@ bool ValidCompressedImageSize(const Context *context, GLenum internalFormat, GLs
bool ValidQueryType(const Context *context, GLenum queryType) bool ValidQueryType(const Context *context, GLenum queryType)
{ {
META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT); static_assert(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT, "GL extension enums not equal.");
META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT); static_assert(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, "GL extension enums not equal.");
switch (queryType) switch (queryType)
{ {
...@@ -679,7 +680,8 @@ bool ValidateGetVertexAttribParameters(Context *context, GLenum pname) ...@@ -679,7 +680,8 @@ bool ValidateGetVertexAttribParameters(Context *context, GLenum pname)
case GL_VERTEX_ATTRIB_ARRAY_DIVISOR: case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
// Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
// the same constant. // the same constant.
META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE); static_assert(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
"ANGLE extension enums not equal to GL enums.");
return true; return true;
case GL_VERTEX_ATTRIB_ARRAY_INTEGER: case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment