Define DebugInfoSelection

This commit is contained in:
Kamil Śliwak 2021-09-13 12:10:51 +02:00
parent 2cd1c644b6
commit 25eedfafe2
3 changed files with 245 additions and 0 deletions

View File

@ -3,6 +3,8 @@ set(sources
Common.h Common.h
CharStream.cpp CharStream.cpp
CharStream.h CharStream.h
DebugInfoSelection.cpp
DebugInfoSelection.h
ErrorReporter.cpp ErrorReporter.cpp
ErrorReporter.h ErrorReporter.h
EVMVersion.h EVMVersion.h

View File

@ -0,0 +1,157 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/Exceptions.h>
#include <libsolutil/StringUtils.h>
#include <boost/algorithm/string/trim.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/map.hpp>
#include <range/v3/view/split.hpp>
#include <vector>
using namespace std;
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::util;
DebugInfoSelection const DebugInfoSelection::All(bool _value) noexcept
{
DebugInfoSelection result;
for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
result.*member = _value;
return result;
}
DebugInfoSelection const DebugInfoSelection::Only(bool DebugInfoSelection::* _member) noexcept
{
DebugInfoSelection result{};
result.*_member = true;
return result;
}
optional<DebugInfoSelection> DebugInfoSelection::fromString(string_view _input)
{
// TODO: Make more stuff constexpr and make it a static_assert().
solAssert(componentMap().count("all") == 0, "");
solAssert(componentMap().count("none") == 0, "");
if (_input == "all")
return All();
if (_input == "none")
return None();
return fromComponents(_input | ranges::views::split(',') | ranges::to<vector<string>>);
}
optional<DebugInfoSelection> DebugInfoSelection::fromComponents(
vector<string> const& _componentNames,
bool _acceptWildcards
)
{
solAssert(componentMap().count("*") == 0, "");
DebugInfoSelection selection;
for (auto const& component: _componentNames)
{
if (component == "*")
return (_acceptWildcards ? make_optional(DebugInfoSelection::All()) : nullopt);
if (!selection.enable(component))
return nullopt;
}
return selection;
}
bool DebugInfoSelection::enable(string _component)
{
auto memberIt = componentMap().find(boost::trim_copy(_component));
if (memberIt == componentMap().end())
return false;
this->*(memberIt->second) = true;
return true;
}
bool DebugInfoSelection::any() const noexcept
{
for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
if (this->*member)
return true;
return false;
}
bool DebugInfoSelection::all() const noexcept
{
for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
if (!(this->*member))
return false;
return true;
}
DebugInfoSelection& DebugInfoSelection::operator&=(DebugInfoSelection const& _other)
{
for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
this->*member &= _other.*member;
return *this;
}
DebugInfoSelection& DebugInfoSelection::operator|=(DebugInfoSelection const& _other)
{
for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
this->*member |= _other.*member;
return *this;
}
DebugInfoSelection DebugInfoSelection::operator&(DebugInfoSelection _other) const noexcept
{
_other &= *this;
return _other;
}
DebugInfoSelection DebugInfoSelection::operator|(DebugInfoSelection _other) const noexcept
{
_other |= *this;
return _other;
}
bool DebugInfoSelection::operator==(DebugInfoSelection const& _other) const noexcept
{
for (bool DebugInfoSelection::* member: componentMap() | ranges::views::values)
if (this->*member != _other.*member)
return false;
return true;
}
ostream& langutil::operator<<(ostream& _stream, DebugInfoSelection const& _selection)
{
vector<string> selectedComponentNames;
for (auto const& [name, member]: _selection.componentMap())
if (_selection.*member)
selectedComponentNames.push_back(name);
return _stream << joinHumanReadable(selectedComponentNames, ",");
}

View File

@ -0,0 +1,86 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Handles selections of debug info components.
*/
#pragma once
#include <map>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>
namespace solidity::langutil
{
/**
* Represents a set of flags corresponding to components of debug info selected for some purpose.
*
* Provides extra functionality for enumerating the components and serializing/deserializing the
* selection to/from a comma-separated string.
*/
struct DebugInfoSelection
{
static DebugInfoSelection const All(bool _value = true) noexcept;
static DebugInfoSelection const None() noexcept { return All(false); }
static DebugInfoSelection const Only(bool DebugInfoSelection::* _member) noexcept;
static DebugInfoSelection const Default() noexcept { return All(); }
static std::optional<DebugInfoSelection> fromString(std::string_view _input);
static std::optional<DebugInfoSelection> fromComponents(
std::vector<std::string> const& _componentNames,
bool _acceptWildcards = false
);
bool enable(std::string _component);
bool all() const noexcept;
bool any() const noexcept;
bool none() const noexcept { return !any(); }
bool only(bool DebugInfoSelection::* _member) const noexcept { return *this == Only(_member); }
DebugInfoSelection& operator&=(DebugInfoSelection const& _other);
DebugInfoSelection& operator|=(DebugInfoSelection const& _other);
DebugInfoSelection operator&(DebugInfoSelection _other) const noexcept;
DebugInfoSelection operator|(DebugInfoSelection _other) const noexcept;
bool operator!=(DebugInfoSelection const& _other) const noexcept { return !(*this == _other); }
bool operator==(DebugInfoSelection const& _other) const noexcept;
friend std::ostream& operator<<(std::ostream& _stream, DebugInfoSelection const& _selection);
static auto const& componentMap()
{
static std::map<std::string, bool DebugInfoSelection::*> const components = {
{"location", &DebugInfoSelection::location},
{"snippet", &DebugInfoSelection::snippet},
{"ast-id", &DebugInfoSelection::astID},
};
return components;
}
bool location = false; ///< Include source location. E.g. `@src 3:50:100`
bool snippet = false; ///< Include source code snippet next to location. E.g. `@src 3:50:100 "contract C {..."`
bool astID = false; ///< Include ID of the Solidity AST node. E.g. `@ast-id 15`
};
std::ostream& operator<<(std::ostream& _stream, DebugInfoSelection const& _selection);
}