Restrict the state when settings can be altered in CompilerStack

This commit is contained in:
Alex Beregszaszi 2019-02-03 00:32:34 +00:00
parent e9543d83c7
commit b04b86a938
3 changed files with 45 additions and 17 deletions

View File

@ -86,6 +86,8 @@ boost::optional<CompilerStack::Remapping> CompilerStack::parseRemapping(string c
void CompilerStack::setRemappings(vector<Remapping> const& _remappings)
{
if (m_stackState >= ParsingSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set remappings before parsing."));
for (auto const& remapping: _remappings)
solAssert(!remapping.prefix.empty(), "");
m_remappings = _remappings;
@ -93,10 +95,40 @@ void CompilerStack::setRemappings(vector<Remapping> const& _remappings)
void CompilerStack::setEVMVersion(langutil::EVMVersion _version)
{
solAssert(m_stackState < State::ParsingSuccessful, "Set EVM version after parsing.");
if (m_stackState >= ParsingSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set EVM version before parsing."));
m_evmVersion = _version;
}
void CompilerStack::setLibraries(std::map<std::string, h160> const& _libraries)
{
if (m_stackState >= ParsingSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set libraries before parsing."));
m_libraries = _libraries;
}
void CompilerStack::setOptimiserSettings(bool _optimize, unsigned _runs)
{
if (m_stackState >= ParsingSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set optimiser settings before parsing."));
m_optimize = _optimize;
m_optimizeRuns = _runs;
}
void CompilerStack::useMetadataLiteralSources(bool _metadataLiteralSources)
{
if (m_stackState >= ParsingSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set use literal sources before parsing."));
m_metadataLiteralSources = _metadataLiteralSources;
}
void CompilerStack::addSMTLib2Response(h256 const& _hash, string const& _response)
{
if (m_stackState >= ParsingSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must add SMTLib2 responses before parsing."));
m_smtlib2Responses[_hash] = _response;
}
void CompilerStack::reset(bool _keepSources)
{
if (_keepSources)

View File

@ -117,43 +117,39 @@ public:
static boost::optional<Remapping> parseRemapping(std::string const& _remapping);
/// Sets path remappings.
/// Must be set before parsing.
void setRemappings(std::vector<Remapping> const& _remappings);
/// Sets library addresses. Addresses are cleared iff @a _libraries is missing.
/// Will not take effect before running compile.
void setLibraries(std::map<std::string, h160> const& _libraries = std::map<std::string, h160>{})
{
m_libraries = _libraries;
}
/// Must be set before parsing.
void setLibraries(std::map<std::string, h160> const& _libraries = std::map<std::string, h160>{});
/// Changes the optimiser settings.
/// Will not take effect before running compile.
void setOptimiserSettings(bool _optimize, unsigned _runs = 200)
{
m_optimize = _optimize;
m_optimizeRuns = _runs;
}
/// Must be set before parsing.
void setOptimiserSettings(bool _optimize, unsigned _runs = 200);
/// Set the EVM version used before running compile.
/// When called without an argument it will revert to the default version.
/// Must be set before parsing.
void setEVMVersion(langutil::EVMVersion _version = langutil::EVMVersion{});
/// Sets the list of requested contract names. If empty, no filtering is performed and every contract
/// found in the supplied sources is compiled. Names are cleared iff @a _contractNames is missing.
void setRequestedContractNames(std::set<std::string> const& _contractNames = std::set<std::string>{})
{
void setRequestedContractNames(std::set<std::string> const& _contractNames = std::set<std::string>{}) {
m_requestedContractNames = _contractNames;
}
/// @arg _metadataLiteralSources When true, store sources as literals in the contract metadata.
void useMetadataLiteralSources(bool _metadataLiteralSources) { m_metadataLiteralSources = _metadataLiteralSources; }
/// Must be set before parsing.
void useMetadataLiteralSources(bool _metadataLiteralSources);
/// Adds a source object (e.g. file) to the parser. After this, parse has to be called again.
/// @returns true if a source object by the name already existed and was replaced.
bool addSource(std::string const& _name, std::string const& _content, bool _isLibrary = false);
/// Adds a response to an SMTLib2 query (identified by the hash of the query input).
void addSMTLib2Response(h256 const& _hash, std::string const& _response) { m_smtlib2Responses[_hash] = _response; }
/// Must be set before parsing.
void addSMTLib2Response(h256 const& _hash, std::string const& _response);
/// Parses all source units that were added
/// @returns false on error.

View File

@ -42,8 +42,8 @@ BOOST_AUTO_TEST_CASE(does_not_include_creation_time_only_internal_functions)
function f() internal { for (uint i = 0; i < 10; ++i) x += 3 + i; }
}
)";
BOOST_REQUIRE(success(sourceCode));
m_compiler.setOptimiserSettings(dev::test::Options::get().optimize);
BOOST_REQUIRE(success(sourceCode));
BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
bytes const& creationBytecode = m_compiler.object("C").bytecode;
bytes const& runtimeBytecode = m_compiler.runtimeObject("C").bytecode;