Skip to content

Commit 5d5cf90

Browse files
author
Enrico Granata
committed
Adjust LLDB for changes to the layout of _SwiftTypePreservingNSNumber
apple-llvm-split-commit: d665662a1f3d75f2b2d5004abc61e78d2f1f7c56 apple-llvm-split-dir: lldb/
1 parent 0f5db02 commit 5d5cf90

File tree

3 files changed

+181
-83
lines changed

3 files changed

+181
-83
lines changed

lldb/packages/Python/lldbsuite/test/lang/swift/variables/bridged_array/TestSwiftBridgedArray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def do_test(self):
6565

6666
self.expect(
6767
"frame variable -d run -- swarr",
68-
substrs=['123456', '234567', '345678', '1.25'])
68+
substrs=['Int(123456)', 'Int32(234567)', 'UInt16(45678)', 'Double(1.250000)', 'Float(2.500000)'])
6969
self.expect(
7070
"expression -d run -- swarr",
71-
substrs=['123456', '234567', '345678', '1.25'])
71+
substrs=['Int(123456)', 'Int32(234567)', 'UInt16(45678)', 'Double(1.250000)', 'Float(2.500000)'])
7272

7373
if __name__ == '__main__':
7474
import atexit

lldb/packages/Python/lldbsuite/test/lang/swift/variables/bridged_array/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import Foundation
1313

1414
func main() {
15-
var nsarr: NSArray = NSArray(array: [123456,234567,345678,1.25,false])
15+
var nsarr: NSArray = NSArray(array: [Int(123456),Int32(234567),UInt16(45678),Double(1.25),Float(2.5)])
1616
var swarr = unsafeBitCast(unsafeBitCast(nsarr, to: Int.self), to: NSArray.self) as! [AnyObject]
1717
print("break here")
1818
}

lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp

Lines changed: 178 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -581,85 +581,183 @@ lldb_private::formatters::swift::ObjC_Selector_SummaryProvider (ValueObject& val
581581
return StringPrinter::ReadStringAndDumpToStream<StringPrinter::StringElementType::ASCII>(read_options);
582582
}
583583

584-
bool
585-
lldb_private::formatters::swift::TypePreservingNSNumber_SummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
586-
{
587-
lldb::addr_t ptr_value(valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS));
588-
if (ptr_value == LLDB_INVALID_ADDRESS)
589-
return false;
590-
591-
ProcessSP process_sp(valobj.GetProcessSP());
592-
if (!process_sp)
593-
return false;
594-
595-
uint32_t ptr_size = process_sp->GetAddressByteSize();
596-
const uint32_t size_of_tag = 4;
597-
const uint32_t size_of_payload = 8;
598-
599-
lldb::addr_t addr_of_tag = ptr_value + ptr_size;
600-
lldb::addr_t addr_of_payload = addr_of_tag + size_of_tag;
601-
602-
Error read_error;
603-
uint64_t tag = process_sp->ReadUnsignedIntegerFromMemory(addr_of_tag, size_of_tag, 0, read_error);
604-
if (read_error.Fail() || tag == 0 || tag > 6)
605-
return false;
606-
607-
DataBufferSP buffer_sp(new DataBufferHeap(size_of_payload,0));
608-
process_sp->ReadMemoryFromInferior(addr_of_payload, buffer_sp->GetBytes(), size_of_payload, read_error);
609-
if (read_error.Fail())
610-
return false;
611-
612-
switch (tag)
613-
{
614-
case 1: // Int
615-
{
616-
uint64_t payload = 0;
617-
memcpy(&payload, buffer_sp->GetBytes(), sizeof(payload));
618-
stream.Printf("Int(%" PRId64 ")", payload);
619-
return true;
620-
}
621-
case 2: // UInt
622-
{
623-
uint64_t payload = 0;
624-
memcpy(&payload, buffer_sp->GetBytes(), sizeof(payload));
625-
stream.Printf("UInt(%" PRIu64 ")", payload);
626-
return true;
627-
}
628-
case 3: // Float
629-
{
630-
float payload = 0;
631-
memcpy(&payload, buffer_sp->GetBytes(), sizeof(payload));
632-
stream.Printf("Float(%f)", payload);
633-
return true;
634-
}
635-
case 4: // Double
636-
{
637-
double payload = 0;
638-
memcpy(&payload, buffer_sp->GetBytes(), sizeof(payload));
639-
stream.Printf("Double(%f)", payload);
640-
return true;
641-
}
642-
case 5: // CGFloat
643-
{
644-
if (ptr_size == 4)
645-
{
646-
float payload = 0;
647-
memcpy(&payload, buffer_sp->GetBytes(), sizeof(payload));
648-
stream.Printf("CGFloat(%f)", payload);
649-
return true;
650-
}
651-
else if (ptr_size == 8)
652-
{
653-
double payload = 0;
654-
memcpy(&payload, buffer_sp->GetBytes(), sizeof(payload));
655-
stream.Printf("CGFloat(%f)", payload);
656-
return true;
657-
}
658-
break;
659-
}
660-
default:
661-
break;
662-
}
663-
584+
template <int Key> struct TypePreservingNSNumber;
585+
586+
template <> struct TypePreservingNSNumber<0> {
587+
typedef int64_t SixtyFourValueType;
588+
typedef int32_t ThirtyTwoValueType;
589+
590+
static constexpr const char *FormatString = "Int(%" PRId64 ")";
591+
};
592+
593+
template <> struct TypePreservingNSNumber<1> {
594+
typedef int64_t ValueType;
595+
static constexpr const char *FormatString = "Int64(%" PRId64 ")";
596+
};
597+
598+
template <> struct TypePreservingNSNumber<2> {
599+
typedef int32_t ValueType;
600+
static constexpr const char *FormatString = "Int32(%" PRId32 ")";
601+
};
602+
603+
template <> struct TypePreservingNSNumber<3> {
604+
typedef int16_t ValueType;
605+
static constexpr const char *FormatString = "Int16(%" PRId16 ")";
606+
};
607+
608+
template <> struct TypePreservingNSNumber<4> {
609+
typedef int8_t ValueType;
610+
static constexpr const char *FormatString = "Int8(%" PRId8 ")";
611+
};
612+
613+
template <> struct TypePreservingNSNumber<5> {
614+
typedef uint64_t SixtyFourValueType;
615+
typedef uint32_t ThirtyTwoValueType;
616+
617+
static constexpr const char *FormatString = "UInt(%" PRIu64 ")";
618+
};
619+
620+
template <> struct TypePreservingNSNumber<6> {
621+
typedef uint64_t ValueType;
622+
static constexpr const char *FormatString = "UInt64(%" PRIu64 ")";
623+
};
624+
625+
template <> struct TypePreservingNSNumber<7> {
626+
typedef uint32_t ValueType;
627+
static constexpr const char *FormatString = "UInt32(%" PRIu32 ")";
628+
};
629+
630+
template <> struct TypePreservingNSNumber<8> {
631+
typedef uint16_t ValueType;
632+
static constexpr const char *FormatString = "UInt16(%" PRIu16 ")";
633+
};
634+
635+
template <> struct TypePreservingNSNumber<9> {
636+
typedef uint8_t ValueType;
637+
static constexpr const char *FormatString = "UInt8(%" PRIu8 ")";
638+
};
639+
640+
template <> struct TypePreservingNSNumber<10> {
641+
typedef float ValueType;
642+
static constexpr const char *FormatString = "Float(%f)";
643+
};
644+
645+
template <> struct TypePreservingNSNumber<11> {
646+
typedef double ValueType;
647+
static constexpr const char *FormatString = "Double(%f)";
648+
};
649+
650+
template <> struct TypePreservingNSNumber<12> {
651+
typedef double SixtyFourValueType;
652+
typedef float ThirtyTwoValueType;
653+
654+
static constexpr const char *FormatString = "CGFloat(%f)";
655+
};
656+
657+
template <> struct TypePreservingNSNumber<13> {
658+
typedef bool ValueType;
659+
static constexpr const char *FormatString = "Bool(%d)";
660+
};
661+
662+
template <int Key,
663+
typename Value = typename TypePreservingNSNumber<Key>::ValueType>
664+
bool PrintTypePreservingNSNumber(DataBufferSP buffer_sp, Stream &stream) {
665+
Value value;
666+
memcpy(&value, buffer_sp->GetBytes(), sizeof(value));
667+
stream.Printf(TypePreservingNSNumber<Key>::FormatString, value);
668+
return true;
669+
}
670+
671+
template <>
672+
bool PrintTypePreservingNSNumber<13, void>(DataBufferSP buffer_sp,
673+
Stream &stream) {
674+
typename TypePreservingNSNumber<13>::ValueType value;
675+
memcpy(&value, buffer_sp->GetBytes(), sizeof(value));
676+
stream.PutCString(value ? "true" : "false");
677+
return true;
678+
}
679+
680+
template <int Key, typename SixtyFour =
681+
typename TypePreservingNSNumber<Key>::SixtyFourValueType,
682+
typename ThirtyTwo =
683+
typename TypePreservingNSNumber<Key>::ThirtyTwoValueType>
684+
bool PrintTypePreservingNSNumber(DataBufferSP buffer_sp, ProcessSP process_sp,
685+
Stream &stream) {
686+
switch (process_sp->GetAddressByteSize()) {
687+
case 4: {
688+
ThirtyTwo value;
689+
memcpy(&value, buffer_sp->GetBytes(), sizeof(value));
690+
stream.Printf(TypePreservingNSNumber<Key>::FormatString, (SixtyFour)value);
691+
return true;
692+
}
693+
case 8: {
694+
SixtyFour value;
695+
memcpy(&value, buffer_sp->GetBytes(), sizeof(value));
696+
stream.Printf(TypePreservingNSNumber<Key>::FormatString, value);
697+
return true;
698+
}
699+
}
700+
701+
llvm_unreachable("unknown address byte size");
702+
}
703+
704+
bool lldb_private::formatters::swift::TypePreservingNSNumber_SummaryProvider(
705+
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
706+
lldb::addr_t ptr_value(valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS));
707+
if (ptr_value == LLDB_INVALID_ADDRESS)
664708
return false;
709+
710+
ProcessSP process_sp(valobj.GetProcessSP());
711+
if (!process_sp)
712+
return false;
713+
714+
uint32_t ptr_size = process_sp->GetAddressByteSize();
715+
const uint32_t size_of_tag = 1;
716+
const uint32_t size_of_payload = 8;
717+
718+
lldb::addr_t addr_of_payload = ptr_value + ptr_size;
719+
lldb::addr_t addr_of_tag = addr_of_payload + size_of_payload;
720+
721+
Error read_error;
722+
uint64_t tag = process_sp->ReadUnsignedIntegerFromMemory(
723+
addr_of_tag, size_of_tag, 0, read_error);
724+
if (read_error.Fail())
725+
return false;
726+
727+
DataBufferSP buffer_sp(new DataBufferHeap(size_of_payload, 0));
728+
process_sp->ReadMemoryFromInferior(addr_of_payload, buffer_sp->GetBytes(),
729+
size_of_payload, read_error);
730+
if (read_error.Fail())
731+
return false;
732+
733+
#define PROCESS_DEPENDENT_TAG(Key) \
734+
case Key: \
735+
return PrintTypePreservingNSNumber<Key>(buffer_sp, process_sp, stream);
736+
#define PROCESS_INDEPENDENT_TAG(Key) \
737+
case Key: \
738+
return PrintTypePreservingNSNumber<Key>(buffer_sp, stream);
739+
740+
switch (tag) {
741+
PROCESS_DEPENDENT_TAG(0);
742+
PROCESS_INDEPENDENT_TAG(1);
743+
PROCESS_INDEPENDENT_TAG(2);
744+
PROCESS_INDEPENDENT_TAG(3);
745+
PROCESS_INDEPENDENT_TAG(4);
746+
PROCESS_DEPENDENT_TAG(5);
747+
PROCESS_INDEPENDENT_TAG(6);
748+
PROCESS_INDEPENDENT_TAG(7);
749+
PROCESS_INDEPENDENT_TAG(8);
750+
PROCESS_INDEPENDENT_TAG(9);
751+
PROCESS_INDEPENDENT_TAG(10);
752+
PROCESS_INDEPENDENT_TAG(11);
753+
PROCESS_DEPENDENT_TAG(12);
754+
PROCESS_INDEPENDENT_TAG(13);
755+
default:
756+
break;
757+
}
758+
759+
#undef PROCESS_DEPENDENT_TAG
760+
#undef PROCESS_INDEPENDENT_TAG
761+
762+
return false;
665763
}

0 commit comments

Comments
 (0)