AnalysisFramework: Get rid of parseAndAnalyse() and success()

- There are too many badly named functions doing different things. E.g. SyntaxTest has both this and parseAndAnalyze().
This commit is contained in:
Kamil Śliwak 2023-08-04 19:41:49 +02:00
parent 309a8de530
commit b766268ff3
3 changed files with 56 additions and 46 deletions

View File

@ -173,22 +173,6 @@ bool AnalysisFramework::stageSuccessful(PipelineStage _stage) const
unreachable();
}
SourceUnit const* AnalysisFramework::parseAndAnalyse(std::string const& _source)
{
auto sourceAndError = parseAnalyseAndReturnError(_source);
BOOST_REQUIRE(!!sourceAndError.first);
std::string message;
if (!sourceAndError.second.empty())
message = "Unexpected error: " + formatErrors(compiler().errors());
BOOST_REQUIRE_MESSAGE(sourceAndError.second.empty(), message);
return sourceAndError.first;
}
bool AnalysisFramework::success(std::string const& _source)
{
return parseAnalyseAndReturnError(_source).second.empty();
}
ErrorList AnalysisFramework::expectError(std::string const& _source, bool _warning, bool _allowMultiple)
{
auto sourceAndErrors = parseAnalyseAndReturnError(_source, _warning, true, _allowMultiple);

View File

@ -58,8 +58,6 @@ protected:
);
virtual ~AnalysisFramework() = default;
SourceUnit const* parseAndAnalyse(std::string const& _source);
bool success(std::string const& _source);
langutil::ErrorList expectError(std::string const& _source, bool _warning = false, bool _allowMultiple = false);
public:
@ -189,16 +187,19 @@ CHECK_ERROR_OR_WARNING(text, Warning, std::vector<std::string>{(substring)}, tru
CHECK_ERROR_OR_WARNING(text, Warning, substrings, true, true)
// [checkSuccess(text)] asserts that the compilation down to typechecking succeeds.
#define CHECK_SUCCESS(text) do { BOOST_CHECK(success((text))); } while(0)
#define CHECK_SUCCESS(text) do { \
auto [ast, errors] = parseAnalyseAndReturnError((text)); \
BOOST_CHECK(errors.empty()); \
} while(0)
#define CHECK_SUCCESS_NO_WARNINGS(text) \
do \
{ \
auto sourceAndError = parseAnalyseAndReturnError((text), true); \
auto [ast, errors] = parseAnalyseAndReturnError((text), true); \
std::string message; \
if (!sourceAndError.second.empty()) \
message = formatErrors(compiler().errors());\
BOOST_CHECK_MESSAGE(sourceAndError.second.empty(), message); \
if (!errors.empty()) \
message = formatErrors(errors);\
BOOST_CHECK_MESSAGE(errors.empty(), message); \
} \
while(0)

View File

@ -21,6 +21,7 @@
*/
#include <test/libsolidity/AnalysisFramework.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/Common.h>
@ -41,13 +42,15 @@ BOOST_FIXTURE_TEST_SUITE(SolidityNameAndTypeResolution, AnalysisFramework)
BOOST_AUTO_TEST_CASE(function_no_implementation)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
abstract contract test {
function functionName(bytes32 input) public virtual returns (bytes32 out);
}
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes();
ContractDefinition* contract = dynamic_cast<ContractDefinition*>(nodes[1].get());
BOOST_REQUIRE(contract);
@ -57,12 +60,14 @@ BOOST_AUTO_TEST_CASE(function_no_implementation)
BOOST_AUTO_TEST_CASE(abstract_contract)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
abstract contract base { function foo() public virtual; }
contract derived is base { function foo() public override {} }
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes();
ContractDefinition* base = dynamic_cast<ContractDefinition*>(nodes[1].get());
ContractDefinition* derived = dynamic_cast<ContractDefinition*>(nodes[2].get());
@ -76,12 +81,14 @@ BOOST_AUTO_TEST_CASE(abstract_contract)
BOOST_AUTO_TEST_CASE(abstract_contract_with_overload)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
abstract contract base { function foo(bool) public virtual; }
abstract contract derived is base { function foo(uint) public {} }
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes();
ContractDefinition* base = dynamic_cast<ContractDefinition*>(nodes[1].get());
ContractDefinition* derived = dynamic_cast<ContractDefinition*>(nodes[2].get());
@ -93,12 +100,14 @@ BOOST_AUTO_TEST_CASE(abstract_contract_with_overload)
BOOST_AUTO_TEST_CASE(implement_abstract_via_constructor)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
abstract contract base { function foo() public virtual; }
abstract contract foo is base { constructor() {} }
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes();
BOOST_CHECK_EQUAL(nodes.size(), 3);
ContractDefinition* derived = dynamic_cast<ContractDefinition*>(nodes[2].get());
@ -108,7 +117,6 @@ BOOST_AUTO_TEST_CASE(implement_abstract_via_constructor)
BOOST_AUTO_TEST_CASE(function_canonical_signature)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
contract Test {
function foo(uint256 arg1, uint64 arg2, bool arg3) public returns (uint256 ret) {
@ -116,7 +124,10 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature)
}
}
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
@ -127,7 +138,6 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature)
BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
contract Test {
function boo(uint, bytes32, address) public returns (uint ret) {
@ -135,7 +145,10 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
}
}
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
@ -148,7 +161,6 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
BOOST_AUTO_TEST_CASE(function_external_types)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
contract C {
uint a;
@ -159,7 +171,10 @@ BOOST_AUTO_TEST_CASE(function_external_types)
}
}
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
@ -172,7 +187,6 @@ BOOST_AUTO_TEST_CASE(function_external_types)
BOOST_AUTO_TEST_CASE(enum_external_type)
{
SourceUnit const* sourceUnit = nullptr;
char const* text = R"(
// test for bug #1801
contract Test {
@ -182,7 +196,10 @@ BOOST_AUTO_TEST_CASE(enum_external_type)
}
}
)";
sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
@ -264,7 +281,10 @@ BOOST_AUTO_TEST_CASE(struct_with_mapping_in_library)
function f(X storage x) external {}
}
)";
SourceUnit const* sourceUnit = parseAndAnalyse(text);
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
@ -287,10 +307,12 @@ BOOST_AUTO_TEST_CASE(state_variable_accessors)
}
)";
SourceUnit const* source;
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
ContractDefinition const* contract;
source = parseAndAnalyse(text);
BOOST_REQUIRE((contract = retrieveContractByName(*source, "test")) != nullptr);
BOOST_REQUIRE((contract = retrieveContractByName(*sourceUnit, "test")) != nullptr);
FunctionTypePointer function = retrieveFunctionBySignature(*contract, "foo()");
BOOST_REQUIRE(function && function->hasDeclaration());
auto returnParams = function->returnParameterTypes();
@ -327,9 +349,12 @@ BOOST_AUTO_TEST_CASE(private_state_variable)
}
)";
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
soltestAssert(sourceUnit);
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
ContractDefinition const* contract;
SourceUnit const* source = parseAndAnalyse(text);
BOOST_CHECK((contract = retrieveContractByName(*source, "test")) != nullptr);
BOOST_CHECK((contract = retrieveContractByName(*sourceUnit, "test")) != nullptr);
FunctionTypePointer function;
function = retrieveFunctionBySignature(*contract, "foo()");
BOOST_CHECK_MESSAGE(function == nullptr, "Accessor function of a private variable should not exist");
@ -345,7 +370,7 @@ BOOST_AUTO_TEST_CASE(string)
function f(string calldata x) external { s = x; }
}
)";
BOOST_CHECK_NO_THROW(parseAndAnalyse(sourceCode));
CHECK_SUCCESS(sourceCode);
}
BOOST_AUTO_TEST_CASE(dynamic_return_types_not_possible)