permassert.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #ifndef PERMASSERT_H
  6. #define PERMASSERT_H
  7. #include <linux/compiler.h>
  8. #include "errors.h"
  9. /* Utilities for asserting that certain conditions are met */
  10. #define STRINGIFY(X) #X
  11. /*
  12. * A hack to apply the "warn if unused" attribute to an integral expression.
  13. *
  14. * Since GCC doesn't propagate the warn_unused_result attribute to conditional expressions
  15. * incorporating calls to functions with that attribute, this function can be used to wrap such an
  16. * expression. With optimization enabled, this function contributes no additional instructions, but
  17. * the warn_unused_result attribute still applies to the code calling it.
  18. */
  19. static inline int __must_check vdo_must_use(int value)
  20. {
  21. return value;
  22. }
  23. /* Assert that an expression is true and return an error if it is not. */
  24. #define VDO_ASSERT(expr, ...) vdo_must_use(__VDO_ASSERT(expr, __VA_ARGS__))
  25. /* Log a message if the expression is not true. */
  26. #define VDO_ASSERT_LOG_ONLY(expr, ...) __VDO_ASSERT(expr, __VA_ARGS__)
  27. #define __VDO_ASSERT(expr, ...) \
  28. (likely(expr) ? VDO_SUCCESS \
  29. : vdo_assertion_failed(STRINGIFY(expr), __FILE__, __LINE__, __VA_ARGS__))
  30. /* Log an assertion failure message. */
  31. int vdo_assertion_failed(const char *expression_string, const char *file_name,
  32. int line_number, const char *format, ...)
  33. __printf(4, 5);
  34. #endif /* PERMASSERT_H */