Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ set(libsolidity_sources
libsolidity/ASTJSONTest.h
libsolidity/ErrorCheck.cpp
libsolidity/ErrorCheck.h
libsolidity/EthdebugTest.cpp
libsolidity/EthdebugTest.h
libsolidity/FunctionDependencyGraphTest.cpp
libsolidity/FunctionDependencyGraphTest.h
libsolidity/GasCosts.cpp
Expand All @@ -100,6 +102,8 @@ set(libsolidity_sources
libsolidity/SemVerMatcher.cpp
libsolidity/SMTCheckerTest.cpp
libsolidity/SMTCheckerTest.h
libsolidity/StandardJSONTest.cpp
libsolidity/StandardJSONTest.h
libsolidity/SolidityCompiler.cpp
libsolidity/SolidityEndToEndTest.cpp
libsolidity/SolidityExecutionFramework.cpp
Expand Down
2 changes: 2 additions & 0 deletions test/InteractiveTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <test/libsolidity/ABIJsonTest.h>
#include <test/libsolidity/ASTJSONTest.h>
#include <test/libsolidity/ASTPropertyTest.h>
Comment thread
nikola-matic marked this conversation as resolved.
#include <test/libsolidity/EthdebugTest.h>
#include <libsolidity/FunctionDependencyGraphTest.h>
#include <test/libsolidity/GasTest.h>
#include <test/libsolidity/MemoryGuardTest.h>
Expand Down Expand Up @@ -89,6 +90,7 @@ Testsuite const g_interactiveTestsuites[] = {
{"JSON ABI", "libsolidity", "ABIJson", false, false, &ABIJsonTest::create},
{"JSON Natspec", "libsolidity", "natspecJSON", false, false, &NatspecJSONTest::create},
{"SMT Checker", "libsolidity", "smtCheckerTests", true, false, &SMTCheckerTest::create},
{"Ethdebug", "libsolidity", "ethdebugTests", false, false, &EthdebugTest::create},
{"Gas Estimates", "libsolidity", "gasTests", false, false, &GasTest::create},
{"Memory Guard", "libsolidity", "memoryGuardTests", false, false, &MemoryGuardTest::create},
{"AST Properties", "libsolidity", "astPropertyTests", false, false, &ASTPropertyTest::create},
Expand Down
132 changes: 132 additions & 0 deletions test/libsolidity/EthdebugTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
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 <test/libsolidity/EthdebugTest.h>
#include <test/Common.h>

#include <liblangutil/DebugInfoSelection.h>

#include <libsolidity/interface/OptimiserSettings.h>

#include <boost/throw_exception.hpp>

#include <stdexcept>

using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::langutil;

EthdebugTest::EthdebugTest(std::string const& _filename):
StandardJSONTest(_filename)
{
m_optimise = m_reader.boolSetting("optimize", false);
m_optimiseYul = m_reader.boolSetting("optimize-yul", false);
m_useSSACFG = m_reader.boolSetting("compileViaSSACFG", false);

auto const revertStrings = revertStringsFromString(m_reader.stringSetting("revertStrings", "default"));
if (!revertStrings)
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid revertStrings setting."));
m_revertStrings = *revertStrings;
}

void EthdebugTest::setupCompiler(CompilerStack& _compiler)
{
AnalysisFramework::setupCompiler(_compiler);

_compiler.setViaIR(true);
_compiler.setExperimental(true);
if (m_useSSACFG)
_compiler.setViaSSACFG(true);

DebugInfoSelection selection = DebugInfoSelection::Default();
selection.enable("ethdebug");
_compiler.selectDebugInfo(selection);

_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
_compiler.setRevertStringBehaviour(m_revertStrings);

OptimiserSettings settings = m_optimise ? OptimiserSettings::standard() : OptimiserSettings::minimal();
if (m_optimiseYul)
settings.runYulOptimiser = true;
_compiler.setOptimiserSettings(settings);
}

std::map<std::string, Json> EthdebugTest::collectScopes() const
{
std::map<std::string, Json> result;

if (compiler().state() < CompilerStack::State::CompilationSuccessful)
return result;

result[".resources"] = compiler().ethdebug();
result[".compilation"] = compiler().ethdebugCompilation();

static auto constexpr shortNameOf = [](std::string const& _qualified) {
size_t const colon = _qualified.rfind(':');
return colon == std::string::npos ? _qualified : _qualified.substr(colon + 1);
};

std::map<std::string, int> shortNameCount;
for (std::string const& contractName: compiler().contractNames())
++shortNameCount[shortNameOf(contractName)];

for (std::string const& contractName: compiler().contractNames())
{
std::string const shortName = shortNameOf(contractName);
Json const creation = compiler().ethdebug(contractName);
Json const runtime = compiler().ethdebugRuntime(contractName);
bool const emitShortAlias = shortNameCount[shortName] == 1;

result[contractName + ".creation"] = creation;
result[contractName + ".runtime"] = runtime;
if (emitShortAlias)
{
result[shortName + ".creation"] = creation;
result[shortName + ".runtime"] = runtime;
}
// Shortcut: "C.contract" -> the contract sub-object from creation
if (!creation.is_null() && creation.contains("contract"))
{
result[contractName + ".contract"] = creation["contract"];
if (emitShortAlias)
result[shortName + ".contract"] = creation["contract"];
}
}

return result;
}

std::pair<std::string_view, std::string_view> EthdebugTest::splitNonGlobalPath(std::string_view _path) const
{
// Qualified paths look like "source.sol:C.kind.subpath" — used to disambiguate
// when the same short contract name appears in multisource tests.
auto const colonPos = _path.find(':');
size_t const contractStart = colonPos == std::string_view::npos ? 0 : colonPos + 1;

auto const firstDot = _path.find('.', contractStart);
if (firstDot == std::string_view::npos)
return {_path, ""};

auto const secondDot = _path.find('.', firstDot + 1);
if (secondDot == std::string_view::npos)
return {_path, ""};

solAssert(secondDot < _path.size());
return {_path.substr(0, secondDot), _path.substr(secondDot + 1)};
}
67 changes: 67 additions & 0 deletions test/libsolidity/EthdebugTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
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

#pragma once

#include <test/libsolidity/StandardJSONTest.h>

#include <libsolidity/interface/DebugSettings.h>

namespace solidity::frontend::test
{
/// Isoltest test case for asserting against the compiler's ethdebug JSON output.
///
/// Settings (in the `// ====` block):
/// - `EVMVersion: <constraint>` — gates whether the test runs (inherited).
/// - `optimize: <bool>` — enable optimizer (default: false).
/// - `optimize-yul: <bool>` — additionally run the Yul optimizer (default: false).
/// - `compileViaSSACFG: <bool>` — route codegen through the SSA-CFG (default: false).
/// - `revertStrings: default|strip|debug|verboseDebug`
/// — revert string behavior (default: default).
///
/// Compilation is always performed with `viaIR=true`, `experimental=true`, ethdebug
/// debug info enabled, and metadata stripped, so the output is stable across builds.
///
/// Scope keys exposed to expectations:
/// - Globals: `.resources`, `.compilation`.
/// - Per contract: `Contract.creation`, `Contract.runtime`, `Contract.contract`.
/// - Source-qualified per contract (when needed to disambiguate same-named
/// contracts in different sources): `source.sol:Contract.creation`, etc.
class EthdebugTest: public StandardJSONTest
{
public:
static std::unique_ptr<TestCase> create(Config const& _config)
{
return std::make_unique<EthdebugTest>(_config.filename);
}

EthdebugTest(std::string const& _filename);

protected:
void setupCompiler(CompilerStack& _compiler) override;
std::map<std::string, Json> collectScopes() const override;
std::pair<std::string_view, std::string_view> splitNonGlobalPath(std::string_view _path) const override;

private:
bool m_optimise = false;
bool m_optimiseYul = false;
bool m_useSSACFG = false;
RevertStrings m_revertStrings = RevertStrings::Default;
};

}
Loading