/src/solidity/liblangutil/DebugInfoSelection.cpp
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 | | #include <liblangutil/DebugInfoSelection.h> |
20 | | |
21 | | #include <liblangutil/Exceptions.h> |
22 | | |
23 | | #include <libsolutil/StringUtils.h> |
24 | | |
25 | | #include <boost/algorithm/string/trim.hpp> |
26 | | |
27 | | #include <range/v3/range/conversion.hpp> |
28 | | #include <range/v3/view/map.hpp> |
29 | | #include <range/v3/view/split.hpp> |
30 | | |
31 | | #include <vector> |
32 | | |
33 | | using namespace std; |
34 | | using namespace solidity; |
35 | | using namespace solidity::langutil; |
36 | | using namespace solidity::util; |
37 | | |
38 | | DebugInfoSelection const DebugInfoSelection::All(bool _value) noexcept |
39 | 5.52k | { |
40 | 5.52k | DebugInfoSelection result; |
41 | 5.52k | for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values) |
42 | 16.5k | result.*member = _value; |
43 | 5.52k | return result; |
44 | 5.52k | } |
45 | | |
46 | | DebugInfoSelection const DebugInfoSelection::Only(bool DebugInfoSelection::* _member) noexcept |
47 | 0 | { |
48 | 0 | DebugInfoSelection result{}; |
49 | 0 | result.*_member = true; |
50 | 0 | return result; |
51 | 0 | } |
52 | | |
53 | | optional<DebugInfoSelection> DebugInfoSelection::fromString(string_view _input) |
54 | 0 | { |
55 | | // TODO: Make more stuff constexpr and make it a static_assert(). |
56 | 0 | solAssert(componentMap().count("all") == 0, ""); |
57 | 0 | solAssert(componentMap().count("none") == 0, ""); |
58 | | |
59 | 0 | if (_input == "all") |
60 | 0 | return All(); |
61 | 0 | if (_input == "none") |
62 | 0 | return None(); |
63 | | |
64 | 0 | return fromComponents(_input | ranges::views::split(',') | ranges::to<vector<string>>); |
65 | 0 | } |
66 | | |
67 | | optional<DebugInfoSelection> DebugInfoSelection::fromComponents( |
68 | | vector<string> const& _componentNames, |
69 | | bool _acceptWildcards |
70 | | ) |
71 | 0 | { |
72 | 0 | solAssert(componentMap().count("*") == 0, ""); |
73 | | |
74 | 0 | DebugInfoSelection selection; |
75 | 0 | for (auto const& component: _componentNames) |
76 | 0 | { |
77 | 0 | if (component == "*") |
78 | 0 | return (_acceptWildcards ? make_optional(DebugInfoSelection::All()) : nullopt); |
79 | | |
80 | 0 | if (!selection.enable(component)) |
81 | 0 | return nullopt; |
82 | 0 | } |
83 | | |
84 | 0 | return selection; |
85 | 0 | } |
86 | | |
87 | | bool DebugInfoSelection::enable(string _component) |
88 | 0 | { |
89 | 0 | auto memberIt = componentMap().find(boost::trim_copy(_component)); |
90 | 0 | if (memberIt == componentMap().end()) |
91 | 0 | return false; |
92 | | |
93 | 0 | this->*(memberIt->second) = true; |
94 | 0 | return true; |
95 | 0 | } |
96 | | |
97 | | bool DebugInfoSelection::any() const noexcept |
98 | 0 | { |
99 | 0 | for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values) |
100 | 0 | if (this->*member) |
101 | 0 | return true; |
102 | | |
103 | 0 | return false; |
104 | 0 | } |
105 | | |
106 | | bool DebugInfoSelection::all() const noexcept |
107 | 0 | { |
108 | 0 | for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values) |
109 | 0 | if (!(this->*member)) |
110 | 0 | return false; |
111 | | |
112 | 0 | return true; |
113 | 0 | } |
114 | | |
115 | | DebugInfoSelection& DebugInfoSelection::operator&=(DebugInfoSelection const& _other) |
116 | 0 | { |
117 | 0 | for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values) |
118 | 0 | this->*member &= _other.*member; |
119 | 0 | return *this; |
120 | 0 | } |
121 | | |
122 | | DebugInfoSelection& DebugInfoSelection::operator|=(DebugInfoSelection const& _other) |
123 | 0 | { |
124 | 0 | for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values) |
125 | 0 | this->*member |= _other.*member; |
126 | 0 | return *this; |
127 | 0 | } |
128 | | |
129 | | DebugInfoSelection DebugInfoSelection::operator&(DebugInfoSelection _other) const noexcept |
130 | 0 | { |
131 | 0 | _other &= *this; |
132 | 0 | return _other; |
133 | 0 | } |
134 | | |
135 | | DebugInfoSelection DebugInfoSelection::operator|(DebugInfoSelection _other) const noexcept |
136 | 0 | { |
137 | 0 | _other |= *this; |
138 | 0 | return _other; |
139 | 0 | } |
140 | | |
141 | | bool DebugInfoSelection::operator==(DebugInfoSelection const& _other) const noexcept |
142 | 0 | { |
143 | 0 | for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values) |
144 | 0 | if (this->*member != _other.*member) |
145 | 0 | return false; |
146 | 0 | return true; |
147 | 0 | } |
148 | | |
149 | | ostream& langutil::operator<<(ostream& _stream, DebugInfoSelection const& _selection) |
150 | 0 | { |
151 | 0 | vector<string> selectedComponentNames; |
152 | 0 | for (auto const& [name, member]: _selection.componentMap()) |
153 | 0 | if (_selection.*member) |
154 | 0 | selectedComponentNames.push_back(name); |
155 | |
|
156 | 0 | return _stream << joinHumanReadable(selectedComponentNames, ","); |
157 | 0 | } |