Skip to content

Commit

Permalink
Merge pull request #15203 from ethereum/replaceErrorsEmptyWithHasErrors
Browse files Browse the repository at this point in the history
Replace empty check of error, warning and info list with check for errors only
  • Loading branch information
cameel authored Sep 9, 2024
2 parents 40758c3 + 13fe5ba commit ac81d42
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 21 deletions.
6 changes: 6 additions & 0 deletions liblangutil/ErrorReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ class ErrorReporter
return m_errorCount > 0;
}

/// @returns true if there is any error, warning or info.
bool hasErrorsWarningsOrInfos() const
{
return m_errorCount + m_warningCount + m_infoCount > 0;
}

/// @returns the number of errors (ignores warnings and infos).
unsigned errorCount() const
{
Expand Down
5 changes: 5 additions & 0 deletions liblangutil/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ class Error: virtual public util::Exception
return false;
}

static bool hasErrorsWarningsOrInfos(ErrorList const& _list)
{
return !_list.empty();
}

static std::string formatErrorSeverity(Severity _severity)
{
switch (_severity)
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/analysis/DeclarationTypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ void DeclarationTypeChecker::endVisit(ArrayTypeName const& _typeName)
Type const* baseType = _typeName.baseType().annotation().type;
if (!baseType)
{
solAssert(!m_errorReporter.errors().empty(), "");
solAssert(m_errorReporter.hasErrors(), "");
return;
}

Expand Down
6 changes: 3 additions & 3 deletions libsolidity/codegen/CompilerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ void CompilerContext::appendInlineAssembly(
dialect,
identifierAccess.resolve
).analyze(parserResult->root());
if (!parserResult || !errorReporter.errors().empty() || !analyzerResult)
if (!parserResult || errorReporter.hasErrorsWarningsOrInfos() || !analyzerResult)
reportError("Invalid assembly generated by code generator.");
std::shared_ptr<yul::AST const> toBeAssembledAST = parserResult;

Expand Down Expand Up @@ -513,10 +513,10 @@ void CompilerContext::appendInlineAssembly(
m_generatedYulUtilityCode = _assembly;
}

if (!errorReporter.errors().empty())
if (errorReporter.hasErrorsWarningsOrInfos())
reportError("Failed to analyze inline assembly block.");

solAssert(errorReporter.errors().empty(), "Failed to analyze inline assembly block.");
solAssert(!errorReporter.hasErrorsWarningsOrInfos(), "Failed to analyze inline assembly block.");
yul::CodeGenerator::assemble(
toBeAssembledAST->root(),
analysisInfo,
Expand Down
9 changes: 1 addition & 8 deletions libsolidity/interface/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,14 +1621,7 @@ Json StandardCompiler::compileYul(InputsAndSettings _inputsAndSettings)

// Inconsistent state - stop here to receive error reports from users
if (!stack.parseAndAnalyze(sourceName, sourceContents) && !stack.hasErrors())
{
output["errors"].emplace_back(formatError(
Error::Type::InternalCompilerError,
"general",
"No error reported, but compilation failed."
));
return output;
}
solAssert(false, "No error reported, but parsing/analysis failed.");

for (auto const& error: stack.errors())
{
Expand Down
2 changes: 1 addition & 1 deletion libyul/YulStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool YulStack::parse(std::string const& _sourceName, std::string const& _source)
reportUnimplementedFeatureError(_error);
}

if (m_errorReporter.errors().empty())
if (!m_errorReporter.hasErrors())
m_stackState = Parsed;

return m_stackState == Parsed;
Expand Down
4 changes: 2 additions & 2 deletions test/libsolidity/InlineAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void parsePrintCompare(std::string const& _source, bool _canWarn = false)
if (_canWarn)
BOOST_REQUIRE(!Error::containsErrors(stack.errors()));
else
BOOST_REQUIRE(stack.errors().empty());
BOOST_REQUIRE(!Error::hasErrorsWarningsOrInfos(stack.errors()));
std::string expectation = "object \"object\" {\n code " + boost::replace_all_copy(_source, "\n", "\n ") + "\n}\n";
BOOST_CHECK_EQUAL(stack.print(), expectation);
}
Expand Down Expand Up @@ -219,7 +219,7 @@ BOOST_AUTO_TEST_CASE(print_string_literal_unicode)
DebugInfoSelection::None()
);
BOOST_REQUIRE(stack.parseAndAnalyze("", source));
BOOST_REQUIRE(stack.errors().empty());
BOOST_REQUIRE(!Error::hasErrorsWarningsOrInfos(stack.errors()));
BOOST_CHECK_EQUAL(stack.print(), parsed);

std::string parsedInner = "{ let x := \"\\xe1\\xae\\xac\" }";
Expand Down
2 changes: 1 addition & 1 deletion test/libyul/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ std::pair<std::shared_ptr<AST const>, std::shared_ptr<yul::AsmAnalysisInfo>> yul
solidity::frontend::OptimiserSettings::minimal(),
DebugInfoSelection::All()
);
if (!stack.parseAndAnalyze("", _source) || !stack.errors().empty())
if (!stack.parseAndAnalyze("", _source) || Error::hasErrorsWarningsOrInfos(stack.errors()))
BOOST_FAIL("Invalid source.");
return std::make_pair(stack.parserResult()->code(), stack.parserResult()->analysisInfo);
}
Expand Down
4 changes: 2 additions & 2 deletions test/tools/yulopti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class YulOpti
try
{
auto ast = yul::Parser(errorReporter, m_dialect).parse(_charStream);
if (!m_astRoot || !errorReporter.errors().empty())
if (!m_astRoot || errorReporter.hasErrors())
{
std::cerr << "Error parsing source." << std::endl;
printErrors(_charStream, errors);
Expand All @@ -104,7 +104,7 @@ class YulOpti
errorReporter,
m_dialect
);
if (!analyzer.analyze(*m_astRoot) || !errorReporter.errors().empty())
if (!analyzer.analyze(*m_astRoot) || errorReporter.hasErrors())
{
std::cerr << "Error analyzing source." << std::endl;
printErrors(_charStream, errors);
Expand Down
2 changes: 1 addition & 1 deletion test/tools/yulrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ std::pair<std::shared_ptr<AST const>, std::shared_ptr<AsmAnalysisInfo>> parse(st
);
if (stack.parseAndAnalyze("--INPUT--", _source))
{
yulAssert(stack.errors().empty(), "Parsed successfully but had errors.");
yulAssert(!Error::hasErrorsWarningsOrInfos(stack.errors()), "Parsed successfully but had errors.");
return make_pair(stack.parserResult()->code(), stack.parserResult()->analysisInfo);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tools/yulPhaser/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ std::variant<std::unique_ptr<AST>, ErrorList> Program::parseObject(Dialect const

ObjectParser parser(errorReporter, _dialect);
std::shared_ptr<Object> object = parser.parse(scanner, false);
if (object == nullptr || !errorReporter.errors().empty())
if (object == nullptr || errorReporter.hasErrors())
// NOTE: It's possible to get errors even if the returned object is non-null.
// For example when there are errors in a nested object.
return errors;
Expand Down Expand Up @@ -165,7 +165,7 @@ std::variant<std::unique_ptr<AsmAnalysisInfo>, ErrorList> Program::analyzeAST(Di
if (!analysisSuccessful)
return errors;

assert(errorReporter.errors().empty());
assert(!errorReporter.hasErrors());
return std::variant<std::unique_ptr<AsmAnalysisInfo>, ErrorList>(std::move(analysisInfo));
}

Expand Down

0 comments on commit ac81d42

Please sign in to comment.