Skip to content

Commit f0f183e

Browse files
committed
[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
1 parent ef447fe commit f0f183e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+368
-205
lines changed

lldb/include/lldb/Interpreter/OptionValue.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_INTERPRETER_OPTIONVALUE_H
1111

1212
#include "lldb/Core/FormatEntity.h"
13+
#include "lldb/Utility/Cloneable.h"
1314
#include "lldb/Utility/CompletionRequest.h"
1415
#include "lldb/Utility/ConstString.h"
1516
#include "lldb/Utility/Status.h"
@@ -87,7 +88,8 @@ class OptionValue {
8788

8889
virtual void Clear() = 0;
8990

90-
virtual lldb::OptionValueSP DeepCopy() const = 0;
91+
virtual lldb::OptionValueSP
92+
DeepCopy(const lldb::OptionValueSP &new_parent) const;
9193

9294
virtual void AutoComplete(CommandInterpreter &interpreter,
9395
CompletionRequest &request);
@@ -306,6 +308,8 @@ class OptionValue {
306308
m_parent_wp = parent_sp;
307309
}
308310

311+
lldb::OptionValueSP GetParent() const { return m_parent_wp.lock(); }
312+
309313
void SetValueChangedCallback(std::function<void()> callback) {
310314
assert(!m_callback);
311315
m_callback = std::move(callback);
@@ -317,6 +321,12 @@ class OptionValue {
317321
}
318322

319323
protected:
324+
using TopmostBase = OptionValue;
325+
326+
// Must be overriden by a derived class for correct downcasting the result of
327+
// DeepCopy to it. Inherit from Cloneable to avoid doing this manually.
328+
virtual lldb::OptionValueSP Clone() const = 0;
329+
320330
lldb::OptionValueWP m_parent_wp;
321331
std::function<void()> m_callback;
322332
bool m_value_was_set; // This can be used to see if a value has been set

lldb/include/lldb/Interpreter/OptionValueArch.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace lldb_private {
1717

18-
class OptionValueArch : public OptionValue {
18+
class OptionValueArch : public Cloneable<OptionValueArch, OptionValue> {
1919
public:
2020
OptionValueArch() = default;
2121

@@ -47,8 +47,6 @@ class OptionValueArch : public OptionValue {
4747
m_value_was_set = false;
4848
}
4949

50-
lldb::OptionValueSP DeepCopy() const override;
51-
5250
void AutoComplete(CommandInterpreter &interpreter,
5351
lldb_private::CompletionRequest &request) override;
5452

lldb/include/lldb/Interpreter/OptionValueArgs.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313

1414
namespace lldb_private {
1515

16-
class OptionValueArgs : public OptionValueArray {
16+
class OptionValueArgs : public Cloneable<OptionValueArgs, OptionValueArray> {
1717
public:
1818
OptionValueArgs()
19-
: OptionValueArray(
20-
OptionValue::ConvertTypeToMask(OptionValue::eTypeString)) {}
19+
: Cloneable(OptionValue::ConvertTypeToMask(OptionValue::eTypeString)) {}
2120

2221
~OptionValueArgs() override = default;
2322

lldb/include/lldb/Interpreter/OptionValueArray.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace lldb_private {
1717

18-
class OptionValueArray : public OptionValue {
18+
class OptionValueArray : public Cloneable<OptionValueArray, OptionValue> {
1919
public:
2020
OptionValueArray(uint32_t type_mask = UINT32_MAX, bool raw_value_dump = false)
2121
: m_type_mask(type_mask), m_values(), m_raw_value_dump(raw_value_dump) {}
@@ -38,7 +38,8 @@ class OptionValueArray : public OptionValue {
3838
m_value_was_set = false;
3939
}
4040

41-
lldb::OptionValueSP DeepCopy() const override;
41+
lldb::OptionValueSP
42+
DeepCopy(const lldb::OptionValueSP &new_parent) const override;
4243

4344
bool IsAggregateValue() const override { return true; }
4445

lldb/include/lldb/Interpreter/OptionValueBoolean.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace lldb_private {
1515

16-
class OptionValueBoolean : public OptionValue {
16+
class OptionValueBoolean : public Cloneable<OptionValueBoolean, OptionValue> {
1717
public:
1818
OptionValueBoolean(bool value)
1919
: m_current_value(value), m_default_value(value) {}
@@ -71,8 +71,6 @@ class OptionValueBoolean : public OptionValue {
7171

7272
void SetDefaultValue(bool value) { m_default_value = value; }
7373

74-
lldb::OptionValueSP DeepCopy() const override;
75-
7674
protected:
7775
bool m_current_value;
7876
bool m_default_value;

lldb/include/lldb/Interpreter/OptionValueChar.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace lldb_private {
1515

16-
class OptionValueChar : public OptionValue {
16+
class OptionValueChar : public Cloneable<OptionValueChar, OptionValue> {
1717
public:
1818
OptionValueChar(char value)
1919
: m_current_value(value), m_default_value(value) {}
@@ -54,8 +54,6 @@ class OptionValueChar : public OptionValue {
5454

5555
void SetDefaultValue(char value) { m_default_value = value; }
5656

57-
lldb::OptionValueSP DeepCopy() const override;
58-
5957
protected:
6058
char m_current_value;
6159
char m_default_value;

lldb/include/lldb/Interpreter/OptionValueDictionary.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
namespace lldb_private {
1717

18-
class OptionValueDictionary : public OptionValue {
18+
class OptionValueDictionary
19+
: public Cloneable<OptionValueDictionary, OptionValue> {
1920
public:
2021
OptionValueDictionary(uint32_t type_mask = UINT32_MAX,
2122
bool raw_value_dump = true)
@@ -39,7 +40,8 @@ class OptionValueDictionary : public OptionValue {
3940
m_value_was_set = false;
4041
}
4142

42-
lldb::OptionValueSP DeepCopy() const override;
43+
lldb::OptionValueSP
44+
DeepCopy(const lldb::OptionValueSP &new_parent) const override;
4345

4446
bool IsAggregateValue() const override { return true; }
4547

lldb/include/lldb/Interpreter/OptionValueEnumeration.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
namespace lldb_private {
2121

22-
class OptionValueEnumeration : public OptionValue {
22+
class OptionValueEnumeration
23+
: public Cloneable<OptionValueEnumeration, OptionValue> {
2324
public:
2425
typedef int64_t enum_type;
2526
struct EnumeratorInfo {
@@ -49,8 +50,6 @@ class OptionValueEnumeration : public OptionValue {
4950
m_value_was_set = false;
5051
}
5152

52-
lldb::OptionValueSP DeepCopy() const override;
53-
5453
void AutoComplete(CommandInterpreter &interpreter,
5554
CompletionRequest &request) override;
5655

lldb/include/lldb/Interpreter/OptionValueFileColonLine.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
namespace lldb_private {
1818

19-
class OptionValueFileColonLine : public OptionValue {
19+
class OptionValueFileColonLine :
20+
public Cloneable<OptionValueFileColonLine, OptionValue> {
2021
public:
2122
OptionValueFileColonLine();
2223
OptionValueFileColonLine(const llvm::StringRef input);
@@ -38,8 +39,6 @@ class OptionValueFileColonLine : public OptionValue {
3839
m_column_number = LLDB_INVALID_COLUMN_NUMBER;
3940
}
4041

41-
lldb::OptionValueSP DeepCopy() const override;
42-
4342
void AutoComplete(CommandInterpreter &interpreter,
4443
CompletionRequest &request) override;
4544

lldb/include/lldb/Interpreter/OptionValueFileSpec.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace lldb_private {
1818

19-
class OptionValueFileSpec : public OptionValue {
19+
class OptionValueFileSpec : public Cloneable<OptionValueFileSpec, OptionValue> {
2020
public:
2121
OptionValueFileSpec(bool resolve = true);
2222

@@ -45,8 +45,6 @@ class OptionValueFileSpec : public OptionValue {
4545
m_data_mod_time = llvm::sys::TimePoint<>();
4646
}
4747

48-
lldb::OptionValueSP DeepCopy() const override;
49-
5048
void AutoComplete(CommandInterpreter &interpreter,
5149
CompletionRequest &request) override;
5250

lldb/include/lldb/Interpreter/OptionValueFileSpecList.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
namespace lldb_private {
1818

19-
class OptionValueFileSpecList : public OptionValue {
19+
class OptionValueFileSpecList
20+
: public Cloneable<OptionValueFileSpecList, OptionValue> {
2021
public:
2122
OptionValueFileSpecList() = default;
2223

23-
OptionValueFileSpecList(const FileSpecList &current_value)
24-
: m_current_value(current_value) {}
24+
OptionValueFileSpecList(const OptionValueFileSpecList &other)
25+
: Cloneable(other), m_current_value(other.GetCurrentValue()) {}
2526

2627
~OptionValueFileSpecList() override = default;
2728

@@ -42,8 +43,6 @@ class OptionValueFileSpecList : public OptionValue {
4243
m_value_was_set = false;
4344
}
4445

45-
lldb::OptionValueSP DeepCopy() const override;
46-
4746
bool IsAggregateValue() const override { return true; }
4847

4948
// Subclass specific functions
@@ -64,6 +63,8 @@ class OptionValueFileSpecList : public OptionValue {
6463
}
6564

6665
protected:
66+
lldb::OptionValueSP Clone() const override;
67+
6768
mutable std::recursive_mutex m_mutex;
6869
FileSpecList m_current_value;
6970
};

lldb/include/lldb/Interpreter/OptionValueFormat.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
namespace lldb_private {
1515

16-
class OptionValueFormat : public OptionValue {
16+
class OptionValueFormat
17+
: public Cloneable<OptionValueFormat, OptionValue> {
1718
public:
1819
OptionValueFormat(lldb::Format value)
1920
: m_current_value(value), m_default_value(value) {}
@@ -39,8 +40,6 @@ class OptionValueFormat : public OptionValue {
3940
m_value_was_set = false;
4041
}
4142

42-
lldb::OptionValueSP DeepCopy() const override;
43-
4443
// Subclass specific functions
4544

4645
lldb::Format GetCurrentValue() const { return m_current_value; }

lldb/include/lldb/Interpreter/OptionValueFormatEntity.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
namespace lldb_private {
1616

17-
class OptionValueFormatEntity : public OptionValue {
17+
class OptionValueFormatEntity
18+
: public Cloneable<OptionValueFormatEntity, OptionValue> {
1819
public:
1920
OptionValueFormatEntity(const char *default_format);
2021

@@ -33,8 +34,6 @@ class OptionValueFormatEntity : public OptionValue {
3334

3435
void Clear() override;
3536

36-
lldb::OptionValueSP DeepCopy() const override;
37-
3837
void AutoComplete(CommandInterpreter &interpreter,
3938
CompletionRequest &request) override;
4039

lldb/include/lldb/Interpreter/OptionValueLanguage.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace lldb_private {
1717

18-
class OptionValueLanguage : public OptionValue {
18+
class OptionValueLanguage : public Cloneable<OptionValueLanguage, OptionValue> {
1919
public:
2020
OptionValueLanguage(lldb::LanguageType value)
2121
: m_current_value(value), m_default_value(value) {}
@@ -42,8 +42,6 @@ class OptionValueLanguage : public OptionValue {
4242
m_value_was_set = false;
4343
}
4444

45-
lldb::OptionValueSP DeepCopy() const override;
46-
4745
// Subclass specific functions
4846

4947
lldb::LanguageType GetCurrentValue() const { return m_current_value; }

lldb/include/lldb/Interpreter/OptionValuePathMappings.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
namespace lldb_private {
1616

17-
class OptionValuePathMappings : public OptionValue {
17+
class OptionValuePathMappings
18+
: public Cloneable<OptionValuePathMappings, OptionValue> {
1819
public:
1920
OptionValuePathMappings(bool notify_changes)
2021
: m_notify_changes(notify_changes) {}
@@ -37,8 +38,6 @@ class OptionValuePathMappings : public OptionValue {
3738
m_value_was_set = false;
3839
}
3940

40-
lldb::OptionValueSP DeepCopy() const override;
41-
4241
bool IsAggregateValue() const override { return true; }
4342

4443
// Subclass specific functions

lldb/include/lldb/Interpreter/OptionValueProperties.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,27 @@
1818
#include "lldb/Utility/ConstString.h"
1919

2020
namespace lldb_private {
21+
class Properties;
2122

2223
class OptionValueProperties
23-
: public OptionValue,
24+
: public Cloneable<OptionValueProperties, OptionValue>,
2425
public std::enable_shared_from_this<OptionValueProperties> {
2526
public:
2627
OptionValueProperties() = default;
2728

2829
OptionValueProperties(ConstString name);
2930

30-
OptionValueProperties(const OptionValueProperties &global_properties);
31-
3231
~OptionValueProperties() override = default;
3332

3433
Type GetType() const override { return eTypeProperties; }
3534

3635
void Clear() override;
3736

38-
lldb::OptionValueSP DeepCopy() const override;
37+
static lldb::OptionValuePropertiesSP
38+
CreateLocalCopy(const Properties &global_properties);
39+
40+
lldb::OptionValueSP
41+
DeepCopy(const lldb::OptionValueSP &new_parent) const override;
3942

4043
Status
4144
SetValueFromString(llvm::StringRef value,

lldb/include/lldb/Interpreter/OptionValueRegex.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace lldb_private {
1616

17-
class OptionValueRegex : public OptionValue {
17+
class OptionValueRegex : public Cloneable<OptionValueRegex, OptionValue> {
1818
public:
1919
OptionValueRegex(const char *value = nullptr)
2020
: m_regex(llvm::StringRef::withNullAsEmpty(value)),
@@ -38,8 +38,6 @@ class OptionValueRegex : public OptionValue {
3838
m_value_was_set = false;
3939
}
4040

41-
lldb::OptionValueSP DeepCopy() const override;
42-
4341
// Subclass specific functions
4442
const RegularExpression *GetCurrentValue() const {
4543
return (m_regex.IsValid() ? &m_regex : nullptr);

lldb/include/lldb/Interpreter/OptionValueSInt64.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace lldb_private {
1616

17-
class OptionValueSInt64 : public OptionValue {
17+
class OptionValueSInt64 : public Cloneable<OptionValueSInt64, OptionValue> {
1818
public:
1919
OptionValueSInt64() = default;
2020

@@ -44,8 +44,6 @@ class OptionValueSInt64 : public OptionValue {
4444
m_value_was_set = false;
4545
}
4646

47-
lldb::OptionValueSP DeepCopy() const override;
48-
4947
// Subclass specific functions
5048

5149
const int64_t &operator=(int64_t value) {

lldb/include/lldb/Interpreter/OptionValueString.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace lldb_private {
1919

20-
class OptionValueString : public OptionValue {
20+
class OptionValueString : public Cloneable<OptionValueString, OptionValue> {
2121
public:
2222
typedef Status (*ValidatorCallback)(const char *string, void *baton);
2323

@@ -78,8 +78,6 @@ class OptionValueString : public OptionValue {
7878
m_value_was_set = false;
7979
}
8080

81-
lldb::OptionValueSP DeepCopy() const override;
82-
8381
// Subclass specific functions
8482

8583
Flags &GetOptions() { return m_options; }

0 commit comments

Comments
 (0)