|
| 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 <test/libsolidity/EthdebugTest.h> |
| 20 | +#include <test/Common.h> |
| 21 | + |
| 22 | +#include <liblangutil/DebugInfoSelection.h> |
| 23 | + |
| 24 | +#include <libsolidity/interface/OptimiserSettings.h> |
| 25 | + |
| 26 | +#include <boost/throw_exception.hpp> |
| 27 | + |
| 28 | +#include <stdexcept> |
| 29 | + |
| 30 | +using namespace solidity; |
| 31 | +using namespace solidity::frontend; |
| 32 | +using namespace solidity::frontend::test; |
| 33 | +using namespace solidity::langutil; |
| 34 | + |
| 35 | +EthdebugTest::EthdebugTest(std::string const& _filename): |
| 36 | + StandardJSONTest(_filename) |
| 37 | +{ |
| 38 | + m_optimise = m_reader.boolSetting("optimize", false); |
| 39 | + m_optimiseYul = m_reader.boolSetting("optimize-yul", false); |
| 40 | + m_useSSACFG = m_reader.boolSetting("compileViaSSACFG", false); |
| 41 | + |
| 42 | + auto revertStrings = revertStringsFromString(m_reader.stringSetting("revertStrings", "default")); |
| 43 | + if (!revertStrings) |
| 44 | + BOOST_THROW_EXCEPTION(std::runtime_error("Invalid revertStrings setting.")); |
| 45 | + m_revertStrings = *revertStrings; |
| 46 | +} |
| 47 | + |
| 48 | +void EthdebugTest::setupCompiler(CompilerStack& _compiler) |
| 49 | +{ |
| 50 | + AnalysisFramework::setupCompiler(_compiler); |
| 51 | + |
| 52 | + _compiler.setViaIR(true); |
| 53 | + _compiler.setExperimental(true); |
| 54 | + if (m_useSSACFG) |
| 55 | + _compiler.setViaSSACFG(true); |
| 56 | + |
| 57 | + DebugInfoSelection selection = DebugInfoSelection::Default(); |
| 58 | + selection.enable("ethdebug"); |
| 59 | + _compiler.selectDebugInfo(selection); |
| 60 | + |
| 61 | + _compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata); |
| 62 | + _compiler.setRevertStringBehaviour(m_revertStrings); |
| 63 | + |
| 64 | + OptimiserSettings settings = m_optimise ? OptimiserSettings::standard() : OptimiserSettings::minimal(); |
| 65 | + if (m_optimiseYul) |
| 66 | + settings.runYulOptimiser = true; |
| 67 | + _compiler.setOptimiserSettings(settings); |
| 68 | +} |
| 69 | + |
| 70 | +std::map<std::string, Json> EthdebugTest::collectScopes() const |
| 71 | +{ |
| 72 | + std::map<std::string, Json> result; |
| 73 | + |
| 74 | + if (compiler().state() < CompilerStack::State::CompilationSuccessful) |
| 75 | + return result; |
| 76 | + |
| 77 | + result[".resources"] = compiler().ethdebug(); |
| 78 | + result[".compilation"] = compiler().ethdebugCompilation(); |
| 79 | + |
| 80 | + auto shortNameOf = [](std::string const& _qualified) { |
| 81 | + size_t colon = _qualified.rfind(':'); |
| 82 | + return colon == std::string::npos ? _qualified : _qualified.substr(colon + 1); |
| 83 | + }; |
| 84 | + |
| 85 | + std::map<std::string, int> shortNameCount; |
| 86 | + for (std::string const& contractName: compiler().contractNames()) |
| 87 | + ++shortNameCount[shortNameOf(contractName)]; |
| 88 | + |
| 89 | + for (std::string const& contractName: compiler().contractNames()) |
| 90 | + { |
| 91 | + std::string shortName = shortNameOf(contractName); |
| 92 | + Json creation = compiler().ethdebug(contractName); |
| 93 | + Json runtime = compiler().ethdebugRuntime(contractName); |
| 94 | + bool emitShortAlias = shortNameCount[shortName] == 1; |
| 95 | + |
| 96 | + result[contractName + ".creation"] = creation; |
| 97 | + result[contractName + ".runtime"] = runtime; |
| 98 | + if (emitShortAlias) |
| 99 | + { |
| 100 | + result[shortName + ".creation"] = creation; |
| 101 | + result[shortName + ".runtime"] = runtime; |
| 102 | + } |
| 103 | + // Shortcut: "C.contract" -> the contract sub-object from creation |
| 104 | + if (!creation.is_null() && creation.contains("contract")) |
| 105 | + { |
| 106 | + result[contractName + ".contract"] = creation["contract"]; |
| 107 | + if (emitShortAlias) |
| 108 | + result[shortName + ".contract"] = creation["contract"]; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return result; |
| 113 | +} |
| 114 | + |
| 115 | +std::pair<std::string, std::string> EthdebugTest::splitNonGlobalPath(std::string_view _path) const |
| 116 | +{ |
| 117 | + // Qualified paths look like "source.sol:C.kind.subpath" — used to disambiguate |
| 118 | + // when the same short contract name appears in multisource tests. |
| 119 | + auto colonPos = _path.find(':'); |
| 120 | + size_t contractStart = colonPos == std::string_view::npos ? 0 : colonPos + 1; |
| 121 | + |
| 122 | + auto firstDot = _path.find('.', contractStart); |
| 123 | + if (firstDot == std::string_view::npos) |
| 124 | + return {std::string(_path), ""}; |
| 125 | + |
| 126 | + auto secondDot = _path.find('.', firstDot + 1); |
| 127 | + if (secondDot == std::string_view::npos) |
| 128 | + return {std::string(_path), ""}; |
| 129 | + |
| 130 | + solAssert(secondDot < _path.size()); |
| 131 | + return {std::string(_path.substr(0, secondDot)), std::string(_path.substr(secondDot + 1))}; |
| 132 | +} |
0 commit comments