File indexing completed on 2024-06-23 05:14:17

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/kleo_assert.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <Libkleo/KleoException>
0013 
0014 #include <assert.h>
0015 
0016 #define S(x) #x
0017 #define S_(x) S(x)
0018 #define S_LINE S_(__LINE__)
0019 
0020 #define kleo_assert_fail_impl(cond, file, line) throw Kleo::Exception(gpg_error(GPG_ERR_INTERNAL), "assertion \"" #cond "\" failed at " file ":" line)
0021 #define kleo_assert_fail_impl_func(cond, file, line, func)                                                                                                     \
0022     throw Kleo::Exception(gpg_error(GPG_ERR_INTERNAL), std::string("assertion \"" #cond "\" failed in ") + func + " (" file ":" line ")")
0023 
0024 #define kleo_assert_impl(cond, file, line)                                                                                                                     \
0025     if (cond) {                                                                                                                                                \
0026     } else                                                                                                                                                     \
0027         kleo_assert_fail_impl(cond, file, line)
0028 #define kleo_assert_impl_func(cond, file, line, func)                                                                                                          \
0029     if (cond) {                                                                                                                                                \
0030     } else                                                                                                                                                     \
0031         kleo_assert_fail_impl_func(cond, file, line, func)
0032 
0033 // from glibc's assert.h:
0034 /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
0035    which contains the name of the function currently being defined.
0036    This is broken in G++ before version 2.6.
0037    C9x has a similar variable called __func__, but prefer the GCC one since
0038    it demangles C++ function names.  */
0039 
0040 #if defined(__GNUC_PREREQ)
0041 #define KLEO_GNUC_PREREQ __GNUC_PREREQ
0042 #elif defined(__MINGW_GNUC_PREREQ)
0043 #define KLEO_GNUC_PREREQ __MINGW_GNUC_PREREQ
0044 #else
0045 #define KLEO_GNUC_PREREQ(maj, min) 0
0046 #endif
0047 
0048 #if KLEO_GNUC_PREREQ(2, 6)
0049 #define kleo_assert(cond) kleo_assert_impl_func(cond, __FILE__, S_LINE, __PRETTY_FUNCTION__)
0050 #define kleo_assert_fail(cond) kleo_assert_fail_impl_func(cond, __FILE__, S_LINE, __PRETTY_FUNCTION__)
0051 #define notImplemented() throw Exception(gpg_error(GPG_ERR_NOT_IMPLEMENTED), __PRETTY_FUNCTION__)
0052 #elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
0053 #define kleo_assert(cond) kleo_assert_impl_func(cond, __FILE__, S_LINE, __func__)
0054 #define kleo_assert_fail(cond) kleo_assert_fail_impl_func(cond, __FILE__, S_LINE, __func__)
0055 #define notImplemented() throw Exception(gpg_error(GPG_ERR_NOT_IMPLEMENTED), __func__)
0056 #endif
0057 
0058 #undef KLEO_GNUC_PREREQ
0059 
0060 #ifndef kleo_assert
0061 #define kleo_assert(cond) kleo_assert_impl(cond, __FILE__, S_LINE)
0062 #endif
0063 
0064 #ifndef kleo_assert_fail
0065 #define kleo_assert_fail(cond) kleo_assert_fail_impl(cond, __FILE__, S_LINE)
0066 #endif
0067 
0068 #ifndef notImplemented
0069 #define notImplemented() kleo_assert(!"Sorry, not yet implemented")
0070 #endif