check_internal.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_COMMON_CHECK_INTERNAL_H_
  5. #define CARBON_COMMON_CHECK_INTERNAL_H_
  6. #include "common/template_string.h"
  7. #include "llvm/Support/FormatVariadic.h"
  8. namespace Carbon::Internal {
  9. // Evaluates a condition in a CHECK. This diagnoses if the condition evaluates
  10. // to the constant `true` or `false`.
  11. [[clang::always_inline]] constexpr bool
  12. // Trailing GNU function attributes are incompatible with trailing return types.
  13. // Filed as https://github.com/llvm/llvm-project/issues/118697
  14. // NOLINTNEXTLINE(modernize-use-trailing-return-type)
  15. CheckCondition(bool condition)
  16. __attribute__((diagnose_if(condition,
  17. "CHECK condition is always true; replace with "
  18. "static_assert if this is intended",
  19. "error")))
  20. __attribute__((diagnose_if(!condition,
  21. "CHECK condition is always false; replace with "
  22. "CARBON_FATAL if this is intended",
  23. "error"))) {
  24. return condition;
  25. }
  26. // Implements the check failure message printing.
  27. //
  28. // This is out-of-line and will arrange to stop the program, print any debugging
  29. // information and this string.
  30. //
  31. // This API uses `const char*` C string arguments rather than `llvm::StringRef`
  32. // because we know that these are available as C strings and passing them that
  33. // way lets the code size of calling it be smaller: it only needs to materialize
  34. // a single pointer argument for each. The runtime cost of re-computing the size
  35. // should be minimal. The extra message however might not be compile-time
  36. // guaranteed to be a C string so we use a normal `StringRef` there.
  37. [[noreturn]] auto CheckFailImpl(const char* kind, const char* file, int line,
  38. const char* condition_str,
  39. llvm::StringRef extra_message) -> void;
  40. // Prints a check failure, including rendering any user-provided message using
  41. // a format string.
  42. //
  43. // Most of the parameters are passed as compile-time template strings to avoid
  44. // runtime cost of parameter setup in optimized builds. Each of these are passed
  45. // along to the underlying implementation to include in the final printed
  46. // message.
  47. //
  48. // Any user-provided format string and values are directly passed to
  49. // `llvm::formatv` which handles all of the formatting of output.
  50. template <TemplateString Kind, TemplateString File, int Line,
  51. TemplateString ConditionStr, TemplateString FormatStr, typename... Ts>
  52. [[noreturn, gnu::cold, clang::noinline]] auto CheckFail(Ts&&... values)
  53. -> void {
  54. if constexpr (llvm::StringRef(FormatStr).empty()) {
  55. // Skip the format string rendering if empty. Note that we don't skip it
  56. // even if there are no values as we want to have consistent handling of
  57. // `{}`s in the format string. This case is about when there is no message
  58. // at all, just the condition.
  59. CheckFailImpl(Kind.c_str(), File.c_str(), Line, ConditionStr.c_str(), "");
  60. } else {
  61. CheckFailImpl(
  62. Kind.c_str(), File.c_str(), Line, ConditionStr.c_str(),
  63. llvm::formatv(FormatStr.c_str(), std::forward<Ts>(values)...).str());
  64. }
  65. }
  66. } // namespace Carbon::Internal
  67. // Evaluates the condition of a CHECK as a boolean value.
  68. //
  69. // This performs a contextual conversion to bool, diagnoses if the condition is
  70. // always true or always false, and returns its value.
  71. #define CARBON_INTERNAL_CHECK_CONDITION(cond) \
  72. (Carbon::Internal::CheckCondition(true && (cond)))
  73. // Implements check messages without any formatted values.
  74. //
  75. // Passes each of the provided components of the message to the template
  76. // parameters of the check failure printing function above, including an empty
  77. // string for the format string. Because there are multiple template arguments,
  78. // the entire call is wrapped in parentheses.
  79. #define CARBON_INTERNAL_CHECK_IMPL(kind, file, line, condition_str) \
  80. (Carbon::Internal::CheckFail<kind, file, line, condition_str, "">())
  81. // Implements check messages with a format string and potentially formatted
  82. // values.
  83. //
  84. // Each of the main components is passed as a template arguments, and then any
  85. // formatted values are passed as arguments. Because there are multiple template
  86. // arguments, the entire call is wrapped in parentheses.
  87. #define CARBON_INTERNAL_CHECK_IMPL_FORMAT(kind, file, line, condition_str, \
  88. format_str, ...) \
  89. (Carbon::Internal::CheckFail<kind, file, line, condition_str, format_str>( \
  90. __VA_ARGS__))
  91. // Implements the failure of a check.
  92. //
  93. // Collects all the metadata about the failure to be printed, such as source
  94. // location and stringified condition, and passes those, any format string and
  95. // formatted arguments to the correct implementation macro above.
  96. #define CARBON_INTERNAL_CHECK(condition, ...) \
  97. CARBON_INTERNAL_CHECK_IMPL##__VA_OPT__(_FORMAT)( \
  98. "CHECK", __FILE__, __LINE__, #condition __VA_OPT__(, ) __VA_ARGS__)
  99. // Implements the fatal macro.
  100. //
  101. // Similar to the check failure macro, but tags the message as a fatal one and
  102. // leaves the stringified condition empty.
  103. #define CARBON_INTERNAL_FATAL(...) \
  104. CARBON_INTERNAL_CHECK_IMPL##__VA_OPT__(_FORMAT)( \
  105. "FATAL", __FILE__, __LINE__, "" __VA_OPT__(, ) __VA_ARGS__)
  106. #ifdef NDEBUG
  107. // For `DCHECK` in optimized builds we have a dead check that we want to
  108. // potentially "use" arguments, but otherwise have the minimal overhead. We
  109. // avoid forming interesting format strings here so that we don't have to
  110. // repeatedly instantiate the `Check` function above. This format string would
  111. // be an error if actually used.
  112. #define CARBON_INTERNAL_DEAD_DCHECK(condition, ...) \
  113. CARBON_INTERNAL_DEAD_DCHECK_IMPL##__VA_OPT__(_FORMAT)(__VA_ARGS__)
  114. #define CARBON_INTERNAL_DEAD_DCHECK_IMPL() \
  115. Carbon::Internal::CheckFail<"", "", 0, "", "">()
  116. #define CARBON_INTERNAL_DEAD_DCHECK_IMPL_FORMAT(format_str, ...) \
  117. Carbon::Internal::CheckFail<"", "", 0, "", "">(__VA_ARGS__)
  118. #endif
  119. #endif // CARBON_COMMON_CHECK_INTERNAL_H_