diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index e9f694c11..4d0010428 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -86,6 +86,8 @@ boost::optional CompilerStack::parseRemapping(string c void CompilerStack::setRemappings(vector 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 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 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) diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index 12a0a5e0c..ffae921ab 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -117,43 +117,39 @@ public: static boost::optional parseRemapping(std::string const& _remapping); /// Sets path remappings. + /// Must be set before parsing. void setRemappings(std::vector const& _remappings); /// Sets library addresses. Addresses are cleared iff @a _libraries is missing. - /// Will not take effect before running compile. - void setLibraries(std::map const& _libraries = std::map{}) - { - m_libraries = _libraries; - } + /// Must be set before parsing. + void setLibraries(std::map const& _libraries = std::map{}); /// 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 const& _contractNames = std::set{}) - { + void setRequestedContractNames(std::set const& _contractNames = std::set{}) { 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. diff --git a/test/libsolidity/SolidityCompiler.cpp b/test/libsolidity/SolidityCompiler.cpp index 0cc80dd82..57a7c8d1b 100644 --- a/test/libsolidity/SolidityCompiler.cpp +++ b/test/libsolidity/SolidityCompiler.cpp @@ -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;