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 
All(bool _value)38 DebugInfoSelection const DebugInfoSelection::All(bool _value) noexcept
39 {
40 	DebugInfoSelection result;
41 	for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
42 		result.*member = _value;
43 	return result;
44 }
45 
Only(bool DebugInfoSelection::* _member)46 DebugInfoSelection const DebugInfoSelection::Only(bool DebugInfoSelection::* _member) noexcept
47 {
48 	DebugInfoSelection result{};
49 	result.*_member = true;
50 	return result;
51 }
52 
fromString(string_view _input)53 optional<DebugInfoSelection> DebugInfoSelection::fromString(string_view _input)
54 {
55 	// TODO: Make more stuff constexpr and make it a static_assert().
56 	solAssert(componentMap().count("all") == 0, "");
57 	solAssert(componentMap().count("none") == 0, "");
58 
59 	if (_input == "all")
60 		return All();
61 	if (_input == "none")
62 		return None();
63 
64 	return fromComponents(_input | ranges::views::split(',') | ranges::to<vector<string>>);
65 }
66 
fromComponents(vector<string> const & _componentNames,bool _acceptWildcards)67 optional<DebugInfoSelection> DebugInfoSelection::fromComponents(
68 	vector<string> const& _componentNames,
69 	bool _acceptWildcards
70 )
71 {
72 	solAssert(componentMap().count("*") == 0, "");
73 
74 	DebugInfoSelection selection;
75 	for (auto const& component: _componentNames)
76 	{
77 		if (component == "*")
78 			return (_acceptWildcards ? make_optional(DebugInfoSelection::All()) : nullopt);
79 
80 		if (!selection.enable(component))
81 			return nullopt;
82 	}
83 
84 	return selection;
85 }
86 
enable(string _component)87 bool DebugInfoSelection::enable(string _component)
88 {
89 	auto memberIt = componentMap().find(boost::trim_copy(_component));
90 	if (memberIt == componentMap().end())
91 		return false;
92 
93 	this->*(memberIt->second) = true;
94 	return true;
95 }
96 
any() const97 bool DebugInfoSelection::any() const noexcept
98 {
99 	for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
100 		if (this->*member)
101 			return true;
102 
103 	return false;
104 }
105 
all() const106 bool DebugInfoSelection::all() const noexcept
107 {
108 	for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
109 		if (!(this->*member))
110 			return false;
111 
112 	return true;
113 }
114 
operator &=(DebugInfoSelection const & _other)115 DebugInfoSelection& DebugInfoSelection::operator&=(DebugInfoSelection const& _other)
116 {
117 	for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
118 		this->*member &= _other.*member;
119 	return *this;
120 }
121 
operator |=(DebugInfoSelection const & _other)122 DebugInfoSelection& DebugInfoSelection::operator|=(DebugInfoSelection const& _other)
123 {
124 	for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
125 		this->*member |= _other.*member;
126 	return *this;
127 }
128 
operator &(DebugInfoSelection _other) const129 DebugInfoSelection DebugInfoSelection::operator&(DebugInfoSelection _other) const noexcept
130 {
131 	_other &= *this;
132 	return _other;
133 }
134 
operator |(DebugInfoSelection _other) const135 DebugInfoSelection DebugInfoSelection::operator|(DebugInfoSelection _other) const noexcept
136 {
137 	_other |= *this;
138 	return _other;
139 }
140 
operator ==(DebugInfoSelection const & _other) const141 bool DebugInfoSelection::operator==(DebugInfoSelection const& _other) const noexcept
142 {
143 	for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
144 		if (this->*member != _other.*member)
145 			return false;
146 	return true;
147 }
148 
operator <<(ostream & _stream,DebugInfoSelection const & _selection)149 ostream& langutil::operator<<(ostream& _stream, DebugInfoSelection const& _selection)
150 {
151 	vector<string> selectedComponentNames;
152 	for (auto const& [name, member]: _selection.componentMap())
153 		if (_selection.*member)
154 			selectedComponentNames.push_back(name);
155 
156 	return _stream << joinHumanReadable(selectedComponentNames, ",");
157 }
158