Skip to content

Commit

Permalink
Unify preamble handling between test cases based on AnalysisFramework
Browse files Browse the repository at this point in the history
  • Loading branch information
cameel committed Aug 8, 2023
1 parent 9c059a3 commit 633cb41
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
29 changes: 22 additions & 7 deletions test/libsolidity/AnalysisFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ AnalysisFramework::parseAnalyseAndReturnError(
)
{
compiler().reset();
// Do not insert license if it is already present.
bool insertLicense = _insertLicenseAndVersionPragma && _source.find("SPDX-License-Identifier:") == string::npos;
compiler().setSources({{"",
string{_insertLicenseAndVersionPragma ? "pragma solidity >=0.0;\n" : ""} +
string{insertLicense ? "// SPDX-License-Identifier: GPL-3.0\n" : ""} +
_source
}});
compiler().setSources({{"", _insertLicenseAndVersionPragma ? withPreamble(_source) : _source}});
compiler().setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
compiler().setParserErrorRecovery(_allowRecoveryErrors);
_allowMultipleErrors = _allowMultipleErrors || _allowRecoveryErrors;
Expand Down Expand Up @@ -191,3 +185,24 @@ FunctionTypePointer AnalysisFramework::retrieveFunctionBySignature(
{
return _contract.interfaceFunctions()[util::selectorFromSignatureH32(_signature)];
}

string AnalysisFramework::withPreamble(string const& _sourceCode)
{
static string const version = "pragma solidity >=0.0;\n";
static string const license = "// SPDX-License-Identifier: GPL-3.0\n";

// NOTE: this check is intentionally loose to match weird cases.
// We can manually adjust a test case where this causes problem.
if (_sourceCode.find("SPDX-License-Identifier:") != string::npos)
return version + _sourceCode;

return version + license + _sourceCode;
}

StringMap AnalysisFramework::withPreamble(StringMap _sources)
{
for (auto&& [sourceName, source]: _sources)
source = withPreamble(source);

return _sources;
}
5 changes: 5 additions & 0 deletions test/libsolidity/AnalysisFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class AnalysisFramework
/// custom constructor arguments.
virtual std::unique_ptr<CompilerStack> createStack() const;

/// @returns @p _sourceCode prefixed with the version pragma and the SPDX license identifier.
static std::string withPreamble(std::string const& _sourceCode);
/// @returns a copy of @p _sources with preamble prepended to all sources.
static StringMap withPreamble(StringMap _sources);

private:
mutable std::unique_ptr<solidity::frontend::CompilerStack> m_compiler;
};
Expand Down
3 changes: 1 addition & 2 deletions test/libsolidity/GasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ void GasTest::printUpdatedExpectations(ostream& _stream, string const& _linePref

TestCase::TestResult GasTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
{
string const preamble = "pragma solidity >=0.0;\n// SPDX-License-Identifier: GPL-3.0\n";
compiler().reset();
// Prerelease CBOR metadata varies in size due to changing version numbers and build dates.
// This leads to volatile creation cost estimates. Therefore we force the compiler to
Expand All @@ -114,7 +113,7 @@ TestCase::TestResult GasTest::run(ostream& _stream, string const& _linePrefix, b
}
settings.expectedExecutionsPerDeployment = m_optimiseRuns;
compiler().setOptimiserSettings(settings);
compiler().setSources({{"", preamble + m_source}});
compiler().setSources({{"", withPreamble(m_source)}});

if (!compiler().parseAndAnalyze() || !compiler().compile())
{
Expand Down
16 changes: 1 addition & 15 deletions test/libsolidity/SyntaxTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,10 @@ SyntaxTest::SyntaxTest(string const& _filename, langutil::EVMVersion _evmVersion
m_parserErrorRecovery = _parserErrorRecovery;
}

string SyntaxTest::addPreamble(string const& _sourceCode)
{
// Silence compiler version warning
string preamble = "pragma solidity >=0.0;\n";
// NOTE: this check is intentionally loose to match weird cases.
// We can manually adjust a test case where this causes problem.
if (_sourceCode.find("SPDX-License-Identifier:") == string::npos)
preamble += "// SPDX-License-Identifier: GPL-3.0\n";
return preamble + _sourceCode;
}

void SyntaxTest::setupCompiler()
{
compiler().reset();
auto sourcesWithPragma = m_sources.sources;
for (auto& source: sourcesWithPragma)
source.second = addPreamble(source.second);
compiler().setSources(sourcesWithPragma);
compiler().setSources(withPreamble(m_sources.sources));
compiler().setEVMVersion(m_evmVersion);
compiler().setParserErrorRecovery(m_parserErrorRecovery);
compiler().setOptimiserSettings(
Expand Down
3 changes: 0 additions & 3 deletions test/libsolidity/SyntaxTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ class SyntaxTest: public AnalysisFramework, public solidity::test::CommonSyntaxT
SyntaxTest(std::string const& _filename, langutil::EVMVersion _evmVersion, bool _parserErrorRecovery = false);

protected:
/// Returns @param _sourceCode prefixed with the version pragma and the SPDX license identifier.
static std::string addPreamble(std::string const& _sourceCode);

virtual void setupCompiler();
void parseAndAnalyze() override;
virtual void filterObtainedErrors();
Expand Down

0 comments on commit 633cb41

Please sign in to comment.