Unify preamble handling between AnalysisFramework and SolidityExecutionFramework

This commit is contained in:
Kamil Śliwak 2023-08-11 14:43:37 +02:00
parent 18db62cf41
commit ec92685bcb
4 changed files with 17 additions and 28 deletions

View File

@ -22,6 +22,7 @@
*/ */
#include <test/libsolidity/SolidityExecutionFramework.h> #include <test/libsolidity/SolidityExecutionFramework.h>
#include <test/libsolidity/util/Common.h>
#include <liblangutil/DebugInfoSelection.h> #include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
@ -48,12 +49,12 @@ bytes SolidityExecutionFramework::multiSourceCompileContract(
{ {
if (_mainSourceName.has_value()) if (_mainSourceName.has_value())
solAssert(_sourceCode.find(_mainSourceName.value()) != _sourceCode.end(), ""); solAssert(_sourceCode.find(_mainSourceName.value()) != _sourceCode.end(), "");
map<string, string> sourcesWithPreamble = _sourceCode;
for (auto& entry: sourcesWithPreamble)
entry.second = addPreamble(entry.second);
m_compiler.reset(); m_compiler.reset();
m_compiler.setSources(sourcesWithPreamble); m_compiler.setSources(withPreamble(
_sourceCode,
solidity::test::CommonOptions::get().useABIEncoderV1 // _addAbicoderV1Pragma
));
m_compiler.setLibraries(_libraryAddresses); m_compiler.setLibraries(_libraryAddresses);
m_compiler.setRevertStringBehaviour(m_revertStrings); m_compiler.setRevertStringBehaviour(m_revertStrings);
m_compiler.setEVMVersion(m_evmVersion); m_compiler.setEVMVersion(m_evmVersion);
@ -141,18 +142,3 @@ bytes SolidityExecutionFramework::compileContract(
_libraryAddresses _libraryAddresses
); );
} }
string SolidityExecutionFramework::addPreamble(string const& _sourceCode)
{
// Silence compiler version warning
string preamble = "pragma solidity >=0.0;\n";
if (_sourceCode.find("SPDX-License-Identifier:") == string::npos)
preamble += "// SPDX-License-Identifier: unlicensed\n";
if (
solidity::test::CommonOptions::get().useABIEncoderV1 &&
_sourceCode.find("pragma experimental ABIEncoderV2;") == string::npos &&
_sourceCode.find("pragma abicoder") == string::npos
)
preamble += "pragma abicoder v1;\n";
return preamble + _sourceCode;
}

View File

@ -79,9 +79,6 @@ public:
std::map<std::string, solidity::test::Address> const& _libraryAddresses = {} std::map<std::string, solidity::test::Address> const& _libraryAddresses = {}
); );
/// Returns @param _sourceCode prefixed with the version pragma and the abi coder v1 pragma,
/// the latter only if it is forced.
static std::string addPreamble(std::string const& _sourceCode);
protected: protected:
using CompilerStack = solidity::frontend::CompilerStack; using CompilerStack = solidity::frontend::CompilerStack;
std::optional<uint8_t> m_eofVersion; std::optional<uint8_t> m_eofVersion;

View File

@ -22,25 +22,30 @@ using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::frontend; using namespace solidity::frontend;
string test::withPreamble(string const& _sourceCode) string test::withPreamble(string const& _sourceCode, bool _addAbicoderV1Pragma)
{ {
static string const versionPragma = "pragma solidity >=0.0;\n"; static string const versionPragma = "pragma solidity >=0.0;\n";
static string const licenseComment = "// SPDX-License-Identifier: GPL-3.0\n"; static string const licenseComment = "// SPDX-License-Identifier: GPL-3.0\n";
static string const abicoderPragma = "pragma abicoder v1;\n";
// NOTE: this check is intentionally loose to match weird cases. // NOTE: These checks are intentionally loose to match weird cases.
// We can manually adjust a test case where this causes problem. // We can manually adjust a test case where this causes problem.
bool licenseMissing = _sourceCode.find("SPDX-License-Identifier:") == string::npos; bool licenseMissing = _sourceCode.find("SPDX-License-Identifier:") == string::npos;
bool abicoderMissing =
_sourceCode.find("pragma experimental ABIEncoderV2;") == string::npos &&
_sourceCode.find("pragma abicoder") == string::npos;
return return
versionPragma + versionPragma +
(licenseMissing ? licenseComment : "") + (licenseMissing ? licenseComment : "") +
(abicoderMissing && _addAbicoderV1Pragma ? abicoderPragma : "") +
_sourceCode; _sourceCode;
} }
StringMap test::withPreamble(StringMap _sources) StringMap test::withPreamble(StringMap _sources, bool _addAbicoderV1Pragma)
{ {
for (auto&& [sourceName, source]: _sources) for (auto&& [sourceName, source]: _sources)
source = withPreamble(source); source = withPreamble(source, _addAbicoderV1Pragma);
return _sources; return _sources;
} }

View File

@ -26,9 +26,10 @@ namespace solidity::frontend::test
{ {
/// @returns @p _sourceCode prefixed with the version pragma and the SPDX license identifier. /// @returns @p _sourceCode prefixed with the version pragma and the SPDX license identifier.
std::string withPreamble(std::string const& _sourceCode); /// Can optionally also insert an abicoder pragma when missing.
std::string withPreamble(std::string const& _sourceCode, bool _addAbicoderV1Pragma = false);
/// @returns a copy of @p _sources with preamble prepended to all sources. /// @returns a copy of @p _sources with preamble prepended to all sources.
StringMap withPreamble(StringMap _sources); StringMap withPreamble(StringMap _sources, bool _addAbicoderV1Pragma = false);
} // namespace solidity::frontend::test } // namespace solidity::frontend::test