mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add ScopedSaveAndRestore helper.
This commit is contained in:
parent
0289994da5
commit
afae46dcb5
@ -133,4 +133,26 @@ private:
|
||||
std::function<void(void)> m_f;
|
||||
};
|
||||
|
||||
/// RAII utility class that sets the value of a variable for the current scope and restores it to its old value
|
||||
/// during its destructor.
|
||||
template<typename V>
|
||||
class ScopedSaveAndRestore
|
||||
{
|
||||
public:
|
||||
explicit ScopedSaveAndRestore(V& _variable, V&& _value): m_variable(_variable), m_oldValue(std::move(_value))
|
||||
{
|
||||
std::swap(m_variable, m_oldValue);
|
||||
}
|
||||
ScopedSaveAndRestore(ScopedSaveAndRestore const&) = delete;
|
||||
~ScopedSaveAndRestore() { std::swap(m_variable, m_oldValue); }
|
||||
ScopedSaveAndRestore& operator=(ScopedSaveAndRestore const&) = delete;
|
||||
|
||||
private:
|
||||
V& m_variable;
|
||||
V m_oldValue;
|
||||
};
|
||||
|
||||
template<typename V, typename... Args>
|
||||
ScopedSaveAndRestore(V, Args...) -> ScopedSaveAndRestore<V>;
|
||||
|
||||
}
|
||||
|
@ -156,16 +156,11 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
||||
{
|
||||
// Save all information. We might rather reinstantiate this class,
|
||||
// but this could be difficult if it is subclassed.
|
||||
map<YulString, AssignedValue> value;
|
||||
size_t loopDepth{0};
|
||||
unordered_map<YulString, set<YulString>> references;
|
||||
unordered_map<YulString, YulString> storage;
|
||||
unordered_map<YulString, YulString> memory;
|
||||
swap(m_value, value);
|
||||
swap(m_loopDepth, loopDepth);
|
||||
swap(m_references, references);
|
||||
swap(m_storage, storage);
|
||||
swap(m_memory, memory);
|
||||
ScopedSaveAndRestore valueResetter(m_value, {});
|
||||
ScopedSaveAndRestore loopDepthResetter(m_loopDepth, 0u);
|
||||
ScopedSaveAndRestore referencesResetter(m_references, {});
|
||||
ScopedSaveAndRestore storageResetter(m_storage, {});
|
||||
ScopedSaveAndRestore memoryResetter(m_memory, {});
|
||||
pushScope(true);
|
||||
|
||||
for (auto const& parameter: _fun.parameters)
|
||||
@ -182,11 +177,6 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
||||
// statement.
|
||||
|
||||
popScope();
|
||||
swap(m_value, value);
|
||||
swap(m_loopDepth, loopDepth);
|
||||
swap(m_references, references);
|
||||
swap(m_storage, storage);
|
||||
swap(m_memory, memory);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
||||
|
Loading…
Reference in New Issue
Block a user