From 5e01d767d02d064a064a67dcf95ee299c46f741f Mon Sep 17 00:00:00 2001
From: chriseth <chris@ethereum.org>
Date: Wed, 17 Jan 2018 12:05:43 +0100
Subject: Prevent externally used functions from being removed.

---
 libyul/optimiser/UnusedPruner.cpp | 8 +++++---
 libyul/optimiser/UnusedPruner.h   | 4 ++--
 2 files changed, 7 insertions(+), 5 deletions(-)

(limited to 'libyul')

diff --git a/libyul/optimiser/UnusedPruner.cpp b/libyul/optimiser/UnusedPruner.cpp
index 74b6bee4..37a74553 100644
--- a/libyul/optimiser/UnusedPruner.cpp
+++ b/libyul/optimiser/UnusedPruner.cpp
@@ -33,12 +33,14 @@ using namespace std;
 using namespace dev;
 using namespace dev::yul;
 
-UnusedPruner::UnusedPruner(Block& _ast)
+UnusedPruner::UnusedPruner(Block& _ast, set<string> const& _externallyUsedFunctions)
 {
 	ReferencesCounter counter;
 	counter(_ast);
 
 	m_references = counter.references();
+	for (auto const& f: _externallyUsedFunctions)
+		++m_references[f];
 }
 
 void UnusedPruner::operator()(Block& _block)
@@ -89,11 +91,11 @@ void UnusedPruner::operator()(Block& _block)
 	ASTModifier::operator()(_block);
 }
 
-void UnusedPruner::runUntilStabilised(Block& _ast)
+void UnusedPruner::runUntilStabilised(Block& _ast, set<string> const& _externallyUsedFunctions)
 {
 	while (true)
 	{
-		UnusedPruner pruner(_ast);
+		UnusedPruner pruner(_ast, _externallyUsedFunctions);
 		pruner(_ast);
 		if (!pruner.shouldRunAgain())
 			return;
diff --git a/libyul/optimiser/UnusedPruner.h b/libyul/optimiser/UnusedPruner.h
index 327921ea..30617ff3 100644
--- a/libyul/optimiser/UnusedPruner.h
+++ b/libyul/optimiser/UnusedPruner.h
@@ -44,7 +44,7 @@ namespace yul
 class UnusedPruner: public ASTModifier
 {
 public:
-	explicit UnusedPruner(Block& _ast);
+	explicit UnusedPruner(Block& _ast, std::set<std::string> const& _externallyUsedFunctions = std::set<std::string>());
 
 	using ASTModifier::operator();
 	virtual void operator()(Block& _block) override;
@@ -53,7 +53,7 @@ public:
 	bool shouldRunAgain() const { return m_shouldRunAgain; }
 
 	// Run the pruner until the code does not change anymore.
-	static void runUntilStabilised(Block& _ast);
+	static void runUntilStabilised(Block& _ast, std::set<std::string> const& _externallyUsedFunctions = std::set<std::string>());
 
 private:
 	bool used(std::string const& _name) const;
-- 
cgit v1.2.3