-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Expand file tree
/
Copy pathSyntaxTest.cpp
More file actions
164 lines (145 loc) · 5.42 KB
/
SyntaxTest.cpp
File metadata and controls
164 lines (145 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <test/libsolidity/SyntaxTest.h>
#include <test/libsolidity/util/Common.h>
#include <test/Common.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp>
#include <range/v3/algorithm/find_if.hpp>
#include <fstream>
#include <memory>
#include <stdexcept>
using namespace solidity;
using namespace solidity::util;
using namespace solidity::util::formatting;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace boost::unit_test;
namespace fs = boost::filesystem;
SyntaxTest::SyntaxTest(
std::string const& _filename,
langutil::EVMVersion _evmVersion,
Error::Severity _minSeverity
):
CommonSyntaxTest(_filename, _evmVersion),
m_minSeverity(_minSeverity)
{
static std::set<std::string> const compileViaYulAllowedValues{"true", "false"};
auto const eofEnabled = solidity::test::CommonOptions::get().eofVersion().has_value();
m_compileViaYul = m_reader.stringSetting("compileViaYul", eofEnabled ? "true" : "false");
if (!compileViaYulAllowedValues.contains(m_compileViaYul))
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid compileViaYul value: " + m_compileViaYul + "."));
if (m_compileViaYul == "false" && eofEnabled)
m_shouldRun = false;
m_optimiseYul = m_reader.boolSetting("optimize-yul", true);
m_experimental = m_reader.boolSetting("experimental", false);
static std::map<std::string, PipelineStage> const pipelineStages = {
{"parsing", PipelineStage::Parsing},
{"analysis", PipelineStage::Analysis},
{"compilation", PipelineStage::Compilation}
};
std::string stopAfter = m_reader.stringSetting("stopAfter", "compilation");
if (!pipelineStages.count(stopAfter))
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid stopAfter value: " + stopAfter + "."));
m_stopAfter = pipelineStages.at(stopAfter);
}
void SyntaxTest::setupCompiler(CompilerStack& _compiler)
{
AnalysisFramework::setupCompiler(_compiler);
_compiler.setEVMVersion(m_evmVersion);
_compiler.setOptimiserSettings(
m_optimiseYul ?
OptimiserSettings::full() :
OptimiserSettings::minimal()
);
_compiler.setViaIR(m_compileViaYul == "true");
_compiler.setExperimental(m_experimental);
_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
_compiler.setMetadataHash(MetadataHash::None);
}
void SyntaxTest::parseAndAnalyze()
{
runFramework(withPreamble(m_sources.sources), m_stopAfter);
if (!pipelineSuccessful() && stageSuccessful(PipelineStage::Analysis) && !compiler().isExperimentalAnalysis())
{
ErrorList const& errors = compiler().errors();
static auto isInternalError = [](std::shared_ptr<Error const> const& _error) {
return
Error::isError(_error->type()) &&
_error->type() != Error::Type::CodeGenerationError &&
_error->type() != Error::Type::UnimplementedFeatureError
;
};
// Most errors are detected during analysis, and should not happen during code generation.
// There are some exceptions, e.g. unimplemented features or stack too deep, but anything else at this stage
// is an internal error that signals a bug in the compiler (rather than in user's code).
if (
auto error = ranges::find_if(errors, isInternalError);
error != ranges::end(errors)
)
BOOST_THROW_EXCEPTION(std::runtime_error(
"Unexpected " + Error::formatErrorType((*error)->type()) + " at compilation stage."
" This error should NOT be encoded as expectation and should be fixed instead."
));
}
filterObtainedErrors();
}
void SyntaxTest::filterObtainedErrors()
{
for (auto const& currentError: filteredErrors())
{
if (currentError->severity() < m_minSeverity)
continue;
int locationStart = -1;
int locationEnd = -1;
std::string sourceName;
if (SourceLocation const* location = currentError->sourceLocation())
{
locationStart = location->start;
locationEnd = location->end;
solAssert(location->sourceName, "");
sourceName = *location->sourceName;
if(m_sources.sources.count(sourceName) == 1)
{
int preambleSize =
static_cast<int>(compiler().charStream(sourceName).size()) -
static_cast<int>(m_sources.sources[sourceName].size());
solAssert(preambleSize >= 0, "");
// ignore the version & license pragma inserted by the testing tool when calculating locations.
if (location->start != -1)
{
solAssert(location->start >= preambleSize, "");
locationStart = location->start - preambleSize;
}
if (location->end != -1)
{
solAssert(location->end >= preambleSize, "");
locationEnd = location->end - preambleSize;
}
}
}
m_errorList.emplace_back(SyntaxTestError{
currentError->type(),
currentError->errorId(),
errorMessage(*currentError),
sourceName,
locationStart,
locationEnd
});
}
}