1 /*
2 This file is part of solidity.
3
4 solidity is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 solidity is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with solidity. If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18
19 #include <test/libsolidity/SyntaxTest.h>
20
21 #include <test/Common.h>
22 #include <boost/algorithm/string.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/test/unit_test.hpp>
25 #include <boost/throw_exception.hpp>
26 #include <fstream>
27 #include <memory>
28 #include <stdexcept>
29
30 using namespace std;
31 using namespace solidity;
32 using namespace solidity::util;
33 using namespace solidity::util::formatting;
34 using namespace solidity::langutil;
35 using namespace solidity::frontend;
36 using namespace solidity::frontend::test;
37 using namespace boost::unit_test;
38 namespace fs = boost::filesystem;
39
SyntaxTest(string const & _filename,langutil::EVMVersion _evmVersion,bool _parserErrorRecovery)40 SyntaxTest::SyntaxTest(string const& _filename, langutil::EVMVersion _evmVersion, bool _parserErrorRecovery): CommonSyntaxTest(_filename, _evmVersion)
41 {
42 m_optimiseYul = m_reader.boolSetting("optimize-yul", true);
43 m_parserErrorRecovery = _parserErrorRecovery;
44 }
45
run(ostream & _stream,string const & _linePrefix,bool _formatted)46 TestCase::TestResult SyntaxTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
47 {
48 setupCompiler();
49 parseAndAnalyze();
50 filterObtainedErrors();
51
52 return conclude(_stream, _linePrefix, _formatted);
53 }
54
addPreamble(string const & _sourceCode)55 string SyntaxTest::addPreamble(string const& _sourceCode)
56 {
57 // Silence compiler version warning
58 string preamble = "pragma solidity >=0.0;\n";
59 // NOTE: this check is intentionally loose to match weird cases.
60 // We can manually adjust a test case where this causes problem.
61 if (_sourceCode.find("SPDX-License-Identifier:") == string::npos)
62 preamble += "// SPDX-License-Identifier: GPL-3.0\n";
63 return preamble + _sourceCode;
64 }
65
setupCompiler()66 void SyntaxTest::setupCompiler()
67 {
68 compiler().reset();
69 auto sourcesWithPragma = m_sources.sources;
70 for (auto& source: sourcesWithPragma)
71 source.second = addPreamble(source.second);
72 compiler().setSources(sourcesWithPragma);
73 compiler().setEVMVersion(m_evmVersion);
74 compiler().setParserErrorRecovery(m_parserErrorRecovery);
75 compiler().setOptimiserSettings(
76 m_optimiseYul ?
77 OptimiserSettings::full() :
78 OptimiserSettings::minimal()
79 );
80 compiler().setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
81 compiler().setMetadataHash(CompilerStack::MetadataHash::None);
82 }
83
parseAndAnalyze()84 void SyntaxTest::parseAndAnalyze()
85 {
86 if (compiler().parse() && compiler().analyze())
87 try
88 {
89 if (!compiler().compile())
90 {
91 ErrorList const& errors = compiler().errors();
92 auto codeGeneretionErrorCount = count_if(errors.cbegin(), errors.cend(), [](auto const& error) {
93 return error->type() == Error::Type::CodeGenerationError;
94 });
95 auto errorCount = count_if(errors.cbegin(), errors.cend(), [](auto const& error) {
96 return Error::isError(error->type());
97 });
98 // failing compilation after successful analysis is a rare case,
99 // it assumes that errors contain exactly one error, and the error is of type Error::Type::CodeGenerationError
100 if (codeGeneretionErrorCount != 1 || errorCount != 1)
101 BOOST_THROW_EXCEPTION(runtime_error("Compilation failed even though analysis was successful."));
102 }
103 }
104 catch (UnimplementedFeatureError const& _e)
105 {
106 m_errorList.emplace_back(SyntaxTestError{
107 "UnimplementedFeatureError",
108 nullopt,
109 errorMessage(_e),
110 "",
111 -1,
112 -1
113 });
114 }
115 }
116
filterObtainedErrors()117 void SyntaxTest::filterObtainedErrors()
118 {
119 for (auto const& currentError: filterErrors(compiler().errors(), true))
120 {
121 int locationStart = -1;
122 int locationEnd = -1;
123 string sourceName;
124 if (SourceLocation const* location = currentError->sourceLocation())
125 {
126 solAssert(location->sourceName, "");
127 sourceName = *location->sourceName;
128 solAssert(m_sources.sources.count(sourceName) == 1, "");
129
130 int preambleSize =
131 static_cast<int>(compiler().charStream(sourceName).size()) -
132 static_cast<int>(m_sources.sources[sourceName].size());
133 solAssert(preambleSize >= 0, "");
134
135 // ignore the version & license pragma inserted by the testing tool when calculating locations.
136 if (location->start != -1)
137 {
138 solAssert(location->start >= preambleSize, "");
139 locationStart = location->start - preambleSize;
140 }
141 if (location->end != -1)
142 {
143 solAssert(location->end >= preambleSize, "");
144 locationEnd = location->end - preambleSize;
145 }
146 }
147 m_errorList.emplace_back(SyntaxTestError{
148 currentError->typeName(),
149 currentError->errorId(),
150 errorMessage(*currentError),
151 sourceName,
152 locationStart,
153 locationEnd
154 });
155 }
156 }
157
158