Merge pull request #5674 from ethereum/parser-pragmaversion

Add version pragma parsing to Parser
This commit is contained in:
chriseth 2018-12-18 16:42:22 +01:00 committed by GitHub
commit d959407f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 14 deletions

View File

@ -114,7 +114,7 @@ bool SyntaxChecker::visit(PragmaDirective const& _pragma)
vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end()); vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end());
SemVerMatchExpressionParser parser(tokens, literals); SemVerMatchExpressionParser parser(tokens, literals);
auto matchExpression = parser.parse(); auto matchExpression = parser.parse();
SemVerVersion currentVersion{string(VersionString)}; static SemVerVersion const currentVersion{string(VersionString)};
if (!matchExpression.matches(currentVersion)) if (!matchExpression.matches(currentVersion))
m_errorReporter.syntaxError( m_errorReporter.syntaxError(
_pragma.location(), _pragma.location(),

View File

@ -22,6 +22,8 @@
#include <libsolidity/parsing/Parser.h> #include <libsolidity/parsing/Parser.h>
#include <libsolidity/analysis/SemVerHandler.h>
#include <libsolidity/interface/Version.h>
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/backends/evm/EVMDialect.h> #include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
@ -106,6 +108,20 @@ ASTPointer<SourceUnit> Parser::parse(shared_ptr<Scanner> const& _scanner)
} }
} }
void Parser::parsePragmaVersion(vector<Token> const& tokens, vector<string> const& literals)
{
SemVerMatchExpressionParser parser(tokens, literals);
auto matchExpression = parser.parse();
static SemVerVersion const currentVersion{string(VersionString)};
// FIXME: only match for major version incompatibility
if (!matchExpression.matches(currentVersion))
fatalParserError(
"Source file requires different compiler version (current compiler is " +
string(VersionString) + " - note that nightly builds are considered to be "
"strictly less than the released version"
);
}
ASTPointer<PragmaDirective> Parser::parsePragmaDirective() ASTPointer<PragmaDirective> Parser::parsePragmaDirective()
{ {
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
@ -134,6 +150,15 @@ ASTPointer<PragmaDirective> Parser::parsePragmaDirective()
while (m_scanner->currentToken() != Token::Semicolon && m_scanner->currentToken() != Token::EOS); while (m_scanner->currentToken() != Token::Semicolon && m_scanner->currentToken() != Token::EOS);
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
expectToken(Token::Semicolon); expectToken(Token::Semicolon);
if (literals.size() >= 2 && literals[0] == "solidity")
{
parsePragmaVersion(
vector<Token>(tokens.begin() + 1, tokens.end()),
vector<string>(literals.begin() + 1, literals.end())
);
}
return nodeFactory.createNode<PragmaDirective>(tokens, literals); return nodeFactory.createNode<PragmaDirective>(tokens, literals);
} }

View File

@ -70,6 +70,7 @@ private:
///@{ ///@{
///@name Parsing functions for the AST nodes ///@name Parsing functions for the AST nodes
void parsePragmaVersion(std::vector<Token> const& tokens, std::vector<std::string> const& literals);
ASTPointer<PragmaDirective> parsePragmaDirective(); ASTPointer<PragmaDirective> parsePragmaDirective();
ASTPointer<ImportDirective> parseImportDirective(); ASTPointer<ImportDirective> parseImportDirective();
ContractDefinition::ContractKind parseContractKind(); ContractDefinition::ContractKind parseContractKind();

View File

@ -52,7 +52,7 @@ AnalysisFramework::parseAnalyseAndReturnError(
m_compiler.setEVMVersion(dev::test::Options::get().evmVersion()); m_compiler.setEVMVersion(dev::test::Options::get().evmVersion());
if (!m_compiler.parse()) if (!m_compiler.parse())
{ {
BOOST_ERROR("Parsing contract failed in analysis test suite:" + formatErrors()); BOOST_FAIL("Parsing contract failed in analysis test suite:" + formatErrors());
} }
m_compiler.analyze(); m_compiler.analyze();

View File

@ -376,18 +376,6 @@ BOOST_AUTO_TEST_CASE(warn_nonpresent_pragma)
BOOST_CHECK(searchErrorMessage(*sourceAndError.second.front(), "Source file does not specify required compiler version!")); BOOST_CHECK(searchErrorMessage(*sourceAndError.second.front(), "Source file does not specify required compiler version!"));
} }
BOOST_AUTO_TEST_CASE(unsatisfied_version)
{
char const* text = R"(
pragma solidity ^99.99.0;
)";
auto sourceAndError = parseAnalyseAndReturnError(text, false, false, false);
BOOST_REQUIRE(!sourceAndError.second.empty());
BOOST_REQUIRE(!!sourceAndError.first);
BOOST_CHECK(sourceAndError.second.front()->type() == Error::Type::SyntaxError);
BOOST_CHECK(searchErrorMessage(*sourceAndError.second.front(), "Source file requires different compiler version"));
}
BOOST_AUTO_TEST_CASE(returndatasize_as_variable) BOOST_AUTO_TEST_CASE(returndatasize_as_variable)
{ {
char const* text = R"( char const* text = R"(

View File

@ -113,6 +113,23 @@ while(0)
BOOST_AUTO_TEST_SUITE(SolidityParser) BOOST_AUTO_TEST_SUITE(SolidityParser)
BOOST_AUTO_TEST_CASE(unsatisfied_version)
{
char const* text = R"(
pragma solidity ^99.99.0;
)";
CHECK_PARSE_ERROR(text, "Source file requires different compiler version");
}
BOOST_AUTO_TEST_CASE(unsatisfied_version_followed_by_invalid_syntax)
{
char const* text = R"(
pragma solidity ^99.99.0;
this is surely invalid
)";
CHECK_PARSE_ERROR(text, "Source file requires different compiler version");
}
BOOST_AUTO_TEST_CASE(function_natspec_documentation) BOOST_AUTO_TEST_CASE(function_natspec_documentation)
{ {
char const* text = R"( char const* text = R"(