-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14575 from ethereum/syntactic-call-graph
Function dependency graph
- Loading branch information
Showing
15 changed files
with
470 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
libsolidity/experimental/analysis/FunctionDependencyAnalysis.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
#include <libsolidity/experimental/analysis/Analysis.h> | ||
#include <libsolidity/experimental/analysis/FunctionDependencyAnalysis.h> | ||
|
||
using namespace solidity::frontend::experimental; | ||
using namespace solidity::util; | ||
|
||
FunctionDependencyAnalysis::FunctionDependencyAnalysis(Analysis& _analysis): | ||
m_analysis(_analysis), | ||
m_errorReporter(_analysis.errorReporter()) | ||
{ | ||
} | ||
|
||
bool FunctionDependencyAnalysis::analyze(SourceUnit const& _sourceUnit) | ||
{ | ||
_sourceUnit.accept(*this); | ||
return !m_errorReporter.hasErrors(); | ||
} | ||
|
||
bool FunctionDependencyAnalysis::visit(FunctionDefinition const& _functionDefinition) | ||
{ | ||
solAssert(!m_currentFunction); | ||
m_currentFunction = &_functionDefinition; | ||
// Insert a function definition pointer that maps to an empty set; the pointed to set will later be | ||
// populated in ``endVisit(Identifier const& _identifier)`` if ``m_currentFunction`` references another. | ||
auto [_, inserted] = annotation().functionCallGraph.edges.try_emplace( | ||
m_currentFunction, std::set<FunctionDefinition const*, ASTCompareByID<FunctionDefinition>>{} | ||
); | ||
solAssert(inserted); | ||
return true; | ||
} | ||
|
||
void FunctionDependencyAnalysis::endVisit(FunctionDefinition const&) | ||
{ | ||
m_currentFunction = nullptr; | ||
} | ||
|
||
void FunctionDependencyAnalysis::endVisit(Identifier const& _identifier) | ||
{ | ||
auto const* callee = dynamic_cast<FunctionDefinition const*>(_identifier.annotation().referencedDeclaration); | ||
// Check that the identifier is within a function body and is a function, and add it to the graph | ||
// as an ``m_currentFunction`` -> ``callee`` edge. | ||
if (m_currentFunction && callee) | ||
addEdge(m_currentFunction, callee); | ||
} | ||
|
||
void FunctionDependencyAnalysis::addEdge(FunctionDefinition const* _caller, FunctionDefinition const* _callee) | ||
{ | ||
annotation().functionCallGraph.edges[_caller].insert(_callee); | ||
} | ||
|
||
FunctionDependencyAnalysis::GlobalAnnotation& FunctionDependencyAnalysis::annotation() | ||
{ | ||
return m_analysis.annotation<FunctionDependencyAnalysis>(); | ||
} |
57 changes: 57 additions & 0 deletions
57
libsolidity/experimental/analysis/FunctionDependencyAnalysis.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
#pragma once | ||
|
||
#include <liblangutil/ErrorReporter.h> | ||
#include <libsolidity/ast/ASTForward.h> | ||
#include <libsolidity/ast/ASTVisitor.h> | ||
#include <libsolidity/experimental/ast/FunctionCallGraph.h> | ||
|
||
#include <memory> | ||
|
||
namespace solidity::frontend::experimental | ||
{ | ||
|
||
class Analysis; | ||
|
||
class FunctionDependencyAnalysis: private ASTConstVisitor | ||
{ | ||
public: | ||
FunctionDependencyAnalysis(Analysis& _analysis); | ||
bool analyze(SourceUnit const& _sourceUnit); | ||
|
||
struct Annotation {}; | ||
struct GlobalAnnotation | ||
{ | ||
FunctionDependencyGraph functionCallGraph; | ||
}; | ||
|
||
private: | ||
bool visit(FunctionDefinition const& _functionDefinition) override; | ||
void endVisit(FunctionDefinition const&) override; | ||
void endVisit(Identifier const& _identifier) override; | ||
void addEdge(FunctionDefinition const* _caller, FunctionDefinition const* _callee); | ||
GlobalAnnotation& annotation(); | ||
|
||
Analysis& m_analysis; | ||
langutil::ErrorReporter& m_errorReporter; | ||
FunctionDefinition const* m_currentFunction = nullptr; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
/// Data structure representing a function dependency graph. | ||
|
||
#pragma once | ||
|
||
#include <libsolidity/ast/AST.h> | ||
|
||
#include <map> | ||
#include <set> | ||
#include <ostream> | ||
|
||
namespace solidity::frontend::experimental | ||
{ | ||
|
||
struct FunctionDependencyGraph | ||
{ | ||
/// Graph edges. Edges are directed and lead from the caller to the callee. | ||
/// The map contains a key for every function, even if does not actually perform | ||
/// any calls. | ||
std::map<FunctionDefinition const*, std::set<FunctionDefinition const*, ASTCompareByID<FunctionDefinition>>, ASTCompareByID<FunctionDefinition>> edges; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.