Clarify FileReader interface.

This commit is contained in:
chriseth 2021-12-13 14:52:17 +01:00
parent 9131475b7c
commit 0e6388a907
4 changed files with 38 additions and 39 deletions

View File

@ -84,7 +84,7 @@ void FileReader::allowDirectory(boost::filesystem::path _path)
m_allowedDirectories.insert(std::move(_path)); m_allowedDirectories.insert(std::move(_path));
} }
void FileReader::setSource(boost::filesystem::path const& _path, SourceCode _source) void FileReader::addOrUpdateFile(boost::filesystem::path const& _path, SourceCode _source)
{ {
m_sourceCodes[cliPathToSourceUnitName(_path)] = std::move(_source); m_sourceCodes[cliPathToSourceUnitName(_path)] = std::move(_source);
} }
@ -94,7 +94,7 @@ void FileReader::setStdin(SourceCode _source)
m_sourceCodes["<stdin>"] = std::move(_source); m_sourceCodes["<stdin>"] = std::move(_source);
} }
void FileReader::setSources(StringMap _sources) void FileReader::setSourceUnits(StringMap _sources)
{ {
m_sourceCodes = std::move(_sources); m_sourceCodes = std::move(_sources);
} }

View File

@ -61,18 +61,17 @@ public:
void allowDirectory(boost::filesystem::path _path); void allowDirectory(boost::filesystem::path _path);
FileSystemPathSet const& allowedDirectories() const noexcept { return m_allowedDirectories; } FileSystemPathSet const& allowedDirectories() const noexcept { return m_allowedDirectories; }
StringMap const& sourceCodes() const noexcept { return m_sourceCodes; } /// @returns all sources by their internal source unit names.
StringMap const& sourceUnits() const noexcept { return m_sourceCodes; }
/// Retrieves the source code for a given source unit name.
SourceCode const& sourceCode(SourceUnitName const& _sourceUnitName) const { return m_sourceCodes.at(_sourceUnitName); }
/// Resets all sources to the given map of source unit name to source codes. /// Resets all sources to the given map of source unit name to source codes.
/// Does not enforce @a allowedDirectories(). /// Does not enforce @a allowedDirectories().
void setSources(StringMap _sources); void setSourceUnits(StringMap _sources);
/// Adds the source code under a source unit name created by normalizing the file path. /// Adds the source code under a source unit name created by normalizing the file path
/// or changes an existing source.
/// Does not enforce @a allowedDirectories(). /// Does not enforce @a allowedDirectories().
void setSource(boost::filesystem::path const& _path, SourceCode _source); void addOrUpdateFile(boost::filesystem::path const& _path, SourceCode _source);
/// Adds the source code under the source unit name of @a <stdin>. /// Adds the source code under the source unit name of @a <stdin>.
/// Does not enforce @a allowedDirectories(). /// Does not enforce @a allowedDirectories().
@ -83,7 +82,7 @@ public:
/// The read will only succeed if the canonical path of the file is within one of the @a allowedDirectories(). /// The read will only succeed if the canonical path of the file is within one of the @a allowedDirectories().
/// @param _kind must be equal to "source". Other values are not supported. /// @param _kind must be equal to "source". Other values are not supported.
/// @return Content of the loaded file or an error message. If the operation succeeds, a copy of /// @return Content of the loaded file or an error message. If the operation succeeds, a copy of
/// the content is retained in @a sourceCodes() under the key of @a _sourceUnitName. If the key /// the content is retained in @a sourceUnits() under the key of @a _sourceUnitName. If the key
/// already exists, previous content is discarded. /// already exists, previous content is discarded.
frontend::ReadCallback::Result readFile(std::string const& _kind, std::string const& _sourceUnitName); frontend::ReadCallback::Result readFile(std::string const& _kind, std::string const& _sourceUnitName);

View File

@ -483,7 +483,7 @@ void CommandLineInterface::readInputFiles()
} }
else else
{ {
m_fileReader.setSource(infile, move(fileContent)); m_fileReader.addOrUpdateFile(infile, move(fileContent));
m_fileReader.allowDirectory(boost::filesystem::canonical(infile).remove_filename()); m_fileReader.allowDirectory(boost::filesystem::canonical(infile).remove_filename());
} }
} }
@ -499,7 +499,7 @@ void CommandLineInterface::readInputFiles()
m_fileReader.setStdin(readUntilEnd(m_sin)); m_fileReader.setStdin(readUntilEnd(m_sin));
} }
if (m_fileReader.sourceCodes().empty() && !m_standardJsonInput.has_value()) if (m_fileReader.sourceUnits().empty() && !m_standardJsonInput.has_value())
solThrow(CommandLineValidationError, "All specified input files either do not exist or are not regular files."); solThrow(CommandLineValidationError, "All specified input files either do not exist or are not regular files.");
} }
@ -510,7 +510,7 @@ map<string, Json::Value> CommandLineInterface::parseAstFromInput()
map<string, Json::Value> sourceJsons; map<string, Json::Value> sourceJsons;
map<string, string> tmpSources; map<string, string> tmpSources;
for (SourceCode const& sourceCode: m_fileReader.sourceCodes() | ranges::views::values) for (SourceCode const& sourceCode: m_fileReader.sourceUnits() | ranges::views::values)
{ {
Json::Value ast; Json::Value ast;
astAssert(jsonParseStrict(sourceCode, ast), "Input file could not be parsed to JSON"); astAssert(jsonParseStrict(sourceCode, ast), "Input file could not be parsed to JSON");
@ -528,7 +528,7 @@ map<string, Json::Value> CommandLineInterface::parseAstFromInput()
} }
} }
m_fileReader.setSources(tmpSources); m_fileReader.setSourceUnits(tmpSources);
return sourceJsons; return sourceJsons;
} }
@ -721,7 +721,7 @@ void CommandLineInterface::compile()
} }
else else
{ {
m_compiler->setSources(m_fileReader.sourceCodes()); m_compiler->setSources(m_fileReader.sourceUnits());
m_compiler->setParserErrorRecovery(m_options.input.errorRecovery); m_compiler->setParserErrorRecovery(m_options.input.errorRecovery);
} }
@ -835,7 +835,7 @@ void CommandLineInterface::handleCombinedJSON()
if (m_options.compiler.combinedJsonRequests->ast) if (m_options.compiler.combinedJsonRequests->ast)
{ {
output[g_strSources] = Json::Value(Json::objectValue); output[g_strSources] = Json::Value(Json::objectValue);
for (auto const& sourceCode: m_fileReader.sourceCodes()) for (auto const& sourceCode: m_fileReader.sourceUnits())
{ {
ASTJsonConverter converter(m_compiler->state(), m_compiler->sourceIndices()); ASTJsonConverter converter(m_compiler->state(), m_compiler->sourceIndices());
output[g_strSources][sourceCode.first] = Json::Value(Json::objectValue); output[g_strSources][sourceCode.first] = Json::Value(Json::objectValue);
@ -858,12 +858,12 @@ void CommandLineInterface::handleAst()
return; return;
vector<ASTNode const*> asts; vector<ASTNode const*> asts;
for (auto const& sourceCode: m_fileReader.sourceCodes()) for (auto const& sourceCode: m_fileReader.sourceUnits())
asts.push_back(&m_compiler->ast(sourceCode.first)); asts.push_back(&m_compiler->ast(sourceCode.first));
if (!m_options.output.dir.empty()) if (!m_options.output.dir.empty())
{ {
for (auto const& sourceCode: m_fileReader.sourceCodes()) for (auto const& sourceCode: m_fileReader.sourceUnits())
{ {
stringstream data; stringstream data;
string postfix = ""; string postfix = "";
@ -876,7 +876,7 @@ void CommandLineInterface::handleAst()
else else
{ {
sout() << "JSON AST (compact format):" << endl << endl; sout() << "JSON AST (compact format):" << endl << endl;
for (auto const& sourceCode: m_fileReader.sourceCodes()) for (auto const& sourceCode: m_fileReader.sourceUnits())
{ {
sout() << endl << "======= " << sourceCode.first << " =======" << endl; sout() << endl << "======= " << sourceCode.first << " =======" << endl;
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(sout(), m_compiler->ast(sourceCode.first)); ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(sout(), m_compiler->ast(sourceCode.first));
@ -908,7 +908,7 @@ void CommandLineInterface::link()
librariesReplacements[replacement] = library.second; librariesReplacements[replacement] = library.second;
} }
FileReader::StringMap sourceCodes = m_fileReader.sourceCodes(); FileReader::StringMap sourceCodes = m_fileReader.sourceUnits();
for (auto& src: sourceCodes) for (auto& src: sourceCodes)
{ {
auto end = src.second.end(); auto end = src.second.end();
@ -944,14 +944,14 @@ void CommandLineInterface::link()
while (!src.second.empty() && *prev(src.second.end()) == '\n') while (!src.second.empty() && *prev(src.second.end()) == '\n')
src.second.resize(src.second.size() - 1); src.second.resize(src.second.size() - 1);
} }
m_fileReader.setSources(move(sourceCodes)); m_fileReader.setSourceUnits(move(sourceCodes));
} }
void CommandLineInterface::writeLinkedFiles() void CommandLineInterface::writeLinkedFiles()
{ {
solAssert(m_options.input.mode == InputMode::Linker, ""); solAssert(m_options.input.mode == InputMode::Linker, "");
for (auto const& src: m_fileReader.sourceCodes()) for (auto const& src: m_fileReader.sourceUnits())
if (src.first == g_stdinFileName) if (src.first == g_stdinFileName)
sout() << src.second << endl; sout() << src.second << endl;
else else
@ -989,7 +989,7 @@ void CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul:
bool successful = true; bool successful = true;
map<string, yul::AssemblyStack> assemblyStacks; map<string, yul::AssemblyStack> assemblyStacks;
for (auto const& src: m_fileReader.sourceCodes()) for (auto const& src: m_fileReader.sourceUnits())
{ {
// --no-optimize-yul option is not accepted in assembly mode. // --no-optimize-yul option is not accepted in assembly mode.
solAssert(!m_options.optimizer.noOptimizeYul, ""); solAssert(!m_options.optimizer.noOptimizeYul, "");
@ -1029,7 +1029,7 @@ void CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul:
solThrow(CommandLineExecutionError, ""); solThrow(CommandLineExecutionError, "");
} }
for (auto const& src: m_fileReader.sourceCodes()) for (auto const& src: m_fileReader.sourceUnits())
{ {
string machine = string machine =
_targetMachine == yul::AssemblyStack::Machine::EVM ? "EVM" : _targetMachine == yul::AssemblyStack::Machine::EVM ? "EVM" :
@ -1118,7 +1118,7 @@ void CommandLineInterface::outputCompilationResults()
if (m_options.compiler.outputs.asmJson) if (m_options.compiler.outputs.asmJson)
ret = jsonPrettyPrint(removeNullMembers(m_compiler->assemblyJSON(contract))); ret = jsonPrettyPrint(removeNullMembers(m_compiler->assemblyJSON(contract)));
else else
ret = m_compiler->assemblyString(contract, m_fileReader.sourceCodes()); ret = m_compiler->assemblyString(contract, m_fileReader.sourceUnits());
if (!m_options.output.dir.empty()) if (!m_options.output.dir.empty())
createFile(m_compiler->filesystemFriendlyName(contract) + (m_options.compiler.outputs.asmJson ? "_evm.json" : ".evm"), ret); createFile(m_compiler->filesystemFriendlyName(contract) + (m_options.compiler.outputs.asmJson ? "_evm.json" : ".evm"), ret);

View File

@ -213,7 +213,7 @@ BOOST_AUTO_TEST_CASE(cli_input)
BOOST_TEST(result.options.input.mode == InputMode::Compiler); BOOST_TEST(result.options.input.mode == InputMode::Compiler);
BOOST_TEST(result.options.input.addStdin); BOOST_TEST(result.options.input.addStdin);
BOOST_CHECK_EQUAL(result.options.input.remappings, expectedRemappings); BOOST_CHECK_EQUAL(result.options.input.remappings, expectedRemappings);
BOOST_CHECK_EQUAL(result.reader.sourceCodes(), expectedSources); BOOST_CHECK_EQUAL(result.reader.sourceUnits(), expectedSources);
BOOST_CHECK_EQUAL(result.reader.allowedDirectories(), expectedAllowedPaths); BOOST_CHECK_EQUAL(result.reader.allowedDirectories(), expectedAllowedPaths);
} }
@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE(cli_ignore_missing_some_files_exist)
BOOST_TEST(result.stderrContent == "\"" + (tempDir2.path() / "input2.sol").string() + "\" is not found. Skipping.\n"); BOOST_TEST(result.stderrContent == "\"" + (tempDir2.path() / "input2.sol").string() + "\" is not found. Skipping.\n");
BOOST_TEST(result.options.input.mode == InputMode::Compiler); BOOST_TEST(result.options.input.mode == InputMode::Compiler);
BOOST_TEST(!result.options.input.addStdin); BOOST_TEST(!result.options.input.addStdin);
BOOST_CHECK_EQUAL(result.reader.sourceCodes(), expectedSources); BOOST_CHECK_EQUAL(result.reader.sourceUnits(), expectedSources);
BOOST_CHECK_EQUAL(result.reader.allowedDirectories(), expectedAllowedPaths); BOOST_CHECK_EQUAL(result.reader.allowedDirectories(), expectedAllowedPaths);
} }
@ -291,7 +291,7 @@ BOOST_AUTO_TEST_CASE(standard_json_base_path)
BOOST_TEST(result.options.input.mode == InputMode::StandardJson); BOOST_TEST(result.options.input.mode == InputMode::StandardJson);
BOOST_TEST(result.options.input.addStdin); BOOST_TEST(result.options.input.addStdin);
BOOST_TEST(result.options.input.paths.empty()); BOOST_TEST(result.options.input.paths.empty());
BOOST_TEST(result.reader.sourceCodes().empty()); BOOST_TEST(result.reader.sourceUnits().empty());
BOOST_TEST(result.reader.allowedDirectories().empty()); BOOST_TEST(result.reader.allowedDirectories().empty());
BOOST_TEST(result.reader.basePath() == "/" / tempDir.path().relative_path()); BOOST_TEST(result.reader.basePath() == "/" / tempDir.path().relative_path());
} }
@ -304,7 +304,7 @@ BOOST_AUTO_TEST_CASE(standard_json_no_input_file)
BOOST_TEST(result.options.input.mode == InputMode::StandardJson); BOOST_TEST(result.options.input.mode == InputMode::StandardJson);
BOOST_TEST(result.options.input.addStdin); BOOST_TEST(result.options.input.addStdin);
BOOST_TEST(result.options.input.paths.empty()); BOOST_TEST(result.options.input.paths.empty());
BOOST_TEST(result.reader.sourceCodes().empty()); BOOST_TEST(result.reader.sourceUnits().empty());
BOOST_TEST(result.reader.allowedDirectories().empty()); BOOST_TEST(result.reader.allowedDirectories().empty());
} }
@ -315,7 +315,7 @@ BOOST_AUTO_TEST_CASE(standard_json_dash)
BOOST_TEST(result.stderrContent == ""); BOOST_TEST(result.stderrContent == "");
BOOST_TEST(result.options.input.mode == InputMode::StandardJson); BOOST_TEST(result.options.input.mode == InputMode::StandardJson);
BOOST_TEST(result.options.input.addStdin); BOOST_TEST(result.options.input.addStdin);
BOOST_TEST(result.reader.sourceCodes().empty()); BOOST_TEST(result.reader.sourceUnits().empty());
BOOST_TEST(result.reader.allowedDirectories().empty()); BOOST_TEST(result.reader.allowedDirectories().empty());
} }
@ -446,7 +446,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_no_base_path)
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == ""); BOOST_TEST(result.reader.basePath() == "");
} }
@ -508,7 +508,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_base_path_same_as_work_dir)
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedWorkDir); BOOST_TEST(result.reader.basePath() == expectedWorkDir);
} }
@ -581,7 +581,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_base_path_different_from_wor
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedBaseDir); BOOST_TEST(result.reader.basePath() == expectedBaseDir);
} }
@ -650,7 +650,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_relative_base_path)
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base"); BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base");
} }
@ -817,7 +817,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_normalization_and_weird_name
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedOptions.input.basePath); BOOST_TEST(result.reader.basePath() == expectedOptions.input.basePath);
} }
@ -874,7 +874,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_symlinks)
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedWorkDir / "sym/z/"); BOOST_TEST(result.reader.basePath() == expectedWorkDir / "sym/z/");
} }
@ -906,7 +906,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_base_path_and_stdin)
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base"); BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base");
} }
@ -1009,7 +1009,7 @@ BOOST_AUTO_TEST_CASE(cli_include_paths)
BOOST_TEST(result.stdoutContent == ""); BOOST_TEST(result.stdoutContent == "");
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.includePaths() == expectedIncludePaths); BOOST_TEST(result.reader.includePaths() == expectedIncludePaths);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base/"); BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base/");
@ -1105,7 +1105,7 @@ BOOST_AUTO_TEST_CASE(standard_json_include_paths)
BOOST_REQUIRE(result.success); BOOST_REQUIRE(result.success);
BOOST_TEST(result.options == expectedOptions); BOOST_TEST(result.options == expectedOptions);
BOOST_TEST(result.reader.sourceCodes() == expectedSources); BOOST_TEST(result.reader.sourceUnits() == expectedSources);
BOOST_TEST(result.reader.includePaths() == expectedIncludePaths); BOOST_TEST(result.reader.includePaths() == expectedIncludePaths);
BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories); BOOST_TEST(result.reader.allowedDirectories() == expectedAllowedDirectories);
BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base/"); BOOST_TEST(result.reader.basePath() == expectedWorkDir / "base/");