diff options
| author | Tatyana Krasnukha <tatyana@synopsys.com> | 2021-02-20 00:49:42 +0300 |
|---|---|---|
| committer | Tatyana Krasnukha <tatyana@synopsys.com> | 2021-02-28 19:23:25 +0300 |
| commit | f0f183ee4ad952d94234cf6971c69a044e05c9df (patch) | |
| tree | d36d5b3aa8719a019529ca7fb702d565042ba498 /lldb/source/Interpreter/OptionValueArray.cpp | |
| parent | ef447fe0088cacc38027028d4c43c1938d3eb9e7 (diff) | |
[lldb/Interpreter] Fix deep copying for OptionValue classes
Some implementations of the DeepCopy function called the copy constructor that copied m_parent member instead of setting a new parent. Others just leaved the base class's members (m_parent, m_callback, m_was_set) empty.
One more problem is that not all classes override this function, e.g. OptionValueArgs::DeepCopy produces OptionValueArray instance, and Target[Process/Thread]ValueProperty::DeepCopy produces OptionValueProperty. This makes downcasting via static_cast invalid.
The patch implements idiom "virtual constructor" to fix these issues.
Add a test that checks DeepCopy for correct copying/setting all data members of the base class.
Differential Revision: https://reviews.llvm.org/D96952
Diffstat (limited to 'lldb/source/Interpreter/OptionValueArray.cpp')
| -rw-r--r-- | lldb/source/Interpreter/OptionValueArray.cpp | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/lldb/source/Interpreter/OptionValueArray.cpp b/lldb/source/Interpreter/OptionValueArray.cpp index 0b293ccfc248..b1545bdebf10 100644 --- a/lldb/source/Interpreter/OptionValueArray.cpp +++ b/lldb/source/Interpreter/OptionValueArray.cpp @@ -303,15 +303,16 @@ Status OptionValueArray::SetArgs(const Args &args, VarSetOperationType op) { return error; } -lldb::OptionValueSP OptionValueArray::DeepCopy() const { - OptionValueArray *copied_array = - new OptionValueArray(m_type_mask, m_raw_value_dump); - lldb::OptionValueSP copied_value_sp(copied_array); - *static_cast<OptionValue *>(copied_array) = *this; - copied_array->m_callback = m_callback; - const uint32_t size = m_values.size(); - for (uint32_t i = 0; i < size; ++i) { - copied_array->AppendValue(m_values[i]->DeepCopy()); - } - return copied_value_sp; +OptionValueSP +OptionValueArray::DeepCopy(const OptionValueSP &new_parent) const { + auto copy_sp = OptionValue::DeepCopy(new_parent); + // copy_sp->GetAsArray cannot be used here as it doesn't work for derived + // types that override GetType returning a different value. + auto *array_value_ptr = static_cast<OptionValueArray *>(copy_sp.get()); + lldbassert(array_value_ptr); + + for (auto &value : array_value_ptr->m_values) + value = value->DeepCopy(copy_sp); + + return copy_sp; } |
