Skip to content

Commit 076275f

Browse files
JDevliegheremahesh-attarde
authored andcommitted
[lldb] Use std::make_shared where possible (NFC) (llvm#150714)
This is a continuation of 68fd102, which did the same thing but only for StopInfo. Using make_shared is both safer and more efficient: - With make_shared, the object and the control block are allocated together, which is more efficient. - With make_shared, the enable_shared_from_this base class is properly linked to the control block before the constructor finishes, so shared_from_this() will be safe to use (though still not recommended during construction).
1 parent de5bbe0 commit 076275f

File tree

28 files changed

+137
-143
lines changed

28 files changed

+137
-143
lines changed

lldb/include/lldb/Target/Process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2619,7 +2619,7 @@ void PruneThreadPlans();
26192619

26202620
void ResetExtendedCrashInfoDict() {
26212621
// StructuredData::Dictionary is add only, so we have to make a new one:
2622-
m_crash_info_dict_sp.reset(new StructuredData::Dictionary());
2622+
m_crash_info_dict_sp = std::make_shared<StructuredData::Dictionary>();
26232623
}
26242624

26252625
size_t AddImageToken(lldb::addr_t image_ptr);

lldb/source/API/SBType.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -184,57 +184,57 @@ SBType SBType::GetPointerType() {
184184
if (!IsValid())
185185
return SBType();
186186

187-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointerType())));
187+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetPointerType()));
188188
}
189189

190190
SBType SBType::GetPointeeType() {
191191
LLDB_INSTRUMENT_VA(this);
192192

193193
if (!IsValid())
194194
return SBType();
195-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointeeType())));
195+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetPointeeType()));
196196
}
197197

198198
SBType SBType::GetReferenceType() {
199199
LLDB_INSTRUMENT_VA(this);
200200

201201
if (!IsValid())
202202
return SBType();
203-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetReferenceType())));
203+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetReferenceType()));
204204
}
205205

206206
SBType SBType::GetTypedefedType() {
207207
LLDB_INSTRUMENT_VA(this);
208208

209209
if (!IsValid())
210210
return SBType();
211-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetTypedefedType())));
211+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetTypedefedType()));
212212
}
213213

214214
SBType SBType::GetDereferencedType() {
215215
LLDB_INSTRUMENT_VA(this);
216216

217217
if (!IsValid())
218218
return SBType();
219-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetDereferencedType())));
219+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetDereferencedType()));
220220
}
221221

222222
SBType SBType::GetArrayElementType() {
223223
LLDB_INSTRUMENT_VA(this);
224224

225225
if (!IsValid())
226226
return SBType();
227-
return SBType(TypeImplSP(new TypeImpl(
228-
m_opaque_sp->GetCompilerType(true).GetArrayElementType(nullptr))));
227+
return SBType(std::make_shared<TypeImpl>(
228+
m_opaque_sp->GetCompilerType(true).GetArrayElementType(nullptr)));
229229
}
230230

231231
SBType SBType::GetArrayType(uint64_t size) {
232232
LLDB_INSTRUMENT_VA(this, size);
233233

234234
if (!IsValid())
235235
return SBType();
236-
return SBType(TypeImplSP(
237-
new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayType(size))));
236+
return SBType(std::make_shared<TypeImpl>(
237+
m_opaque_sp->GetCompilerType(true).GetArrayType(size)));
238238
}
239239

240240
SBType SBType::GetVectorElementType() {
@@ -245,7 +245,7 @@ SBType SBType::GetVectorElementType() {
245245
CompilerType vector_element_type;
246246
if (m_opaque_sp->GetCompilerType(true).IsVectorType(&vector_element_type,
247247
nullptr))
248-
type_sb.SetSP(TypeImplSP(new TypeImpl(vector_element_type)));
248+
type_sb.SetSP(std::make_shared<TypeImpl>(vector_element_type));
249249
}
250250
return type_sb;
251251
}
@@ -421,14 +421,14 @@ lldb::SBType SBType::GetUnqualifiedType() {
421421

422422
if (!IsValid())
423423
return SBType();
424-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetUnqualifiedType())));
424+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetUnqualifiedType()));
425425
}
426426

427427
lldb::SBType SBType::GetCanonicalType() {
428428
LLDB_INSTRUMENT_VA(this);
429429

430430
if (IsValid())
431-
return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetCanonicalType())));
431+
return SBType(std::make_shared<TypeImpl>(m_opaque_sp->GetCanonicalType()));
432432
return SBType();
433433
}
434434

@@ -508,7 +508,7 @@ SBTypeMember SBType::GetDirectBaseClassAtIndex(uint32_t idx) {
508508
idx, &bit_offset);
509509
if (base_class_type.IsValid())
510510
sb_type_member.reset(new TypeMemberImpl(
511-
TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
511+
std::make_shared<TypeImpl>(base_class_type), bit_offset));
512512
}
513513
return sb_type_member;
514514
}
@@ -524,7 +524,7 @@ SBTypeMember SBType::GetVirtualBaseClassAtIndex(uint32_t idx) {
524524
idx, &bit_offset);
525525
if (base_class_type.IsValid())
526526
sb_type_member.reset(new TypeMemberImpl(
527-
TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
527+
std::make_shared<TypeImpl>(base_class_type), bit_offset));
528528
}
529529
return sb_type_member;
530530
}
@@ -546,16 +546,15 @@ SBTypeEnumMemberList SBType::GetEnumMembers() {
546546
if (IsValid()) {
547547
CompilerType this_type(m_opaque_sp->GetCompilerType(true));
548548
if (this_type.IsValid()) {
549-
this_type.ForEachEnumerator([&sb_enum_member_list](
550-
const CompilerType &integer_type,
551-
ConstString name,
552-
const llvm::APSInt &value) -> bool {
553-
SBTypeEnumMember enum_member(
554-
lldb::TypeEnumMemberImplSP(new TypeEnumMemberImpl(
555-
lldb::TypeImplSP(new TypeImpl(integer_type)), name, value)));
556-
sb_enum_member_list.Append(enum_member);
557-
return true; // Keep iterating
558-
});
549+
this_type.ForEachEnumerator(
550+
[&sb_enum_member_list](const CompilerType &integer_type,
551+
ConstString name,
552+
const llvm::APSInt &value) -> bool {
553+
SBTypeEnumMember enum_member(std::make_shared<TypeEnumMemberImpl>(
554+
std::make_shared<TypeImpl>(integer_type), name, value));
555+
sb_enum_member_list.Append(enum_member);
556+
return true; // Keep iterating
557+
});
559558
}
560559
}
561560
return sb_enum_member_list;
@@ -578,9 +577,9 @@ SBTypeMember SBType::GetFieldAtIndex(uint32_t idx) {
578577
ConstString name;
579578
if (!name_sstr.empty())
580579
name.SetCString(name_sstr.c_str());
581-
sb_type_member.reset(
582-
new TypeMemberImpl(TypeImplSP(new TypeImpl(field_type)), bit_offset,
583-
name, bitfield_bit_size, is_bitfield));
580+
sb_type_member.reset(new TypeMemberImpl(
581+
std::make_shared<TypeImpl>(field_type), bit_offset, name,
582+
bitfield_bit_size, is_bitfield));
584583
}
585584
}
586585
}
@@ -978,7 +977,7 @@ SBType SBTypeMemberFunction::GetType() {
978977

979978
SBType sb_type;
980979
if (m_opaque_sp) {
981-
sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetType())));
980+
sb_type.SetSP(std::make_shared<TypeImpl>(m_opaque_sp->GetType()));
982981
}
983982
return sb_type;
984983
}
@@ -988,7 +987,7 @@ lldb::SBType SBTypeMemberFunction::GetReturnType() {
988987

989988
SBType sb_type;
990989
if (m_opaque_sp) {
991-
sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetReturnType())));
990+
sb_type.SetSP(std::make_shared<TypeImpl>(m_opaque_sp->GetReturnType()));
992991
}
993992
return sb_type;
994993
}
@@ -1007,7 +1006,7 @@ lldb::SBType SBTypeMemberFunction::GetArgumentTypeAtIndex(uint32_t i) {
10071006
SBType sb_type;
10081007
if (m_opaque_sp) {
10091008
sb_type.SetSP(
1010-
lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetArgumentAtIndex(i))));
1009+
std::make_shared<TypeImpl>(m_opaque_sp->GetArgumentAtIndex(i)));
10111010
}
10121011
return sb_type;
10131012
}

lldb/source/API/SBTypeFilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using namespace lldb_private;
1919
SBTypeFilter::SBTypeFilter() { LLDB_INSTRUMENT_VA(this); }
2020

2121
SBTypeFilter::SBTypeFilter(uint32_t options)
22-
: m_opaque_sp(TypeFilterImplSP(new TypeFilterImpl(options))) {
22+
: m_opaque_sp(std::make_shared<TypeFilterImpl>(options)) {
2323
LLDB_INSTRUMENT_VA(this, options);
2424
}
2525

lldb/source/API/SBTypeNameSpecifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ SBTypeNameSpecifier::SBTypeNameSpecifier(SBType type) {
3838
LLDB_INSTRUMENT_VA(this, type);
3939

4040
if (type.IsValid())
41-
m_opaque_sp = TypeNameSpecifierImplSP(
42-
new TypeNameSpecifierImpl(type.m_opaque_sp->GetCompilerType(true)));
41+
m_opaque_sp = std::make_shared<TypeNameSpecifierImpl>(
42+
type.m_opaque_sp->GetCompilerType(true));
4343
}
4444

4545
SBTypeNameSpecifier::SBTypeNameSpecifier(const lldb::SBTypeNameSpecifier &rhs)

lldb/source/API/SBTypeSynthetic.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ SBTypeSynthetic SBTypeSynthetic::CreateWithClassName(const char *data,
2424

2525
if (!data || data[0] == 0)
2626
return SBTypeSynthetic();
27-
return SBTypeSynthetic(ScriptedSyntheticChildrenSP(
28-
new ScriptedSyntheticChildren(options, data, "")));
27+
return SBTypeSynthetic(
28+
std::make_shared<ScriptedSyntheticChildren>(options, data, ""));
2929
}
3030

3131
SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
@@ -34,8 +34,8 @@ SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
3434

3535
if (!data || data[0] == 0)
3636
return SBTypeSynthetic();
37-
return SBTypeSynthetic(ScriptedSyntheticChildrenSP(
38-
new ScriptedSyntheticChildren(options, "", data)));
37+
return SBTypeSynthetic(
38+
std::make_shared<ScriptedSyntheticChildren>(options, "", data));
3939
}
4040

4141
SBTypeSynthetic::SBTypeSynthetic(const lldb::SBTypeSynthetic &rhs)

lldb/source/API/SBValue.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,11 +1120,11 @@ void SBValue::SetSP(const lldb::ValueObjectSP &sp) {
11201120
lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue();
11211121
bool use_synthetic =
11221122
target_sp->TargetProperties::GetEnableSyntheticValue();
1123-
m_opaque_sp = ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic));
1123+
m_opaque_sp = std::make_shared<ValueImpl>(sp, use_dynamic, use_synthetic);
11241124
} else
1125-
m_opaque_sp = ValueImplSP(new ValueImpl(sp, eNoDynamicValues, true));
1125+
m_opaque_sp = std::make_shared<ValueImpl>(sp, eNoDynamicValues, true);
11261126
} else
1127-
m_opaque_sp = ValueImplSP(new ValueImpl(sp, eNoDynamicValues, false));
1127+
m_opaque_sp = std::make_shared<ValueImpl>(sp, eNoDynamicValues, false);
11281128
}
11291129

11301130
void SBValue::SetSP(const lldb::ValueObjectSP &sp,
@@ -1155,14 +1155,14 @@ void SBValue::SetSP(const lldb::ValueObjectSP &sp, bool use_synthetic) {
11551155

11561156
void SBValue::SetSP(const lldb::ValueObjectSP &sp,
11571157
lldb::DynamicValueType use_dynamic, bool use_synthetic) {
1158-
m_opaque_sp = ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic));
1158+
m_opaque_sp = std::make_shared<ValueImpl>(sp, use_dynamic, use_synthetic);
11591159
}
11601160

11611161
void SBValue::SetSP(const lldb::ValueObjectSP &sp,
11621162
lldb::DynamicValueType use_dynamic, bool use_synthetic,
11631163
const char *name) {
11641164
m_opaque_sp =
1165-
ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic, name));
1165+
std::make_shared<ValueImpl>(sp, use_dynamic, use_synthetic, name);
11661166
}
11671167

11681168
bool SBValue::GetExpressionPath(SBStream &description) {

lldb/source/Breakpoint/Breakpoint.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ StructuredData::ObjectSP Breakpoint::SerializeToStructuredData() {
8383
if (!m_name_list.empty()) {
8484
StructuredData::ArraySP names_array_sp(new StructuredData::Array());
8585
for (auto name : m_name_list) {
86-
names_array_sp->AddItem(
87-
StructuredData::StringSP(new StructuredData::String(name)));
86+
names_array_sp->AddItem(std::make_shared<StructuredData::String>(name));
8887
}
8988
breakpoint_contents_sp->AddItem(Breakpoint::GetKey(OptionNames::Names),
9089
names_array_sp);

lldb/source/Breakpoint/BreakpointResolverName.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
195195
StructuredData::ArraySP names_sp(new StructuredData::Array());
196196
StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
197197
for (auto lookup : m_lookups) {
198-
names_sp->AddItem(StructuredData::StringSP(
199-
new StructuredData::String(lookup.GetName().GetStringRef())));
200-
name_masks_sp->AddItem(StructuredData::UnsignedIntegerSP(
201-
new StructuredData::UnsignedInteger(lookup.GetNameTypeMask())));
198+
names_sp->AddItem(std::make_shared<StructuredData::String>(
199+
lookup.GetName().GetStringRef()));
200+
name_masks_sp->AddItem(std::make_shared<StructuredData::UnsignedInteger>(
201+
lookup.GetNameTypeMask()));
202202
}
203203
options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
204204
options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lldb/Utility/Args.h"
2626
#include "lldb/Utility/StringList.h"
2727
#include "llvm/ADT/StringRef.h"
28+
#include <memory>
2829
#include <optional>
2930

3031
using namespace lldb;
@@ -467,7 +468,7 @@ other command as far as there is only one alias command match.");
467468
// Verify & handle any options/arguments passed to the alias command
468469

469470
OptionArgVectorSP option_arg_vector_sp =
470-
OptionArgVectorSP(new OptionArgVector);
471+
std::make_shared<OptionArgVector>();
471472

472473
const bool include_aliases = true;
473474
// Look up the command using command's name first. This is to resolve
@@ -543,7 +544,7 @@ other command as far as there is only one alias command match.");
543544
CommandObject *cmd_obj = command_obj_sp.get();
544545
CommandObject *sub_cmd_obj = nullptr;
545546
OptionArgVectorSP option_arg_vector_sp =
546-
OptionArgVectorSP(new OptionArgVector);
547+
std::make_shared<OptionArgVector>();
547548

548549
while (cmd_obj->IsMultiwordObject() && !args.empty()) {
549550
auto sub_command = args[0].ref();
@@ -2504,9 +2505,9 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
25042505

25052506
CommandObjectSP new_cmd_sp;
25062507
if (m_options.m_class_name.empty()) {
2507-
new_cmd_sp.reset(new CommandObjectPythonFunction(
2508+
new_cmd_sp = std::make_shared<CommandObjectPythonFunction>(
25082509
m_interpreter, m_cmd_name, m_options.m_funct_name,
2509-
m_options.m_short_help, m_synchronicity, m_completion_type));
2510+
m_options.m_short_help, m_synchronicity, m_completion_type);
25102511
} else {
25112512
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
25122513
if (!interpreter) {
@@ -2528,9 +2529,9 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
25282529
if (!result.Succeeded())
25292530
return;
25302531
} else
2531-
new_cmd_sp.reset(new CommandObjectScriptingObjectRaw(
2532+
new_cmd_sp = std::make_shared<CommandObjectScriptingObjectRaw>(
25322533
m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity,
2533-
m_completion_type));
2534+
m_completion_type);
25342535
}
25352536

25362537
// Assume we're going to succeed...

lldb/source/Commands/CommandObjectFrame.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,9 @@ void CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
901901
StackFrameRecognizerSP(new ScriptedStackFrameRecognizer(
902902
interpreter, m_options.m_class_name.c_str()));
903903
if (m_options.m_regex) {
904-
auto module =
905-
RegularExpressionSP(new RegularExpression(m_options.m_module));
904+
auto module = std::make_shared<RegularExpression>(m_options.m_module);
906905
auto func =
907-
RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
906+
std::make_shared<RegularExpression>(m_options.m_symbols.front());
908907
GetTarget().GetFrameRecognizerManager().AddRecognizer(
909908
recognizer_sp, module, func, Mangled::NamePreference::ePreferDemangled,
910909
m_options.m_first_instruction_only);

0 commit comments

Comments
 (0)