Skip to content

Commit d2bfb2f

Browse files
committed
AnalysisFramework: Reimplement parseAnalyseAndReturnError() with runFramework()
1 parent b766268 commit d2bfb2f

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

test/libsolidity/AnalysisFramework.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,20 @@ using namespace solidity::langutil;
4242
using namespace solidity::frontend;
4343
using namespace solidity::frontend::test;
4444

45-
std::pair<SourceUnit const*, ErrorList>
46-
AnalysisFramework::parseAnalyseAndReturnError(
45+
std::pair<SourceUnit const*, ErrorList> AnalysisFramework::runAnalysisAndExpectNoParsingErrors(
4746
std::string const& _source,
48-
bool _reportWarnings,
49-
bool _insertLicenseAndVersionPragma,
50-
bool _allowMultipleErrors
47+
bool _includeWarningsAndInfos,
48+
bool _addPreamble,
49+
bool _allowMultiple
5150
)
5251
{
53-
compiler().reset();
54-
compiler().setSources({{"", _insertLicenseAndVersionPragma ? withPreamble(_source) : _source}});
55-
compiler().setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
56-
if (!compiler().parse())
57-
{
58-
BOOST_FAIL("Parsing contract failed in analysis test suite:" + formatErrors(compiler().errors()));
59-
}
52+
runFramework(_addPreamble ? withPreamble(_source) : _source, PipelineStage::Analysis);
6053

61-
compiler().analyze();
54+
if (!stageSuccessful(PipelineStage::Parsing))
55+
BOOST_FAIL("Parsing contract failed in analysis test suite:" + formatErrors(m_compiler->errors()));
6256

63-
ErrorList errors = filteredErrors(_reportWarnings);
64-
if (errors.size() > 1 && !_allowMultipleErrors)
57+
ErrorList errors = filteredErrors(_includeWarningsAndInfos);
58+
if (errors.size() > 1 && !_allowMultiple)
6559
BOOST_FAIL("Multiple errors found: " + formatErrors(errors));
6660

6761
return make_pair(&compiler().ast(""), std::move(errors));
@@ -173,12 +167,16 @@ bool AnalysisFramework::stageSuccessful(PipelineStage _stage) const
173167
unreachable();
174168
}
175169

176-
ErrorList AnalysisFramework::expectError(std::string const& _source, bool _warning, bool _allowMultiple)
170+
ErrorList AnalysisFramework::runAnalysisAndExpectError(
171+
std::string const& _source,
172+
bool _includeWarningsAndInfos,
173+
bool _allowMultiple
174+
)
177175
{
178-
auto sourceAndErrors = parseAnalyseAndReturnError(_source, _warning, true, _allowMultiple);
179-
BOOST_REQUIRE(!sourceAndErrors.second.empty());
180-
BOOST_REQUIRE_MESSAGE(!!sourceAndErrors.first, "Expected error, but no error happened.");
181-
return sourceAndErrors.second;
176+
auto [ast, errors] = runAnalysisAndExpectNoParsingErrors(_source, _includeWarningsAndInfos, true, _allowMultiple);
177+
BOOST_REQUIRE(!errors.empty());
178+
BOOST_REQUIRE_MESSAGE(ast, "Expected error, but no error happened.");
179+
return errors;
182180
}
183181

184182
std::string AnalysisFramework::formatErrors(

test/libsolidity/AnalysisFramework.h

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,37 @@ class AnalysisFramework
4949
{
5050

5151
protected:
52-
virtual std::pair<SourceUnit const*, langutil::ErrorList>
53-
parseAnalyseAndReturnError(
52+
virtual ~AnalysisFramework() = default;
53+
54+
/// Runs analysis via runFramework() and returns either an AST or a filtered list of errors.
55+
/// Uses Boost test macros to fail if errors do not occur specifically at the analysis stage.
56+
///
57+
/// @deprecated This is a legacy helper. Use runFramework() directly in new tests.
58+
///
59+
/// @param _includeWarningsAndInfos Do not remove warning and info messages from the error list.
60+
/// @param _addPreamble Apply withPreamble() to @p _source.
61+
/// @param _allowMultiple When false, use Boost test macros to fail when there's more
62+
/// than one item on the error list.
63+
std::pair<SourceUnit const*, langutil::ErrorList> runAnalysisAndExpectNoParsingErrors(
5464
std::string const& _source,
55-
bool _reportWarnings = false,
56-
bool _insertLicenseAndVersionPragma = true,
57-
bool _allowMultipleErrors = false
65+
bool _includeWarningsAndInfos = false,
66+
bool _addPreamble = true,
67+
bool _allowMultiple = false
5868
);
59-
virtual ~AnalysisFramework() = default;
6069

61-
langutil::ErrorList expectError(std::string const& _source, bool _warning = false, bool _allowMultiple = false);
70+
/// Runs analysis via runAnalysisAndExpectNoParsingErrors() and returns the list of errors.
71+
/// Uses Boost test macros to fail if there are no errors.
72+
///
73+
/// @deprecated This is a legacy helper. Use runFramework() directly in new tests.
74+
///
75+
/// @param _includeWarningsAndInfos Do not remove warning and info messages from the error list.
76+
/// @param _allowMultiple When false, use Boost test macros to fail when there's more
77+
/// than one item on the error list.
78+
langutil::ErrorList runAnalysisAndExpectError(
79+
std::string const& _source,
80+
bool _includeWarningsAndInfos = false,
81+
bool _allowMultiple = false
82+
);
6283

6384
public:
6485
/// Runs the full compiler pipeline on specified sources. This is the main function of the
@@ -145,15 +166,15 @@ class AnalysisFramework
145166
#define CHECK_ALLOW_MULTI(text, expectations) \
146167
do \
147168
{ \
148-
ErrorList errors = expectError((text), true, true); \
169+
ErrorList errors = runAnalysisAndExpectError((text), true, true); \
149170
auto message = searchErrors(errors, (expectations)); \
150171
BOOST_CHECK_MESSAGE(message.empty(), message); \
151172
} while(0)
152173

153-
#define CHECK_ERROR_OR_WARNING(text, typ, substrings, warning, allowMulti) \
174+
#define CHECK_ERROR_OR_WARNING(text, typ, substrings, includeWarningsAndInfos, allowMulti) \
154175
do \
155176
{ \
156-
ErrorList errors = expectError((text), (warning), (allowMulti)); \
177+
ErrorList errors = runAnalysisAndExpectError((text), (includeWarningsAndInfos), (allowMulti)); \
157178
std::vector<std::pair<Error::Type, std::string>> expectations; \
158179
for (auto const& str: substrings) \
159180
expectations.emplace_back((Error::Type::typ), str); \
@@ -188,14 +209,14 @@ CHECK_ERROR_OR_WARNING(text, Warning, substrings, true, true)
188209

189210
// [checkSuccess(text)] asserts that the compilation down to typechecking succeeds.
190211
#define CHECK_SUCCESS(text) do { \
191-
auto [ast, errors] = parseAnalyseAndReturnError((text)); \
212+
auto [ast, errors] = runAnalysisAndExpectNoParsingErrors((text)); \
192213
BOOST_CHECK(errors.empty()); \
193214
} while(0)
194215

195216
#define CHECK_SUCCESS_NO_WARNINGS(text) \
196217
do \
197218
{ \
198-
auto [ast, errors] = parseAnalyseAndReturnError((text), true); \
219+
auto [ast, errors] = runAnalysisAndExpectNoParsingErrors((text), true); \
199220
std::string message; \
200221
if (!errors.empty()) \
201222
message = formatErrors(errors);\

test/libsolidity/SolidityNameAndTypeResolution.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(function_no_implementation)
4747
function functionName(bytes32 input) public virtual returns (bytes32 out);
4848
}
4949
)";
50-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
50+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
5151
soltestAssert(sourceUnit);
5252
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
5353

@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(abstract_contract)
6464
abstract contract base { function foo() public virtual; }
6565
contract derived is base { function foo() public override {} }
6666
)";
67-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
67+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
6868
soltestAssert(sourceUnit);
6969
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
7070

@@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(abstract_contract_with_overload)
8585
abstract contract base { function foo(bool) public virtual; }
8686
abstract contract derived is base { function foo(uint) public {} }
8787
)";
88-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
88+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
8989
soltestAssert(sourceUnit);
9090
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
9191

@@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(implement_abstract_via_constructor)
104104
abstract contract base { function foo() public virtual; }
105105
abstract contract foo is base { constructor() {} }
106106
)";
107-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
107+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
108108
soltestAssert(sourceUnit);
109109
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
110110

@@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature)
124124
}
125125
}
126126
)";
127-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
127+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
128128
soltestAssert(sourceUnit);
129129
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
130130

@@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
145145
}
146146
}
147147
)";
148-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
148+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
149149
soltestAssert(sourceUnit);
150150
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
151151

@@ -171,7 +171,7 @@ BOOST_AUTO_TEST_CASE(function_external_types)
171171
}
172172
}
173173
)";
174-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
174+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
175175
soltestAssert(sourceUnit);
176176
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
177177

@@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(enum_external_type)
196196
}
197197
}
198198
)";
199-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
199+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
200200
soltestAssert(sourceUnit);
201201
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
202202

@@ -228,7 +228,7 @@ BOOST_AUTO_TEST_CASE(external_struct_signatures)
228228
// Ignore analysis errors. This test only checks that correct signatures
229229
// are generated for external structs, but they are not yet supported
230230
// in code generation and therefore cause an error in the TypeChecker.
231-
SourceUnit const* sourceUnit = parseAnalyseAndReturnError(text, false, true, true).first;
231+
SourceUnit const* sourceUnit = runAnalysisAndExpectNoParsingErrors(text, false, true, true).first;
232232
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
233233
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
234234
{
@@ -259,7 +259,7 @@ BOOST_AUTO_TEST_CASE(external_struct_signatures_in_libraries)
259259
// Ignore analysis errors. This test only checks that correct signatures
260260
// are generated for external structs, but calldata structs are not yet supported
261261
// in code generation and therefore cause an error in the TypeChecker.
262-
SourceUnit const* sourceUnit = parseAnalyseAndReturnError(text, false, true, true).first;
262+
SourceUnit const* sourceUnit = runAnalysisAndExpectNoParsingErrors(text, false, true, true).first;
263263
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
264264
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
265265
{
@@ -281,7 +281,7 @@ BOOST_AUTO_TEST_CASE(struct_with_mapping_in_library)
281281
function f(X storage x) external {}
282282
}
283283
)";
284-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
284+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
285285
soltestAssert(sourceUnit);
286286
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
287287

@@ -307,7 +307,7 @@ BOOST_AUTO_TEST_CASE(state_variable_accessors)
307307
}
308308
)";
309309

310-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
310+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
311311
soltestAssert(sourceUnit);
312312
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
313313

@@ -349,7 +349,7 @@ BOOST_AUTO_TEST_CASE(private_state_variable)
349349
}
350350
)";
351351

352-
auto [sourceUnit, errors] = parseAnalyseAndReturnError(text);
352+
auto [sourceUnit, errors] = runAnalysisAndExpectNoParsingErrors(text);
353353
soltestAssert(sourceUnit);
354354
soltestAssert(errors.empty(), "Unexpected error: " + formatErrors(errors));
355355

@@ -397,7 +397,7 @@ BOOST_AUTO_TEST_CASE(warn_nonpresent_pragma)
397397
// SPDX-License-Identifier: GPL-3.0
398398
contract C {}
399399
)";
400-
auto sourceAndError = parseAnalyseAndReturnError(text, true, false);
400+
auto sourceAndError = runAnalysisAndExpectNoParsingErrors(text, true, false);
401401
BOOST_REQUIRE(!sourceAndError.second.empty());
402402
BOOST_REQUIRE(!!sourceAndError.first);
403403
BOOST_CHECK(searchErrorMessage(*sourceAndError.second.front(), "Source file does not specify required compiler version!"));

0 commit comments

Comments
 (0)