Skip to content

Commit df8835f

Browse files
Merge pull request #11052 from swiftlang/lldb/cherry-picks-to-6.2
🍒 [swift/release/6.2][lldb] Assortment of cherry-picks related to data-formatters
2 parents 923ab2d + 39c63d1 commit df8835f

File tree

40 files changed

+583
-472
lines changed

40 files changed

+583
-472
lines changed

lldb/include/lldb/DataFormatters/FormattersHelpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ void AddFilter(TypeCategoryImpl::SharedPointer category_sp,
6464

6565
size_t ExtractIndexFromString(const char *item_name);
6666

67+
/// Prints the summary for the pointer value of a C++
68+
/// std::unique_ptr/std::shared_ptr/std::weak_ptr.
69+
void DumpCxxSmartPtrPointerSummary(Stream &stream, ValueObject &ptr,
70+
const TypeSummaryOptions &options);
71+
6772
Address GetArrayAddressOrPointerValue(ValueObject &valobj);
6873

6974
time_t GetOSXEpoch();

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
152152
return;
153153
}
154154
}
155+
m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
156+
m_cmd_name);
155157
result.SetStatus(eReturnStatusSuccessFinishResult);
156158
};
157159

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
494494
return false;
495495
}
496496

497+
m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
498+
m_cmd_name);
499+
497500
if (suppress_result)
498501
if (auto result_var_sp =
499502
target.GetPersistentVariable(result_valobj_sp->GetName())) {

lldb/source/DataFormatters/FormattersHelpers.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,22 @@ lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
430430

431431
return data_addr;
432432
}
433+
434+
void lldb_private::formatters::DumpCxxSmartPtrPointerSummary(
435+
Stream &stream, ValueObject &ptr, const TypeSummaryOptions &options) {
436+
if (ptr.GetValueAsUnsigned(0) == 0) {
437+
stream.Printf("nullptr");
438+
return;
439+
}
440+
441+
Status error;
442+
ValueObjectSP pointee_sp = ptr.Dereference(error);
443+
if (!pointee_sp || !error.Success())
444+
return;
445+
446+
if (!pointee_sp->DumpPrintableRepresentation(
447+
stream, ValueObject::eValueObjectRepresentationStyleSummary,
448+
lldb::eFormatInvalid,
449+
ValueObject::PrintableRepresentationSpecialCases::eDisable, false))
450+
stream.Printf("ptr = 0x%" PRIx64, ptr.GetValueAsUnsigned(0));
451+
}

lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
1212
CPlusPlusLanguage.cpp
1313
CPlusPlusNameParser.cpp
1414
CxxStringTypes.cpp
15+
Generic.cpp
1516
GenericBitset.cpp
1617
GenericOptional.cpp
1718
LibCxx.cpp
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Generic.cpp ------------------------------------------------------===//
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+
#include "Generic.h"
10+
11+
lldb::ValueObjectSP lldb_private::formatters::GetDesugaredSmartPointerValue(
12+
ValueObject &ptr, ValueObject &container) {
13+
auto container_type = container.GetCompilerType().GetNonReferenceType();
14+
if (!container_type)
15+
return nullptr;
16+
17+
auto arg = container_type.GetTypeTemplateArgument(0);
18+
if (!arg)
19+
return nullptr;
20+
21+
return ptr.Cast(arg.GetPointerType());
22+
}

lldb/source/Plugins/Language/CPlusPlus/Generic.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace formatters {
1919
bool GenericOptionalSummaryProvider(ValueObject &valobj, Stream &stream,
2020
const TypeSummaryOptions &options);
2121

22+
/// Return the ValueObjectSP of the underlying pointer member whose type
23+
/// is a desugared 'std::shared_ptr::element_type *'.
24+
lldb::ValueObjectSP GetDesugaredSmartPointerValue(ValueObject &ptr,
25+
ValueObject &container);
26+
2227
} // namespace formatters
2328
} // namespace lldb_private
2429

lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "lldb/ValueObject/ValueObject.h"
2525
#include "lldb/ValueObject/ValueObjectConstResult.h"
2626

27+
#include "Plugins/Language/CPlusPlus/CxxStringTypes.h"
28+
#include "Plugins/Language/CPlusPlus/Generic.h"
2729
#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
2830
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
2931
#include "lldb/lldb-enumerations.h"
@@ -156,39 +158,43 @@ bool lldb_private::formatters::LibcxxSmartPointerSummaryProvider(
156158
ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
157159
if (!valobj_sp)
158160
return false;
159-
ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName("__ptr_"));
160-
ValueObjectSP count_sp(
161-
valobj_sp->GetChildAtNamePath({"__cntrl_", "__shared_owners_"}));
162-
ValueObjectSP weakcount_sp(
163-
valobj_sp->GetChildAtNamePath({"__cntrl_", "__shared_weak_owners_"}));
164161

165-
if (!ptr_sp)
162+
ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName("__ptr_"));
163+
ValueObjectSP ctrl_sp(valobj_sp->GetChildMemberWithName("__cntrl_"));
164+
if (!ctrl_sp || !ptr_sp)
166165
return false;
167166

168-
if (ptr_sp->GetValueAsUnsigned(0) == 0) {
169-
stream.Printf("nullptr");
167+
DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options);
168+
169+
bool success;
170+
uint64_t ctrl_addr = ctrl_sp->GetValueAsUnsigned(0, &success);
171+
// Empty control field. We're done.
172+
if (!success || ctrl_addr == 0)
170173
return true;
171-
} else {
172-
bool print_pointee = false;
173-
Status error;
174-
ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
175-
if (pointee_sp && error.Success()) {
176-
if (pointee_sp->DumpPrintableRepresentation(
177-
stream, ValueObject::eValueObjectRepresentationStyleSummary,
178-
lldb::eFormatInvalid,
179-
ValueObject::PrintableRepresentationSpecialCases::eDisable,
180-
false))
181-
print_pointee = true;
182-
}
183-
if (!print_pointee)
184-
stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
174+
175+
if (auto count_sp = ctrl_sp->GetChildMemberWithName("__shared_owners_")) {
176+
bool success;
177+
uint64_t count = count_sp->GetValueAsUnsigned(0, &success);
178+
if (!success)
179+
return false;
180+
181+
// std::shared_ptr releases the underlying resource when the
182+
// __shared_owners_ count hits -1. So `__shared_owners_ == 0` indicates 1
183+
// owner. Hence add +1 here.
184+
stream.Printf(" strong=%" PRIu64, count + 1);
185185
}
186186

187-
if (count_sp)
188-
stream.Printf(" strong=%" PRIu64, 1 + count_sp->GetValueAsUnsigned(0));
187+
if (auto weak_count_sp =
188+
ctrl_sp->GetChildMemberWithName("__shared_weak_owners_")) {
189+
bool success;
190+
uint64_t count = weak_count_sp->GetValueAsUnsigned(0, &success);
191+
if (!success)
192+
return false;
189193

190-
if (weakcount_sp)
191-
stream.Printf(" weak=%" PRIu64, 1 + weakcount_sp->GetValueAsUnsigned(0));
194+
// Unlike __shared_owners_, __shared_weak_owners_ indicates the exact
195+
// std::weak_ptr reference count.
196+
stream.Printf(" weak=%" PRIu64, count);
197+
}
192198

193199
return true;
194200
}
@@ -209,24 +215,7 @@ bool lldb_private::formatters::LibcxxUniquePointerSummaryProvider(
209215
if (!ptr_sp)
210216
return false;
211217

212-
if (ptr_sp->GetValueAsUnsigned(0) == 0) {
213-
stream.Printf("nullptr");
214-
return true;
215-
} else {
216-
bool print_pointee = false;
217-
Status error;
218-
ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
219-
if (pointee_sp && error.Success()) {
220-
if (pointee_sp->DumpPrintableRepresentation(
221-
stream, ValueObject::eValueObjectRepresentationStyleSummary,
222-
lldb::eFormatInvalid,
223-
ValueObject::PrintableRepresentationSpecialCases::eDisable,
224-
false))
225-
print_pointee = true;
226-
}
227-
if (!print_pointee)
228-
stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
229-
}
218+
DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options);
230219

231220
return true;
232221
}
@@ -250,7 +239,8 @@ lldb_private::formatters::LibCxxVectorIteratorSyntheticFrontEndCreator(
250239

251240
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
252241
LibcxxSharedPtrSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
253-
: SyntheticChildrenFrontEnd(*valobj_sp), m_cntrl(nullptr) {
242+
: SyntheticChildrenFrontEnd(*valobj_sp), m_cntrl(nullptr),
243+
m_ptr_obj(nullptr) {
254244
if (valobj_sp)
255245
Update();
256246
}
@@ -263,28 +253,21 @@ llvm::Expected<uint32_t> lldb_private::formatters::
263253
lldb::ValueObjectSP
264254
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex(
265255
uint32_t idx) {
266-
if (!m_cntrl)
256+
if (!m_cntrl || !m_ptr_obj)
267257
return lldb::ValueObjectSP();
268258

269259
ValueObjectSP valobj_sp = m_backend.GetSP();
270260
if (!valobj_sp)
271261
return lldb::ValueObjectSP();
272262

273263
if (idx == 0)
274-
return valobj_sp->GetChildMemberWithName("__ptr_");
264+
return m_ptr_obj->GetSP();
275265

276266
if (idx == 1) {
277-
if (auto ptr_sp = valobj_sp->GetChildMemberWithName("__ptr_")) {
278-
Status status;
279-
auto value_type_sp =
280-
valobj_sp->GetCompilerType()
281-
.GetTypeTemplateArgument(0).GetPointerType();
282-
ValueObjectSP cast_ptr_sp = ptr_sp->Cast(value_type_sp);
283-
ValueObjectSP value_sp = cast_ptr_sp->Dereference(status);
284-
if (status.Success()) {
285-
return value_sp;
286-
}
287-
}
267+
Status status;
268+
ValueObjectSP value_sp = m_ptr_obj->Dereference(status);
269+
if (status.Success())
270+
return value_sp;
288271
}
289272

290273
return lldb::ValueObjectSP();
@@ -293,6 +276,7 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex(
293276
lldb::ChildCacheState
294277
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
295278
m_cntrl = nullptr;
279+
m_ptr_obj = nullptr;
296280

297281
ValueObjectSP valobj_sp = m_backend.GetSP();
298282
if (!valobj_sp)
@@ -302,6 +286,16 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
302286
if (!target_sp)
303287
return lldb::ChildCacheState::eRefetch;
304288

289+
auto ptr_obj_sp = valobj_sp->GetChildMemberWithName("__ptr_");
290+
if (!ptr_obj_sp)
291+
return lldb::ChildCacheState::eRefetch;
292+
293+
auto cast_ptr_sp = GetDesugaredSmartPointerValue(*ptr_obj_sp, *valobj_sp);
294+
if (!cast_ptr_sp)
295+
return lldb::ChildCacheState::eRefetch;
296+
297+
m_ptr_obj = cast_ptr_sp->Clone(ConstString("pointer")).get();
298+
305299
lldb::ValueObjectSP cntrl_sp(valobj_sp->GetChildMemberWithName("__cntrl_"));
306300

307301
m_cntrl = cntrl_sp.get(); // need to store the raw pointer to avoid a circular
@@ -316,10 +310,12 @@ bool lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
316310

317311
size_t lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
318312
GetIndexOfChildWithName(ConstString name) {
319-
if (name == "__ptr_")
313+
if (name == "pointer")
320314
return 0;
321-
if (name == "$$dereference$$")
315+
316+
if (name == "object" || name == "$$dereference$$")
322317
return 1;
318+
323319
return UINT32_MAX;
324320
}
325321

@@ -423,7 +419,7 @@ size_t lldb_private::formatters::LibcxxUniquePtrSyntheticFrontEnd::
423419
return 0;
424420
if (name == "deleter")
425421
return 1;
426-
if (name == "$$dereference$$")
422+
if (name == "obj" || name == "object" || name == "$$dereference$$")
427423
return 2;
428424
return UINT32_MAX;
429425
}

lldb/source/Plugins/Language/CPlusPlus/LibCxx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class LibcxxSharedPtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
110110

111111
private:
112112
ValueObject *m_cntrl;
113+
ValueObject *m_ptr_obj;
113114
};
114115

115116
class LibcxxUniquePtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {

lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ CompilerType lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::
117117
// wraps a std::pair. Peel away the internal wrapper type - whose structure is
118118
// of no value to users, to expose the std::pair. This matches the structure
119119
// returned by the std::map synthetic provider.
120-
if (isUnorderedMap(
121-
m_backend.GetCompilerType().GetCanonicalType().GetTypeName())) {
120+
if (isUnorderedMap(m_backend.GetCompilerType()
121+
.GetNonReferenceType()
122+
.GetCanonicalType()
123+
.GetTypeName())) {
122124
std::string name;
123125
CompilerType field_type =
124126
element_type.GetFieldAtIndex(0, name, nullptr, nullptr, nullptr);

0 commit comments

Comments
 (0)