CommandLineInterface: Replace code that prints to serr() and returns false with CommandLineValidationError exception

This commit is contained in:
neel iyer 2021-08-15 11:14:38 +10:00 committed by Kamil Śliwak
parent 16f62ed43e
commit e829bcd933

View File

@ -420,16 +420,10 @@ bool CommandLineInterface::readInputFiles()
if (m_fileReader.basePath() != "") if (m_fileReader.basePath() != "")
{ {
if (!boost::filesystem::exists(m_fileReader.basePath())) if (!boost::filesystem::exists(m_fileReader.basePath()))
{ solThrow(CommandLineValidationError, "Base path does not exist: \"" + m_fileReader.basePath().string() + '"');
serr() << "Base path does not exist: " << m_fileReader.basePath() << endl;
return false;
}
if (!boost::filesystem::is_directory(m_fileReader.basePath())) if (!boost::filesystem::is_directory(m_fileReader.basePath()))
{ solThrow(CommandLineValidationError, "Base path is not a directory: \"" + m_fileReader.basePath().string() + '"');
serr() << "Base path is not a directory: " << m_fileReader.basePath() << endl;
return false;
}
} }
for (boost::filesystem::path const& includePath: m_options.input.includePaths) for (boost::filesystem::path const& includePath: m_options.input.includePaths)
@ -444,16 +438,18 @@ bool CommandLineInterface::readInputFiles()
{ {
auto pathToQuotedString = [](boost::filesystem::path const& _path){ return "\"" + _path.string() + "\""; }; auto pathToQuotedString = [](boost::filesystem::path const& _path){ return "\"" + _path.string() + "\""; };
serr() << "Source unit name collision detected. "; string message =
serr() << "The specified values of base path and/or include paths would result in multiple "; "Source unit name collision detected. "
serr() << "input files being assigned the same source unit name:" << endl; "The specified values of base path and/or include paths would result in multiple "
"input files being assigned the same source unit name:\n";
for (auto const& [sourceUnitName, normalizedInputPaths]: collisions) for (auto const& [sourceUnitName, normalizedInputPaths]: collisions)
{ {
serr() << sourceUnitName << " matches: "; message += sourceUnitName + " matches: ";
serr() << joinHumanReadable(normalizedInputPaths | ranges::views::transform(pathToQuotedString)) << endl; message += joinHumanReadable(normalizedInputPaths | ranges::views::transform(pathToQuotedString)) + "\n";
} }
return false; solThrow(CommandLineValidationError, message);
} }
for (boost::filesystem::path const& infile: m_options.input.paths) for (boost::filesystem::path const& infile: m_options.input.paths)
@ -461,10 +457,7 @@ bool CommandLineInterface::readInputFiles()
if (!boost::filesystem::exists(infile)) if (!boost::filesystem::exists(infile))
{ {
if (!m_options.input.ignoreMissingFiles) if (!m_options.input.ignoreMissingFiles)
{ solThrow(CommandLineValidationError, '"' + infile.string() + "\" is not found.");
serr() << infile << " is not found." << endl;
return false;
}
else else
serr() << infile << " is not found. Skipping." << endl; serr() << infile << " is not found. Skipping." << endl;
@ -474,10 +467,7 @@ bool CommandLineInterface::readInputFiles()
if (!boost::filesystem::is_regular_file(infile)) if (!boost::filesystem::is_regular_file(infile))
{ {
if (!m_options.input.ignoreMissingFiles) if (!m_options.input.ignoreMissingFiles)
{ solThrow(CommandLineValidationError, '"' + infile.string() + "\" is not a valid file.");
serr() << infile << " is not a valid file." << endl;
return false;
}
else else
serr() << infile << " is not a valid file. Skipping." << endl; serr() << infile << " is not a valid file. Skipping." << endl;
@ -510,10 +500,7 @@ bool CommandLineInterface::readInputFiles()
} }
if (m_fileReader.sourceCodes().empty() && !m_standardJsonInput.has_value()) if (m_fileReader.sourceCodes().empty() && !m_standardJsonInput.has_value())
{ solThrow(CommandLineValidationError, "All specified input files either do not exist or are not regular files.");
serr() << "All specified input files either do not exist or are not regular files." << endl;
return false;
}
return true; return true;
} }
@ -647,7 +634,8 @@ bool CommandLineInterface::processInput()
outputCompilationResults(); outputCompilationResults();
} }
return !m_outputFailed; if (m_outputFailed)
solThrow(CommandLineOutputError, "Failed to write all output files to disk.");
} }
void CommandLineInterface::printVersion() void CommandLineInterface::printVersion()
@ -726,8 +714,7 @@ bool CommandLineInterface::compile()
} }
catch (Exception const& _exc) catch (Exception const& _exc)
{ {
serr() << string("Failed to import AST: ") << _exc.what() << endl; solThrow(CommandLineExecutionError, "Failed to import AST: "s + _exc.what());
return false;
} }
} }
else else
@ -744,26 +731,28 @@ bool CommandLineInterface::compile()
formatter.printErrorInformation(*error); formatter.printErrorInformation(*error);
} }
if (!successful) if (!successful && !m_options.input.errorRecovery)
return m_options.input.errorRecovery; solThrow(CommandLineExecutionError, "");
} }
catch (CompilerError const& _exception) catch (CompilerError const& _exception)
{ {
m_hasOutput = true; m_hasOutput = true;
formatter.printExceptionInformation(_exception, "Compiler error"); formatter.printExceptionInformation(_exception, "Compiler error");
return false; solThrow(CommandLineExecutionError, "");
} }
catch (Error const& _error) catch (Error const& _error)
{ {
if (_error.type() == Error::Type::DocstringParsingError) if (_error.type() == Error::Type::DocstringParsingError)
serr() << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl; {
serr() << *boost::get_error_info<errinfo_comment>(_error);
solThrow(CommandLineExecutionError, "Documentation parsing failed.");
}
else else
{ {
m_hasOutput = true; m_hasOutput = true;
formatter.printExceptionInformation(_error, _error.typeName()); formatter.printExceptionInformation(_error, _error.typeName());
solThrow(CommandLineExecutionError, "");
} }
return false;
} }
return true; return true;
@ -933,11 +922,11 @@ bool CommandLineInterface::link()
*(it + placeholderSize - 2) != '_' || *(it + placeholderSize - 2) != '_' ||
*(it + placeholderSize - 1) != '_' *(it + placeholderSize - 1) != '_'
) )
{ solThrow(
serr() << "Error in binary object file " << src.first << " at position " << (it - src.second.begin()) << endl; CommandLineExecutionError,
serr() << '"' << string(it, it + min(placeholderSize, static_cast<int>(end - it))) << "\" is not a valid link reference." << endl; "Error in binary object file " + src.first + " at position " + to_string(it - src.second.begin()) + "\n" +
return false; '"' + string(it, it + min(placeholderSize, static_cast<int>(end - it))) + "\" is not a valid link reference."
} );
string foundPlaceholder(it, it + placeholderSize); string foundPlaceholder(it, it + placeholderSize);
if (librariesReplacements.count(foundPlaceholder)) if (librariesReplacements.count(foundPlaceholder))
@ -973,9 +962,8 @@ void CommandLineInterface::writeLinkedFiles()
outFile << src.second; outFile << src.second;
if (!outFile) if (!outFile)
{ {
serr() << "Could not write to file " << src.first << ". Aborting." << endl;
m_outputFailed = true; m_outputFailed = true;
return; solThrow(CommandLineOutputError, "Could not write to file " + src.first + ". Aborting.");
} }
} }
sout() << "Linking completed." << endl; sout() << "Linking completed." << endl;
@ -1041,7 +1029,10 @@ bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul:
} }
if (!successful) if (!successful)
return false; {
solAssert(m_hasOutput);
solThrow(CommandLineExecutionError, "");
}
for (auto const& src: m_fileReader.sourceCodes()) for (auto const& src: m_fileReader.sourceCodes())
{ {