/src/solidity/libsolutil/Assertions.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * @file Assertions.h |
20 | | * @author Christian <c@ethdev.com> |
21 | | * @date 2015 |
22 | | * |
23 | | * Assertion handling. |
24 | | */ |
25 | | |
26 | | #pragma once |
27 | | |
28 | | #include <libsolutil/Exceptions.h> |
29 | | |
30 | | #include <string> |
31 | | |
32 | | namespace solidity::util |
33 | | { |
34 | | |
35 | | #if defined(_MSC_VER) |
36 | | #define ETH_FUNC __FUNCSIG__ |
37 | | #elif defined(__GNUC__) |
38 | 0 | #define ETH_FUNC __PRETTY_FUNCTION__ |
39 | | #else |
40 | | #define ETH_FUNC __func__ |
41 | | #endif |
42 | | |
43 | | #if defined(__GNUC__) |
44 | | // GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above) |
45 | | [[noreturn]] inline __attribute__((always_inline)) void unreachable() |
46 | 0 | { |
47 | 0 | __builtin_unreachable(); |
48 | 0 | } |
49 | | |
50 | | #elif defined(_MSC_VER) // MSVC |
51 | | |
52 | | [[noreturn]] __forceinline void unreachable() |
53 | | { |
54 | | __assume(false); |
55 | | } |
56 | | |
57 | | #else |
58 | | |
59 | | [[noreturn]] inline void unreachable() |
60 | | { |
61 | | solThrow(Exception, "Unreachable"); |
62 | | } |
63 | | #endif |
64 | | |
65 | | namespace assertions |
66 | | { |
67 | | |
68 | | inline std::string stringOrDefault(std::string _string, std::string _defaultString) |
69 | 0 | { |
70 | | // NOTE: Putting this in a function rather than directly in a macro prevents the string from |
71 | | // being evaluated multiple times if it's not just a literal. |
72 | 0 | return (!_string.empty() ? _string : _defaultString); |
73 | 0 | } |
74 | | |
75 | | } |
76 | | |
77 | | /// Base macro that can be used to implement assertion macros. |
78 | | /// Throws an exception containing the given description if the condition is not met. |
79 | | /// Allows you to provide the default description for the case where the user of your macro does |
80 | | /// not provide any. |
81 | | /// The second parameter must be an exception class (rather than an instance). |
82 | | #define assertThrowWithDefaultDescription(_condition, _exceptionType, _description, _defaultDescription) \ |
83 | 800M | do \ |
84 | 800M | { \ |
85 | 799M | if (!(_condition)) \ Unexecuted instantiation: ControlFlowGraphBuilder.cpp:solidity::yul::ControlFlowGraphBuilder::lookupFunction(solidity::yul::YulString) const::$_12::operator()(solidity::yul::Scope::Variable&) const ControlFlowGraphBuilder.cpp:solidity::yul::ControlFlowGraphBuilder::lookupFunction(solidity::yul::YulString) const::$_13::operator()(solidity::yul::Scope::Function&) const Line | Count | Source | 85 | 82.8k | if (!(_condition)) \ |
Unexecuted instantiation: EVMCodeTransform.cpp:solidity::yul::CodeTransform::operator()(solidity::yul::FunctionCall const&)::$_0::operator()(solidity::yul::Scope::Variable&) const Unexecuted instantiation: EVMCodeTransform.cpp:solidity::yul::CodeTransform::operator()(solidity::yul::FunctionCall const&)::$_1::operator()(solidity::yul::Scope::Function&) const Unexecuted instantiation: WasmCodeTransform.cpp:solidity::yul::WasmCodeTransform::allocateGlobals(std::__1::vector<solidity::yul::wasm::Type, std::__1::allocator<solidity::yul::wasm::Type> > const&)::$_0::operator()(unsigned long) const |
86 | 799M | solThrow( \ |
87 | 799M | _exceptionType, \ |
88 | 799M | ::solidity::util::assertions::stringOrDefault((_description), (_defaultDescription)) \ |
89 | 799M | ); \ |
90 | 799M | } \ |
91 | 800M | while (false) |
92 | | |
93 | | /// Assertion that throws an exception containing the given description if it is not met. |
94 | | /// Use it as assertThrow(1 == 1, ExceptionType, "Mathematics is wrong."); |
95 | | /// The second parameter must be an exception class (rather than an instance). |
96 | | #define assertThrow(_condition, _exceptionType, _description) \ |
97 | 443M | assertThrowWithDefaultDescription((_condition), _exceptionType, (_description), "Assertion failed") |
98 | | |
99 | | } |