Skip to content

Commit ca779a4

Browse files
committed
Test: Add universal host for compilers under test
1 parent 95f517f commit ca779a4

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ set(libsolidity_util_sources
121121
libsolidity/util/compiler/Compiler.cpp
122122
libsolidity/util/compiler/InternalCompiler.h
123123
libsolidity/util/compiler/InternalCompiler.cpp
124+
libsolidity/util/compiler/CompilerHost.h
125+
libsolidity/util/compiler/CompilerHost.cpp
124126
libsolidity/util/BytesUtils.cpp
125127
libsolidity/util/BytesUtilsTests.cpp
126128
libsolidity/util/BytesUtils.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <test/libsolidity/util/compiler/CompilerHost.h>
2+
3+
using namespace solidity;
4+
using namespace solidity::frontend::test;
5+
6+
CompilerOutput const& CompilerHost::compile(CompilerInput _input)
7+
{
8+
auto compile = [=](auto& compiler) { return compiler.compile(_input); };
9+
auto output = std::visit(compile, m_compiler);
10+
11+
m_output.emplace(output);
12+
13+
return this->output();
14+
}
15+
16+
void CompilerHost::reset()
17+
{
18+
m_output.reset();
19+
}
20+
21+
CompilerOutput const& CompilerHost::output() const
22+
{
23+
solAssert(m_output.has_value(), "No output found. Please compile first.");
24+
return m_output.value();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#pragma once
20+
21+
#include <test/libsolidity/util/compiler/Compiler.h>
22+
#include <test/libsolidity/util/compiler/InternalCompiler.h>
23+
24+
namespace solidity::frontend::test
25+
{
26+
27+
class CompilerHost
28+
{
29+
public:
30+
/// Takes the current compiler input, requests the hosted compiler to compile
31+
/// and updates the output accordingly.
32+
CompilerOutput const& compile(CompilerInput _input);
33+
34+
/// Resets the output generated by the previous compilation.
35+
void reset();
36+
37+
/// @returns the stored output generated by the previous compilation.
38+
CompilerOutput const& output() const;
39+
40+
private:
41+
/// Instance of an invocable compiler.
42+
std::variant<InternalCompiler> m_compiler;
43+
/// Last generated output. Will be none before initial compilation
44+
/// and after reset.
45+
std::optional<CompilerOutput> m_output;
46+
};
47+
48+
}

test/tools/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ add_executable(isoltest
2525
../libsolidity/util/TestFunctionCall.cpp
2626
../libsolidity/util/compiler/Compiler.h
2727
../libsolidity/util/compiler/Compiler.cpp
28+
../libsolidity/util/compiler/CompilerHost.h
29+
../libsolidity/util/compiler/CompilerHost.cpp
2830
../libsolidity/util/compiler/InternalCompiler.h
2931
../libsolidity/util/compiler/InternalCompiler.cpp
3032
../libsolidity/GasTest.cpp

0 commit comments

Comments
 (0)