Skip to content

Commit bd4bf82

Browse files
committedMar 6, 2019
[SBAPI] Don't check IsValid in constructor
When running the test suite with the instrumentation macros, I noticed two lldb-mi tests regressed. The issue was the copy constructor of SBLineEntry. Without the macros the returned value would be elided, but with the macros the copy constructor was called. The latter using ::IsValid to determine whether the underlying opaque pointer should be set. This is likely a remnant of when ::IsValid would only check the validity of the smart pointer. In SBLineEntry however, it actually forwards to LineEntry::IsValid(). So what happened here was that because of the macros the copy constructor was called. The opaque pointer was valid but the LineEntry didn't consider itself valid. So the copied-to object ended up default initialized. This patch replaces all checks for IsValid in copy (assignment) constructors with checks for the opaque pointer itself. Differential revision: https://reviews.llvm.org/D58946 llvm-svn: 355458
1 parent d823020 commit bd4bf82

21 files changed

+138
-150
lines changed
 

‎lldb/lldb.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,7 @@
32383238
9A4633DA11F65D8600955CE1 /* UserSettingsController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UserSettingsController.h; path = include/lldb/Core/UserSettingsController.h; sourceTree = "<group>"; };
32393239
4C00833F1B9F9BA900D5CF24 /* UtilityFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UtilityFunction.cpp; path = source/Expression/UtilityFunction.cpp; sourceTree = "<group>"; };
32403240
4C00833D1B9F9B8400D5CF24 /* UtilityFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UtilityFunction.h; path = include/lldb/Expression/UtilityFunction.h; sourceTree = "<group>"; };
3241+
DD54302F222F190D00C1F021 /* Utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Utils.h; path = source/API/Utils.h; sourceTree = "<group>"; };
32413242
2654A6921E552F4600DA1013 /* VASPrintf.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = VASPrintf.h; path = include/lldb/Utility/VASPrintf.h; sourceTree = "<group>"; };
32423243
2654A68F1E552ED500DA1013 /* VASprintf.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = VASprintf.cpp; path = source/Utility/VASprintf.cpp; sourceTree = "<group>"; };
32433244
9A3D43C41F3150D200EB767C /* VASprintfTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VASprintfTest.cpp; sourceTree = "<group>"; };
@@ -4442,6 +4443,7 @@
44424443
23059A0F1958B319007B8189 /* SBUnixSignals.cpp */,
44434444
9A19A6A51163BB7E00E0D453 /* SBValue.h */,
44444445
9A19A6AD1163BB9800E0D453 /* SBValue.cpp */,
4446+
DD54302F222F190D00C1F021 /* Utils.h */,
44454447
9A357582116CFDEE00E8ED2F /* SBValueList.h */,
44464448
9A35758D116CFE0F00E8ED2F /* SBValueList.cpp */,
44474449
94235B9A1A8D5FD800EB2EED /* SBVariablesOptions.h */,

‎lldb/source/API/SBAddress.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBAddress.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBProcess.h"
1112
#include "lldb/API/SBSection.h"
1213
#include "lldb/API/SBStream.h"
@@ -25,12 +26,11 @@ SBAddress::SBAddress() : m_opaque_up(new Address()) {}
2526
SBAddress::SBAddress(const Address *lldb_object_ptr)
2627
: m_opaque_up(new Address()) {
2728
if (lldb_object_ptr)
28-
ref() = *lldb_object_ptr;
29+
m_opaque_up = llvm::make_unique<Address>(*lldb_object_ptr);
2930
}
3031

3132
SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
32-
if (rhs.IsValid())
33-
ref() = rhs.ref();
33+
m_opaque_up = clone(rhs.m_opaque_up);
3434
}
3535

3636
SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
@@ -45,12 +45,8 @@ SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
4545
SBAddress::~SBAddress() {}
4646

4747
const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
48-
if (this != &rhs) {
49-
if (rhs.IsValid())
50-
ref() = rhs.ref();
51-
else
52-
m_opaque_up.reset(new Address());
53-
}
48+
if (this != &rhs)
49+
m_opaque_up = clone(rhs.m_opaque_up);
5450
return *this;
5551
}
5652

‎lldb/source/API/SBAttachInfo.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBAttachInfo.h"
10-
10+
#include "Utils.h"
1111
#include "lldb/API/SBFileSpec.h"
1212
#include "lldb/API/SBListener.h"
1313
#include "lldb/Target/Process.h"
@@ -39,7 +39,7 @@ SBAttachInfo::SBAttachInfo(const char *path, bool wait_for, bool async)
3939

4040
SBAttachInfo::SBAttachInfo(const SBAttachInfo &rhs)
4141
: m_opaque_sp(new ProcessAttachInfo()) {
42-
*m_opaque_sp = *rhs.m_opaque_sp;
42+
m_opaque_sp = clone(rhs.m_opaque_sp);
4343
}
4444

4545
SBAttachInfo::~SBAttachInfo() {}
@@ -48,7 +48,7 @@ lldb_private::ProcessAttachInfo &SBAttachInfo::ref() { return *m_opaque_sp; }
4848

4949
SBAttachInfo &SBAttachInfo::operator=(const SBAttachInfo &rhs) {
5050
if (this != &rhs)
51-
*m_opaque_sp = *rhs.m_opaque_sp;
51+
m_opaque_sp = clone(rhs.m_opaque_sp);
5252
return *this;
5353
}
5454

‎lldb/source/API/SBCommandReturnObject.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBCommandReturnObject.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBError.h"
1112
#include "lldb/API/SBStream.h"
12-
1313
#include "lldb/Interpreter/CommandReturnObject.h"
1414
#include "lldb/Utility/ConstString.h"
1515
#include "lldb/Utility/Log.h"
@@ -23,8 +23,7 @@ SBCommandReturnObject::SBCommandReturnObject()
2323

2424
SBCommandReturnObject::SBCommandReturnObject(const SBCommandReturnObject &rhs)
2525
: m_opaque_up() {
26-
if (rhs.m_opaque_up)
27-
m_opaque_up.reset(new CommandReturnObject(*rhs.m_opaque_up));
26+
m_opaque_up = clone(rhs.m_opaque_up);
2827
}
2928

3029
SBCommandReturnObject::SBCommandReturnObject(CommandReturnObject *ptr)
@@ -38,12 +37,8 @@ CommandReturnObject *SBCommandReturnObject::Release() {
3837

3938
const SBCommandReturnObject &SBCommandReturnObject::
4039
operator=(const SBCommandReturnObject &rhs) {
41-
if (this != &rhs) {
42-
if (rhs.m_opaque_up)
43-
m_opaque_up.reset(new CommandReturnObject(*rhs.m_opaque_up));
44-
else
45-
m_opaque_up.reset();
46-
}
40+
if (this != &rhs)
41+
m_opaque_up = clone(rhs.m_opaque_up);
4742
return *this;
4843
}
4944

‎lldb/source/API/SBDeclaration.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBDeclaration.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBStream.h"
1112
#include "lldb/Host/PosixApi.h"
1213
#include "lldb/Symbol/Declaration.h"
@@ -21,23 +22,18 @@ using namespace lldb_private;
2122
SBDeclaration::SBDeclaration() : m_opaque_up() {}
2223

2324
SBDeclaration::SBDeclaration(const SBDeclaration &rhs) : m_opaque_up() {
24-
if (rhs.IsValid())
25-
ref() = rhs.ref();
25+
m_opaque_up = clone(rhs.m_opaque_up);
2626
}
2727

2828
SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr)
2929
: m_opaque_up() {
3030
if (lldb_object_ptr)
31-
ref() = *lldb_object_ptr;
31+
m_opaque_up = llvm::make_unique<Declaration>(*lldb_object_ptr);
3232
}
3333

3434
const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) {
35-
if (this != &rhs) {
36-
if (rhs.IsValid())
37-
ref() = rhs.ref();
38-
else
39-
m_opaque_up.reset();
40-
}
35+
if (this != &rhs)
36+
m_opaque_up = clone(rhs.m_opaque_up);
4137
return *this;
4238
}
4339

‎lldb/source/API/SBError.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBError.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBStream.h"
1112
#include "lldb/Utility/Log.h"
1213
#include "lldb/Utility/Status.h"
@@ -19,21 +20,14 @@ using namespace lldb_private;
1920
SBError::SBError() : m_opaque_up() {}
2021

2122
SBError::SBError(const SBError &rhs) : m_opaque_up() {
22-
if (rhs.IsValid())
23-
m_opaque_up.reset(new Status(*rhs));
23+
m_opaque_up = clone(rhs.m_opaque_up);
2424
}
2525

2626
SBError::~SBError() {}
2727

2828
const SBError &SBError::operator=(const SBError &rhs) {
29-
if (rhs.IsValid()) {
30-
if (m_opaque_up)
31-
*m_opaque_up = *rhs;
32-
else
33-
m_opaque_up.reset(new Status(*rhs));
34-
} else
35-
m_opaque_up.reset();
36-
29+
if (this != &rhs)
30+
m_opaque_up = clone(rhs.m_opaque_up);
3731
return *this;
3832
}
3933

‎lldb/source/API/SBExpressionOptions.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "lldb/API/SBExpressionOptions.h"
11+
#include "Utils.h"
1112
#include "lldb/API/SBStream.h"
12-
1313
#include "lldb/Target/Target.h"
1414

1515
using namespace lldb;
@@ -18,16 +18,15 @@ using namespace lldb_private;
1818
SBExpressionOptions::SBExpressionOptions()
1919
: m_opaque_up(new EvaluateExpressionOptions()) {}
2020

21-
SBExpressionOptions::SBExpressionOptions(const SBExpressionOptions &rhs) {
22-
m_opaque_up.reset(new EvaluateExpressionOptions());
23-
*(m_opaque_up.get()) = rhs.ref();
21+
SBExpressionOptions::SBExpressionOptions(const SBExpressionOptions &rhs)
22+
: m_opaque_up() {
23+
m_opaque_up = clone(rhs.m_opaque_up);
2424
}
2525

2626
const SBExpressionOptions &SBExpressionOptions::
2727
operator=(const SBExpressionOptions &rhs) {
28-
if (this != &rhs) {
29-
this->ref() = rhs.ref();
30-
}
28+
if (this != &rhs)
29+
m_opaque_up = clone(rhs.m_opaque_up);
3130
return *this;
3231
}
3332

‎lldb/source/API/SBFileSpec.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <inttypes.h>
1010
#include <limits.h>
1111

12+
#include "Utils.h"
1213
#include "lldb/API/SBFileSpec.h"
1314
#include "lldb/API/SBStream.h"
1415
#include "lldb/Host/FileSystem.h"
@@ -24,8 +25,9 @@ using namespace lldb_private;
2425

2526
SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {}
2627

27-
SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
28-
: m_opaque_up(new lldb_private::FileSpec(*rhs.m_opaque_up)) {}
28+
SBFileSpec::SBFileSpec(const SBFileSpec &rhs) : m_opaque_up() {
29+
m_opaque_up = clone(rhs.m_opaque_up);
30+
}
2931

3032
SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
3133
: m_opaque_up(new lldb_private::FileSpec(fspec)) {}
@@ -45,7 +47,7 @@ SBFileSpec::~SBFileSpec() {}
4547

4648
const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
4749
if (this != &rhs)
48-
*m_opaque_up = *rhs.m_opaque_up;
50+
m_opaque_up = clone(rhs.m_opaque_up);
4951
return *this;
5052
}
5153

‎lldb/source/API/SBFileSpecList.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <limits.h>
1010

11+
#include "Utils.h"
1112
#include "lldb/API/SBFileSpec.h"
1213
#include "lldb/API/SBFileSpecList.h"
1314
#include "lldb/API/SBStream.h"
@@ -25,8 +26,7 @@ SBFileSpecList::SBFileSpecList() : m_opaque_up(new FileSpecList()) {}
2526
SBFileSpecList::SBFileSpecList(const SBFileSpecList &rhs) : m_opaque_up() {
2627
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
2728

28-
if (rhs.m_opaque_up)
29-
m_opaque_up.reset(new FileSpecList(*(rhs.get())));
29+
m_opaque_up = clone(rhs.m_opaque_up);
3030

3131
if (log) {
3232
log->Printf("SBFileSpecList::SBFileSpecList (const SBFileSpecList "
@@ -39,9 +39,8 @@ SBFileSpecList::SBFileSpecList(const SBFileSpecList &rhs) : m_opaque_up() {
3939
SBFileSpecList::~SBFileSpecList() {}
4040

4141
const SBFileSpecList &SBFileSpecList::operator=(const SBFileSpecList &rhs) {
42-
if (this != &rhs) {
43-
m_opaque_up.reset(new lldb_private::FileSpecList(*(rhs.get())));
44-
}
42+
if (this != &rhs)
43+
m_opaque_up = clone(rhs.m_opaque_up);
4544
return *this;
4645
}
4746

‎lldb/source/API/SBFrame.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "lldb/lldb-types.h"
1616

1717
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
18+
#include "Utils.h"
1819
#include "lldb/Core/Address.h"
1920
#include "lldb/Core/StreamFile.h"
2021
#include "lldb/Core/ValueObjectRegister.h"
@@ -68,14 +69,15 @@ SBFrame::SBFrame(const StackFrameSP &lldb_object_sp)
6869
}
6970
}
7071

71-
SBFrame::SBFrame(const SBFrame &rhs)
72-
: m_opaque_sp(new ExecutionContextRef(*rhs.m_opaque_sp)) {}
72+
SBFrame::SBFrame(const SBFrame &rhs) : m_opaque_sp() {
73+
m_opaque_sp = clone(rhs.m_opaque_sp);
74+
}
7375

7476
SBFrame::~SBFrame() = default;
7577

7678
const SBFrame &SBFrame::operator=(const SBFrame &rhs) {
7779
if (this != &rhs)
78-
*m_opaque_sp = *rhs.m_opaque_sp;
80+
m_opaque_sp = clone(rhs.m_opaque_sp);
7981
return *this;
8082
}
8183

‎lldb/source/API/SBLineEntry.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,39 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <limits.h>
10-
119
#include "lldb/API/SBLineEntry.h"
10+
#include "Utils.h"
1211
#include "lldb/API/SBStream.h"
1312
#include "lldb/Host/PosixApi.h"
1413
#include "lldb/Symbol/LineEntry.h"
1514
#include "lldb/Utility/Log.h"
1615
#include "lldb/Utility/StreamString.h"
1716

17+
#include <limits.h>
18+
1819
using namespace lldb;
1920
using namespace lldb_private;
2021

2122
SBLineEntry::SBLineEntry() : m_opaque_up() {}
2223

2324
SBLineEntry::SBLineEntry(const SBLineEntry &rhs) : m_opaque_up() {
24-
if (rhs.IsValid())
25-
ref() = rhs.ref();
25+
m_opaque_up = clone(rhs.m_opaque_up);
2626
}
2727

2828
SBLineEntry::SBLineEntry(const lldb_private::LineEntry *lldb_object_ptr)
2929
: m_opaque_up() {
3030
if (lldb_object_ptr)
31-
ref() = *lldb_object_ptr;
31+
m_opaque_up = llvm::make_unique<LineEntry>(*lldb_object_ptr);
3232
}
3333

3434
const SBLineEntry &SBLineEntry::operator=(const SBLineEntry &rhs) {
35-
if (this != &rhs) {
36-
if (rhs.IsValid())
37-
ref() = rhs.ref();
38-
else
39-
m_opaque_up.reset();
40-
}
35+
if (this != &rhs)
36+
m_opaque_up = clone(rhs.m_opaque_up);
4137
return *this;
4238
}
4339

4440
void SBLineEntry::SetLineEntry(const lldb_private::LineEntry &lldb_object_ref) {
45-
ref() = lldb_object_ref;
41+
m_opaque_up = llvm::make_unique<LineEntry>(lldb_object_ref);
4642
}
4743

4844
SBLineEntry::~SBLineEntry() {}

‎lldb/source/API/SBMemoryRegionInfo.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBMemoryRegionInfo.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBDefines.h"
1112
#include "lldb/API/SBError.h"
1213
#include "lldb/API/SBStream.h"
@@ -26,15 +27,14 @@ SBMemoryRegionInfo::SBMemoryRegionInfo(const MemoryRegionInfo *lldb_object_ptr)
2627
}
2728

2829
SBMemoryRegionInfo::SBMemoryRegionInfo(const SBMemoryRegionInfo &rhs)
29-
: m_opaque_up(new MemoryRegionInfo()) {
30-
ref() = rhs.ref();
30+
: m_opaque_up() {
31+
m_opaque_up = clone(rhs.m_opaque_up);
3132
}
3233

3334
const SBMemoryRegionInfo &SBMemoryRegionInfo::
3435
operator=(const SBMemoryRegionInfo &rhs) {
35-
if (this != &rhs) {
36-
ref() = rhs.ref();
37-
}
36+
if (this != &rhs)
37+
m_opaque_up = clone(rhs.m_opaque_up);
3838
return *this;
3939
}
4040

‎lldb/source/API/SBModuleSpec.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBModuleSpec.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBStream.h"
1112
#include "lldb/Core/Module.h"
1213
#include "lldb/Core/ModuleSpec.h"
@@ -19,12 +20,13 @@ using namespace lldb_private;
1920

2021
SBModuleSpec::SBModuleSpec() : m_opaque_up(new lldb_private::ModuleSpec()) {}
2122

22-
SBModuleSpec::SBModuleSpec(const SBModuleSpec &rhs)
23-
: m_opaque_up(new lldb_private::ModuleSpec(*rhs.m_opaque_up)) {}
23+
SBModuleSpec::SBModuleSpec(const SBModuleSpec &rhs) : m_opaque_up() {
24+
m_opaque_up = clone(rhs.m_opaque_up);
25+
}
2426

2527
const SBModuleSpec &SBModuleSpec::operator=(const SBModuleSpec &rhs) {
2628
if (this != &rhs)
27-
*m_opaque_up = *(rhs.m_opaque_up);
29+
m_opaque_up = clone(rhs.m_opaque_up);
2830
return *this;
2931
}
3032

‎lldb/source/API/SBProcessInfo.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBProcessInfo.h"
10-
10+
#include "Utils.h"
1111
#include "lldb/API/SBFileSpec.h"
1212
#include "lldb/Utility/ProcessInfo.h"
1313

@@ -17,20 +17,14 @@ using namespace lldb_private;
1717
SBProcessInfo::SBProcessInfo() : m_opaque_up() {}
1818

1919
SBProcessInfo::SBProcessInfo(const SBProcessInfo &rhs) : m_opaque_up() {
20-
if (rhs.IsValid()) {
21-
ref() = *rhs.m_opaque_up;
22-
}
20+
m_opaque_up = clone(rhs.m_opaque_up);
2321
}
2422

2523
SBProcessInfo::~SBProcessInfo() {}
2624

2725
SBProcessInfo &SBProcessInfo::operator=(const SBProcessInfo &rhs) {
28-
if (this != &rhs) {
29-
if (rhs.IsValid())
30-
ref() = *rhs.m_opaque_up;
31-
else
32-
m_opaque_up.reset();
33-
}
26+
if (this != &rhs)
27+
m_opaque_up = clone(rhs.m_opaque_up);
3428
return *this;
3529
}
3630

‎lldb/source/API/SBStringList.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBStringList.h"
10-
10+
#include "Utils.h"
1111
#include "lldb/Utility/StringList.h"
1212

1313
using namespace lldb;
@@ -18,21 +18,16 @@ SBStringList::SBStringList() : m_opaque_up() {}
1818
SBStringList::SBStringList(const lldb_private::StringList *lldb_strings_ptr)
1919
: m_opaque_up() {
2020
if (lldb_strings_ptr)
21-
m_opaque_up.reset(new lldb_private::StringList(*lldb_strings_ptr));
21+
m_opaque_up = llvm::make_unique<StringList>(*lldb_strings_ptr);
2222
}
2323

2424
SBStringList::SBStringList(const SBStringList &rhs) : m_opaque_up() {
25-
if (rhs.IsValid())
26-
m_opaque_up.reset(new lldb_private::StringList(*rhs));
25+
m_opaque_up = clone(rhs.m_opaque_up);
2726
}
2827

2928
const SBStringList &SBStringList::operator=(const SBStringList &rhs) {
30-
if (this != &rhs) {
31-
if (rhs.IsValid())
32-
m_opaque_up.reset(new lldb_private::StringList(*rhs));
33-
else
34-
m_opaque_up.reset();
35-
}
29+
if (this != &rhs)
30+
m_opaque_up = clone(rhs.m_opaque_up);
3631
return *this;
3732
}
3833

‎lldb/source/API/SBSymbolContext.cpp

+9-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBSymbolContext.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBStream.h"
1112
#include "lldb/Core/Module.h"
1213
#include "lldb/Symbol/Function.h"
@@ -21,38 +22,26 @@ SBSymbolContext::SBSymbolContext() : m_opaque_up() {}
2122

2223
SBSymbolContext::SBSymbolContext(const SymbolContext *sc_ptr) : m_opaque_up() {
2324
if (sc_ptr)
24-
m_opaque_up.reset(new SymbolContext(*sc_ptr));
25+
m_opaque_up = llvm::make_unique<SymbolContext>(*sc_ptr);
2526
}
2627

2728
SBSymbolContext::SBSymbolContext(const SBSymbolContext &rhs) : m_opaque_up() {
28-
if (rhs.IsValid()) {
29-
if (m_opaque_up)
30-
*m_opaque_up = *rhs.m_opaque_up;
31-
else
32-
ref() = *rhs.m_opaque_up;
33-
}
29+
m_opaque_up = clone(rhs.m_opaque_up);
3430
}
3531

3632
SBSymbolContext::~SBSymbolContext() {}
3733

3834
const SBSymbolContext &SBSymbolContext::operator=(const SBSymbolContext &rhs) {
39-
if (this != &rhs) {
40-
if (rhs.IsValid())
41-
m_opaque_up.reset(new lldb_private::SymbolContext(*rhs.m_opaque_up));
42-
}
35+
if (this != &rhs)
36+
m_opaque_up = clone(rhs.m_opaque_up);
4337
return *this;
4438
}
4539

4640
void SBSymbolContext::SetSymbolContext(const SymbolContext *sc_ptr) {
47-
if (sc_ptr) {
48-
if (m_opaque_up)
49-
*m_opaque_up = *sc_ptr;
50-
else
51-
m_opaque_up.reset(new SymbolContext(*sc_ptr));
52-
} else {
53-
if (m_opaque_up)
54-
m_opaque_up->Clear(true);
55-
}
41+
if (sc_ptr)
42+
m_opaque_up = llvm::make_unique<SymbolContext>(*sc_ptr);
43+
else
44+
m_opaque_up->Clear(true);
5645
}
5746

5847
bool SBSymbolContext::IsValid() const { return m_opaque_up != NULL; }

‎lldb/source/API/SBSymbolContextList.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBSymbolContextList.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBStream.h"
1112
#include "lldb/Symbol/SymbolContext.h"
1213

@@ -17,15 +18,17 @@ SBSymbolContextList::SBSymbolContextList()
1718
: m_opaque_up(new SymbolContextList()) {}
1819

1920
SBSymbolContextList::SBSymbolContextList(const SBSymbolContextList &rhs)
20-
: m_opaque_up(new SymbolContextList(*rhs.m_opaque_up)) {}
21+
: m_opaque_up() {
22+
23+
m_opaque_up = clone(rhs.m_opaque_up);
24+
}
2125

2226
SBSymbolContextList::~SBSymbolContextList() {}
2327

2428
const SBSymbolContextList &SBSymbolContextList::
2529
operator=(const SBSymbolContextList &rhs) {
26-
if (this != &rhs) {
27-
*m_opaque_up = *rhs.m_opaque_up;
28-
}
30+
if (this != &rhs)
31+
m_opaque_up = clone(rhs.m_opaque_up);
2932
return *this;
3033
}
3134

‎lldb/source/API/SBThread.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBThread.h"
10-
10+
#include "Utils.h"
11+
#include "lldb/API/SBAddress.h"
12+
#include "lldb/API/SBDebugger.h"
13+
#include "lldb/API/SBEvent.h"
1114
#include "lldb/API/SBFileSpec.h"
15+
#include "lldb/API/SBFrame.h"
16+
#include "lldb/API/SBProcess.h"
1217
#include "lldb/API/SBStream.h"
1318
#include "lldb/API/SBSymbolContext.h"
19+
#include "lldb/API/SBThreadCollection.h"
20+
#include "lldb/API/SBThreadPlan.h"
21+
#include "lldb/API/SBValue.h"
1422
#include "lldb/Breakpoint/BreakpointLocation.h"
1523
#include "lldb/Core/Debugger.h"
1624
#include "lldb/Core/StreamFile.h"
@@ -33,15 +41,6 @@
3341
#include "lldb/Utility/State.h"
3442
#include "lldb/Utility/Stream.h"
3543
#include "lldb/Utility/StructuredData.h"
36-
37-
#include "lldb/API/SBAddress.h"
38-
#include "lldb/API/SBDebugger.h"
39-
#include "lldb/API/SBEvent.h"
40-
#include "lldb/API/SBFrame.h"
41-
#include "lldb/API/SBProcess.h"
42-
#include "lldb/API/SBThreadCollection.h"
43-
#include "lldb/API/SBThreadPlan.h"
44-
#include "lldb/API/SBValue.h"
4544
#include "lldb/lldb-enumerations.h"
4645

4746
#include <memory>
@@ -61,16 +60,17 @@ SBThread::SBThread() : m_opaque_sp(new ExecutionContextRef()) {}
6160
SBThread::SBThread(const ThreadSP &lldb_object_sp)
6261
: m_opaque_sp(new ExecutionContextRef(lldb_object_sp)) {}
6362

64-
SBThread::SBThread(const SBThread &rhs)
65-
: m_opaque_sp(new ExecutionContextRef(*rhs.m_opaque_sp)) {}
63+
SBThread::SBThread(const SBThread &rhs) : m_opaque_sp() {
64+
m_opaque_sp = clone(rhs.m_opaque_sp);
65+
}
6666

6767
//----------------------------------------------------------------------
6868
// Assignment operator
6969
//----------------------------------------------------------------------
7070

7171
const lldb::SBThread &SBThread::operator=(const SBThread &rhs) {
7272
if (this != &rhs)
73-
*m_opaque_sp = *rhs.m_opaque_sp;
73+
m_opaque_sp = clone(rhs.m_opaque_sp);
7474
return *this;
7575
}
7676

‎lldb/source/API/SBTypeEnumMember.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBTypeEnumMember.h"
10+
#include "Utils.h"
1011
#include "lldb/API/SBDefines.h"
1112
#include "lldb/API/SBStream.h"
1213
#include "lldb/API/SBType.h"
@@ -22,23 +23,19 @@ using namespace lldb_private;
2223
SBTypeEnumMember::SBTypeEnumMember() : m_opaque_sp() {}
2324

2425
SBTypeEnumMember::~SBTypeEnumMember() {}
26+
2527
SBTypeEnumMember::SBTypeEnumMember(
2628
const lldb::TypeEnumMemberImplSP &enum_member_sp)
2729
: m_opaque_sp(enum_member_sp) {}
2830

2931
SBTypeEnumMember::SBTypeEnumMember(const SBTypeEnumMember &rhs)
3032
: m_opaque_sp() {
31-
if (this != &rhs) {
32-
if (rhs.IsValid())
33-
m_opaque_sp = std::make_shared<TypeEnumMemberImpl>(rhs.ref());
34-
}
33+
m_opaque_sp = clone(rhs.m_opaque_sp);
3534
}
3635

3736
SBTypeEnumMember &SBTypeEnumMember::operator=(const SBTypeEnumMember &rhs) {
38-
if (this != &rhs) {
39-
if (rhs.IsValid())
40-
m_opaque_sp = std::make_shared<TypeEnumMemberImpl>(rhs.ref());
41-
}
37+
if (this != &rhs)
38+
m_opaque_sp = clone(rhs.m_opaque_sp);
4239
return *this;
4340
}
4441

‎lldb/source/API/SBTypeSummary.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "lldb/API/SBTypeSummary.h"
11+
#include "Utils.h"
1112
#include "lldb/API/SBStream.h"
1213
#include "lldb/API/SBValue.h"
1314
#include "lldb/DataFormatters/DataVisualization.h"
@@ -17,16 +18,12 @@
1718
using namespace lldb;
1819
using namespace lldb_private;
1920

20-
SBTypeSummaryOptions::SBTypeSummaryOptions() {
21-
m_opaque_up.reset(new TypeSummaryOptions());
22-
}
21+
SBTypeSummaryOptions::SBTypeSummaryOptions()
22+
: m_opaque_up(new TypeSummaryOptions()) {}
2323

2424
SBTypeSummaryOptions::SBTypeSummaryOptions(
2525
const lldb::SBTypeSummaryOptions &rhs) {
26-
if (rhs.m_opaque_up)
27-
m_opaque_up.reset(new TypeSummaryOptions(*rhs.m_opaque_up));
28-
else
29-
m_opaque_up.reset(new TypeSummaryOptions());
26+
m_opaque_up = clone(rhs.m_opaque_up);
3027
}
3128

3229
SBTypeSummaryOptions::~SBTypeSummaryOptions() {}

‎lldb/source/API/Utils.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Utils.h -------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_API_UTILS_H
10+
#define LLDB_API_UTILS_H
11+
12+
#include "llvm/ADT/STLExtras.h"
13+
#include <memory>
14+
15+
namespace lldb_private {
16+
17+
template <typename T> std::unique_ptr<T> clone(const std::unique_ptr<T> &src) {
18+
if (src)
19+
return llvm::make_unique<T>(*src);
20+
return nullptr;
21+
}
22+
23+
template <typename T> std::shared_ptr<T> clone(const std::shared_ptr<T> &src) {
24+
if (src)
25+
return std::make_shared<T>(*src);
26+
return nullptr;
27+
}
28+
29+
} // namespace lldb_private
30+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.