2017-08-28 18:40:38 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-08-28 18:40:38 +00:00
|
|
|
/**
|
|
|
|
* Framework for testing features from the analysis phase of compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <test/libsolidity/ErrorCheck.h>
|
|
|
|
|
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
|
|
|
class Type;
|
|
|
|
class FunctionType;
|
2019-04-15 13:33:39 +00:00
|
|
|
using FunctionTypePointer = FunctionType const*;
|
2019-12-23 15:50:30 +00:00
|
|
|
}
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
enum class PipelineStage {
|
|
|
|
Parsing,
|
|
|
|
Analysis,
|
|
|
|
Compilation,
|
|
|
|
};
|
|
|
|
|
2017-08-28 18:40:38 +00:00
|
|
|
class AnalysisFramework
|
|
|
|
{
|
|
|
|
|
|
|
|
protected:
|
2018-11-14 16:11:55 +00:00
|
|
|
virtual std::pair<SourceUnit const*, langutil::ErrorList>
|
2017-08-28 18:40:38 +00:00
|
|
|
parseAnalyseAndReturnError(
|
|
|
|
std::string const& _source,
|
|
|
|
bool _reportWarnings = false,
|
2020-05-12 09:56:28 +00:00
|
|
|
bool _insertLicenseAndVersionPragma = true,
|
2023-08-04 20:54:48 +00:00
|
|
|
bool _allowMultipleErrors = false
|
2017-08-28 18:40:38 +00:00
|
|
|
);
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~AnalysisFramework() = default;
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
langutil::ErrorList expectError(std::string const& _source, bool _warning = false, bool _allowMultiple = false);
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
public:
|
|
|
|
/// Runs the full compiler pipeline on specified sources. This is the main function of the
|
|
|
|
/// framework. Resets the stack, configures it and runs either until the first failed stage or
|
|
|
|
/// until the @p _targetStage is reached.
|
|
|
|
/// Afterwards the caller can inspect the stack via @p compiler(). The framework provides a few
|
|
|
|
/// convenience helpers to check the state and error list, in general the caller can freely
|
|
|
|
/// access the stack, including generating outputs if the compilation succeeded.
|
|
|
|
bool runFramework(StringMap _sources, PipelineStage _targetStage = PipelineStage::Compilation);
|
|
|
|
bool runFramework(std::string _source, PipelineStage _targetStage = PipelineStage::Compilation)
|
|
|
|
{
|
|
|
|
return runFramework({{"", std::move(_source)}}, _targetStage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetFramework();
|
|
|
|
|
|
|
|
PipelineStage targetStage() const { return m_targetStage; }
|
|
|
|
bool pipelineSuccessful() const { return stageSuccessful(m_targetStage); }
|
|
|
|
bool stageSuccessful(PipelineStage _stage) const;
|
|
|
|
|
2023-08-04 11:32:32 +00:00
|
|
|
std::string formatErrors(
|
2023-08-08 15:19:43 +00:00
|
|
|
langutil::ErrorList const& _errors,
|
2023-08-04 11:32:32 +00:00
|
|
|
bool _colored = false,
|
|
|
|
bool _withErrorIds = false
|
|
|
|
) const;
|
|
|
|
std::string formatError(
|
|
|
|
langutil::Error const& _error,
|
|
|
|
bool _colored = false,
|
|
|
|
bool _withErrorIds = false
|
|
|
|
) const;
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2017-08-30 21:44:46 +00:00
|
|
|
static ContractDefinition const* retrieveContractByName(SourceUnit const& _source, std::string const& _name);
|
|
|
|
static FunctionTypePointer retrieveFunctionBySignature(
|
2017-08-28 18:40:38 +00:00
|
|
|
ContractDefinition const& _contract,
|
|
|
|
std::string const& _signature
|
|
|
|
);
|
|
|
|
|
2023-08-04 18:37:21 +00:00
|
|
|
/// filter out the warnings in m_warningsToFilter or all warnings and infos if _includeWarningsAndInfos is false
|
|
|
|
langutil::ErrorList filterErrors(langutil::ErrorList const& _errorList, bool _includeWarningsAndInfos = true) const;
|
2023-08-04 18:38:15 +00:00
|
|
|
langutil::ErrorList filteredErrors(bool _includeWarningsAndInfos = true) const
|
|
|
|
{
|
|
|
|
return filterErrors(compiler().errors(), _includeWarningsAndInfos);
|
|
|
|
}
|
2018-04-03 10:05:26 +00:00
|
|
|
|
2022-12-25 21:39:50 +00:00
|
|
|
/// @returns reference to lazy-instantiated CompilerStack.
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::CompilerStack& compiler()
|
2019-04-15 13:27:51 +00:00
|
|
|
{
|
|
|
|
if (!m_compiler)
|
2023-08-04 14:25:33 +00:00
|
|
|
m_compiler = createStack();
|
2019-04-15 13:27:51 +00:00
|
|
|
return *m_compiler;
|
|
|
|
}
|
|
|
|
|
2022-12-25 21:39:50 +00:00
|
|
|
/// @returns reference to lazy-instantiated CompilerStack.
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::CompilerStack const& compiler() const
|
2019-04-15 13:27:51 +00:00
|
|
|
{
|
|
|
|
if (!m_compiler)
|
2023-08-04 14:25:33 +00:00
|
|
|
m_compiler = createStack();
|
2019-04-15 13:27:51 +00:00
|
|
|
return *m_compiler;
|
|
|
|
}
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
protected:
|
2023-08-04 14:25:33 +00:00
|
|
|
/// Creates a new instance of @p CompilerStack. Override if your test case needs to pass in
|
|
|
|
/// custom constructor arguments.
|
|
|
|
virtual std::unique_ptr<CompilerStack> createStack() const;
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
/// Configures @p CompilerStack. The default implementation sets basic parameters based on
|
|
|
|
/// CLI options. Override if your test case needs extra configuration.
|
|
|
|
virtual void setupCompiler(CompilerStack& _compiler);
|
|
|
|
|
|
|
|
/// Executes the requested pipeline stages until @p m_targetStage is reached.
|
|
|
|
/// Stops at the first failed stage.
|
|
|
|
void executeCompilationPipeline();
|
|
|
|
|
|
|
|
std::vector<std::string> m_warningsToFilter = {"This is a pre-release compiler version"};
|
|
|
|
std::vector<std::string> m_messagesToCut = {"Source file requires different compiler version (current compiler is"};
|
|
|
|
|
2019-04-15 13:27:51 +00:00
|
|
|
private:
|
2019-12-23 15:50:30 +00:00
|
|
|
mutable std::unique_ptr<solidity::frontend::CompilerStack> m_compiler;
|
2023-08-07 11:00:08 +00:00
|
|
|
PipelineStage m_targetStage = PipelineStage::Compilation;
|
2017-08-28 18:40:38 +00:00
|
|
|
};
|
|
|
|
|
2018-02-09 22:54:05 +00:00
|
|
|
// Asserts that the compilation down to typechecking
|
|
|
|
// emits multiple errors of different types and messages, provided in the second argument.
|
|
|
|
#define CHECK_ALLOW_MULTI(text, expectations) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
ErrorList errors = expectError((text), true, true); \
|
|
|
|
auto message = searchErrors(errors, (expectations)); \
|
|
|
|
BOOST_CHECK_MESSAGE(message.empty(), message); \
|
|
|
|
} while(0)
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2018-02-09 22:54:05 +00:00
|
|
|
#define CHECK_ERROR_OR_WARNING(text, typ, substrings, warning, allowMulti) \
|
2017-08-28 18:40:38 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2018-02-09 22:54:05 +00:00
|
|
|
ErrorList errors = expectError((text), (warning), (allowMulti)); \
|
|
|
|
std::vector<std::pair<Error::Type, std::string>> expectations; \
|
|
|
|
for (auto const& str: substrings) \
|
|
|
|
expectations.emplace_back((Error::Type::typ), str); \
|
|
|
|
auto message = searchErrors(errors, expectations); \
|
|
|
|
BOOST_CHECK_MESSAGE(message.empty(), message); \
|
2017-08-28 18:40:38 +00:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
// [checkError(text, type, substring)] asserts that the compilation down to typechecking
|
|
|
|
// emits an error of type [type] and with a message containing [substring].
|
|
|
|
#define CHECK_ERROR(text, type, substring) \
|
2018-02-09 22:54:05 +00:00
|
|
|
CHECK_ERROR_OR_WARNING(text, type, std::vector<std::string>{(substring)}, false, false)
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
// [checkError(text, type, substring)] asserts that the compilation down to typechecking
|
2018-02-09 22:54:05 +00:00
|
|
|
// emits multiple errors of the same type [type] and with a messages containing [substrings].
|
|
|
|
// Because of the limitations of the preprocessor, you cannot use {{T1, "abc"}, {T2, "def"}} as arguments,
|
|
|
|
// but have to replace them by (std::vector<std::pair<Error::Type, std::string>>{"abc", "def"})
|
|
|
|
// (note the parentheses)
|
|
|
|
#define CHECK_ERROR_ALLOW_MULTI(text, type, substrings) \
|
|
|
|
CHECK_ERROR_OR_WARNING(text, type, substrings, false, true)
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
// [checkWarning(text, substring)] asserts that the compilation down to typechecking
|
|
|
|
// emits a warning and with a message containing [substring].
|
|
|
|
#define CHECK_WARNING(text, substring) \
|
2018-02-09 22:54:05 +00:00
|
|
|
CHECK_ERROR_OR_WARNING(text, Warning, std::vector<std::string>{(substring)}, true, false)
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
// [checkWarningAllowMulti(text, substring)] aserts that the compilation down to typechecking
|
|
|
|
// emits a warning and with a message containing [substring].
|
2018-02-09 22:54:05 +00:00
|
|
|
// Because of the limitations of the preprocessor, you cannot use {"abc", "def"} as arguments,
|
|
|
|
// but have to replace them by (std::vector<std::string>{"abc", "def"}) (note the parentheses)
|
|
|
|
#define CHECK_WARNING_ALLOW_MULTI(text, substrings) \
|
|
|
|
CHECK_ERROR_OR_WARNING(text, Warning, substrings, true, true)
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
// [checkSuccess(text)] asserts that the compilation down to typechecking succeeds.
|
2023-08-04 17:41:49 +00:00
|
|
|
#define CHECK_SUCCESS(text) do { \
|
|
|
|
auto [ast, errors] = parseAnalyseAndReturnError((text)); \
|
|
|
|
BOOST_CHECK(errors.empty()); \
|
|
|
|
} while(0)
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
#define CHECK_SUCCESS_NO_WARNINGS(text) \
|
|
|
|
do \
|
|
|
|
{ \
|
2023-08-04 17:41:49 +00:00
|
|
|
auto [ast, errors] = parseAnalyseAndReturnError((text), true); \
|
2017-09-28 08:59:15 +00:00
|
|
|
std::string message; \
|
2023-08-04 17:41:49 +00:00
|
|
|
if (!errors.empty()) \
|
|
|
|
message = formatErrors(errors);\
|
|
|
|
BOOST_CHECK_MESSAGE(errors.empty(), message); \
|
2017-08-28 18:40:38 +00:00
|
|
|
} \
|
|
|
|
while(0)
|
|
|
|
|
|
|
|
}
|