Assertions.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #undef AssertMsg
  7. #undef Assert
  8. #if defined(DBG) && !defined(DIAG_DAC)
  9. // AutoDebug functions that are only available in DEBUG builds
  10. extern int AssertCount;
  11. extern int AssertsToConsole;
  12. #if _WIN32
  13. _declspec(thread, selectany) int IsInAssert = false;
  14. #elif !defined(__IOS__)
  15. extern __declspec(thread) int IsInAssert;
  16. #else
  17. // todo: implement thread local variable for iOS ??
  18. extern int IsInAssert;
  19. #endif
  20. #if !defined(USED_IN_STATIC_LIB)
  21. #define REPORT_ASSERT(f, comment) Js::Throw::ReportAssert(__FILE__, __LINE__, STRINGIZE((f)), comment)
  22. #define LOG_ASSERT() Js::Throw::LogAssert()
  23. #else
  24. #define REPORT_ASSERT(f, comment) FALSE
  25. #define LOG_ASSERT()
  26. #endif
  27. #ifdef NTBUILD
  28. #include <ntassert.h>
  29. #define RAISE_ASSERTION(comment) NT_ASSERTMSG(comment, FALSE)
  30. #else
  31. #include <assert.h>
  32. #define RAISE_ASSERTION(comment) DbgRaiseAssertionFailure()
  33. #endif
  34. #define AssertMsg(f, comment) \
  35. do { \
  36. if (!(f)) \
  37. { \
  38. AssertCount++; \
  39. LOG_ASSERT(); \
  40. IsInAssert = TRUE; \
  41. if (!REPORT_ASSERT(f, comment)) \
  42. { \
  43. RAISE_ASSERTION(comment); \
  44. } \
  45. IsInAssert = FALSE; \
  46. __analysis_assume(false); \
  47. } \
  48. } while (false)
  49. #define Assert(exp) AssertMsg(exp, #exp)
  50. #define AssertVerify(exp) Assert(exp)
  51. #define Assume(x) Assert(x)
  52. #define DebugOnly(x) x
  53. #else // DBG
  54. #define AssertMsg(f, comment) ((void) 0)
  55. #define Assert(exp) ((void) 0)
  56. #ifdef NTBUILD
  57. #include <ntassert.h>
  58. #define AssertVerify(exp) NT_VERIFY(exp) // Execute the expression but don't do anything with the result in non-debug builds
  59. #else
  60. #define AssertVerify(exp) (exp)
  61. #endif
  62. #define Assume(x) __assume(x)
  63. #define DebugOnly(x)
  64. #endif // DBG
  65. #define AnalysisAssert(x) Assert(x); __analysis_assume(x)
  66. #define AnalysisAssertMsg(x, comment) AssertMsg(x, comment); __analysis_assume(x)
  67. #ifdef DBG
  68. #define AssertOrFailFast(x) Assert(x)
  69. #define AssertOrFailFastHR(x, hr) Assert(x)
  70. #define AssertOrFailFastMsg(x, msg) AssertMsg(x, msg)
  71. #define AssertOrFailFastMsgHR(x, msg) AssertOrFailFastHR(x, hr)
  72. #define AnalysisAssertOrFailFast(x) AnalysisAssert(x)
  73. #define AnalysisAssertOrFailFastMsg(x, msg) AnalysisAssertMsg(x, msg)
  74. #else
  75. #define AssertOrFailFastHR(x, hr) do { if (!(x)) { Js::Throw::FatalInternalError(hr); } } while (false)
  76. #define AssertOrFailFast(x) do { if (!(x)) { Js::Throw::FatalInternalError(); } } while (false)
  77. #define AssertOrFailFastMsg(x, msg) AssertOrFailFast(x)
  78. #define AssertOrFailFastMsgHR(x, msg) AssertOrFailFastHR(x, hr)
  79. #define AnalysisAssertOrFailFast(x) AssertOrFailFast(x)
  80. #define AnalysisAssertOrFailFastMsg(x, msg) AssertOrFailFast(x)
  81. #endif
  82. #define Unused(var) var;
  83. #define UNREACHED (0)
  84. #ifndef CompileAssert
  85. #define CompileAssert(e) static_assert(e, #e)
  86. #endif
  87. #ifndef CompileAssertMsg
  88. #define CompileAssertMsg(e, msg) static_assert(e, msg)
  89. #endif
  90. // We set IsPointer<T>::IsTrue to true if T is a pointer type
  91. // Otherwise, it's set to false
  92. template <class T>
  93. struct IsPointer
  94. {
  95. enum
  96. {
  97. IsTrue = false
  98. };
  99. };
  100. template <class T>
  101. struct IsPointer<T*>
  102. {
  103. enum
  104. {
  105. IsTrue = true
  106. };
  107. };
  108. // Trick adopted from WinRT/WinTypes/Value.h
  109. template <class T1, class T2>
  110. struct IsSame
  111. {
  112. enum
  113. {
  114. IsTrue = false
  115. };
  116. };
  117. template <class T1>
  118. struct IsSame<T1, T1>
  119. {
  120. enum
  121. {
  122. IsTrue = true
  123. };
  124. };