Merge pull request #7961 from ethereum/error-recovery-ast-segfault

Fix segfault on empty contract compiled w/ --error-recovery
This commit is contained in:
chriseth 2019-12-16 11:45:25 +01:00 committed by GitHub
commit 800090e850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 30 deletions

View File

@ -281,30 +281,31 @@ bool CompilerStack::analyze()
{
SyntaxChecker syntaxChecker(m_errorReporter, m_optimiserSettings.runYulOptimiser);
for (Source const* source: m_sourceOrder)
if (!syntaxChecker.checkSyntax(*source->ast))
if (source->ast && !syntaxChecker.checkSyntax(*source->ast))
noErrors = false;
DocStringAnalyser docStringAnalyser(m_errorReporter);
for (Source const* source: m_sourceOrder)
if (!docStringAnalyser.analyseDocStrings(*source->ast))
if (source->ast && !docStringAnalyser.analyseDocStrings(*source->ast))
noErrors = false;
m_globalContext = make_shared<GlobalContext>();
NameAndTypeResolver resolver(*m_globalContext, m_evmVersion, m_scopes, m_errorReporter);
for (Source const* source: m_sourceOrder)
if (!resolver.registerDeclarations(*source->ast))
if (source->ast && !resolver.registerDeclarations(*source->ast))
return false;
map<string, SourceUnit const*> sourceUnitsByName;
for (auto& source: m_sources)
sourceUnitsByName[source.first] = source.second.ast.get();
for (Source const* source: m_sourceOrder)
if (!resolver.performImports(*source->ast, sourceUnitsByName))
if (source->ast && !resolver.performImports(*source->ast, sourceUnitsByName))
return false;
// This is the main name and type resolution loop. Needs to be run for every contract, because
// the special variables "this" and "super" must be set appropriately.
for (Source const* source: m_sourceOrder)
if (source->ast)
for (ASTPointer<ASTNode> const& node: source->ast->nodes())
{
if (!resolver.resolveNamesAndTypes(*node))
@ -324,6 +325,7 @@ bool CompilerStack::analyze()
// type checker.
ContractLevelChecker contractLevelChecker(m_errorReporter);
for (Source const* source: m_sourceOrder)
if (source->ast)
for (ASTPointer<ASTNode> const& node: source->ast->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
if (!contractLevelChecker.check(*contract))
@ -338,6 +340,7 @@ bool CompilerStack::analyze()
// which is only done one step later.
TypeChecker typeChecker(m_evmVersion, m_errorReporter);
for (Source const* source: m_sourceOrder)
if (source->ast)
for (ASTPointer<ASTNode> const& node: source->ast->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
if (!typeChecker.checkTypeRequirements(*contract))
@ -348,7 +351,7 @@ bool CompilerStack::analyze()
// Checks that can only be done when all types of all AST nodes are known.
PostTypeChecker postTypeChecker(m_errorReporter);
for (Source const* source: m_sourceOrder)
if (!postTypeChecker.check(*source->ast))
if (source->ast && !postTypeChecker.check(*source->ast))
noErrors = false;
}
@ -358,14 +361,14 @@ bool CompilerStack::analyze()
// variable is used before it is assigned to.
CFG cfg(m_errorReporter);
for (Source const* source: m_sourceOrder)
if (!cfg.constructFlow(*source->ast))
if (source->ast && !cfg.constructFlow(*source->ast))
noErrors = false;
if (noErrors)
{
ControlFlowAnalyzer controlFlowAnalyzer(cfg, m_errorReporter);
for (Source const* source: m_sourceOrder)
if (!controlFlowAnalyzer.analyze(*source->ast))
if (source->ast && !controlFlowAnalyzer.analyze(*source->ast))
noErrors = false;
}
}
@ -375,7 +378,7 @@ bool CompilerStack::analyze()
// Checks for common mistakes. Only generates warnings.
StaticAnalyzer staticAnalyzer(m_errorReporter);
for (Source const* source: m_sourceOrder)
if (!staticAnalyzer.analyze(*source->ast))
if (source->ast && !staticAnalyzer.analyze(*source->ast))
noErrors = false;
}
@ -384,6 +387,7 @@ bool CompilerStack::analyze()
// Check for state mutability in every function.
vector<ASTPointer<ASTNode>> ast;
for (Source const* source: m_sourceOrder)
if (source->ast)
ast.push_back(source->ast);
if (!ViewPureChecker(ast, m_errorReporter).check())
@ -394,6 +398,7 @@ bool CompilerStack::analyze()
{
ModelChecker modelChecker(m_errorReporter, m_smtlib2Responses, m_readFile, m_enabledSMTSolvers);
for (Source const* source: m_sourceOrder)
if (source->ast)
modelChecker.analyze(*source->ast);
m_unhandledSMTLib2Queries += modelChecker.unhandledQueries();
}

View File

@ -0,0 +1 @@
--error-recovery

View File

@ -0,0 +1,8 @@
Error: Expected pragma, import directive or contract/interface/library/struct/enum definition.
--> recovery_ast_empty_contract/input.sol:2:1:
|
2 | c
| ^
Compilation halted after AST generation due to errors.

View File

@ -0,0 +1,2 @@
pragma 0.5.11;
c