Skip to content

Commit 056f6f1

Browse files
committedJun 8, 2019
[LanguageRuntime] Introduce LLVM-style casts
Summary: Using llvm-style rtti gives us stronger guarantees around casting LanguageRuntimes. As discussed in D62755 Differential Revision: https://reviews.llvm.org/D62934 llvm-svn: 362884
1 parent 6ddd7aa commit 056f6f1

26 files changed

+128
-113
lines changed
 

‎lldb/include/lldb/Target/CPPLanguageRuntime.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@ class CPPLanguageRuntime : public LanguageRuntime {
3939

4040
~CPPLanguageRuntime() override;
4141

42+
static char ID;
43+
44+
bool isA(const void *ClassID) const override {
45+
return ClassID == &ID || LanguageRuntime::isA(ClassID);
46+
}
47+
48+
static bool classof(const LanguageRuntime *runtime) {
49+
return runtime->isA(&ID);
50+
}
51+
4252
lldb::LanguageType GetLanguageType() const override {
4353
return lldb::eLanguageTypeC_plus_plus;
4454
}
4555

4656
static CPPLanguageRuntime *GetCPPLanguageRuntime(Process &process) {
47-
return static_cast<CPPLanguageRuntime *>(
57+
return llvm::cast_or_null<CPPLanguageRuntime>(
4858
process.GetLanguageRuntime(lldb::eLanguageTypeC_plus_plus));
4959
}
5060

‎lldb/include/lldb/Target/LanguageRuntime.h

+3
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ class LanguageRuntime : public PluginInterface {
175175
return LLDB_INVALID_ADDRESS;
176176
}
177177

178+
virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
179+
static char ID;
180+
178181
protected:
179182
// Classes that inherit from LanguageRuntime can see and modify these
180183

‎lldb/include/lldb/Target/ObjCLanguageRuntime.h

+10
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ class ObjCLanguageRuntime : public LanguageRuntime {
189189

190190
~ObjCLanguageRuntime() override;
191191

192+
static char ID;
193+
194+
bool isA(const void *ClassID) const override {
195+
return ClassID == &ID || LanguageRuntime::isA(ClassID);
196+
}
197+
198+
static bool classof(const LanguageRuntime *runtime) {
199+
return runtime->isA(&ID);
200+
}
201+
192202
virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
193203

194204
typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;

‎lldb/source/Plugins/Language/ObjC/CF.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ bool lldb_private::formatters::CFBagSummaryProvider(
5050
if (!process_sp)
5151
return false;
5252

53-
ObjCLanguageRuntime *runtime =
54-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
55-
lldb::eLanguageTypeObjC);
53+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
5654

5755
if (!runtime)
5856
return false;
@@ -114,9 +112,7 @@ bool lldb_private::formatters::CFBitVectorSummaryProvider(
114112
if (!process_sp)
115113
return false;
116114

117-
ObjCLanguageRuntime *runtime =
118-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
119-
lldb::eLanguageTypeObjC);
115+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
120116

121117
if (!runtime)
122118
return false;
@@ -236,9 +232,7 @@ bool lldb_private::formatters::CFBinaryHeapSummaryProvider(
236232
if (!process_sp)
237233
return false;
238234

239-
ObjCLanguageRuntime *runtime =
240-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
241-
lldb::eLanguageTypeObjC);
235+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
242236

243237
if (!runtime)
244238
return false;

‎lldb/source/Plugins/Language/ObjC/Cocoa.cpp

+12-32
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ bool lldb_private::formatters::NSBundleSummaryProvider(
4343
if (!process_sp)
4444
return false;
4545

46-
ObjCLanguageRuntime *runtime =
47-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
48-
lldb::eLanguageTypeObjC);
46+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
4947

5048
if (!runtime)
5149
return false;
@@ -93,9 +91,7 @@ bool lldb_private::formatters::NSTimeZoneSummaryProvider(
9391
if (!process_sp)
9492
return false;
9593

96-
ObjCLanguageRuntime *runtime =
97-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
98-
lldb::eLanguageTypeObjC);
94+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
9995

10096
if (!runtime)
10197
return false;
@@ -140,9 +136,7 @@ bool lldb_private::formatters::NSNotificationSummaryProvider(
140136
if (!process_sp)
141137
return false;
142138

143-
ObjCLanguageRuntime *runtime =
144-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
145-
lldb::eLanguageTypeObjC);
139+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
146140

147141
if (!runtime)
148142
return false;
@@ -187,9 +181,7 @@ bool lldb_private::formatters::NSMachPortSummaryProvider(
187181
if (!process_sp)
188182
return false;
189183

190-
ObjCLanguageRuntime *runtime =
191-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
192-
lldb::eLanguageTypeObjC);
184+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
193185

194186
if (!runtime)
195187
return false;
@@ -235,9 +227,7 @@ bool lldb_private::formatters::NSIndexSetSummaryProvider(
235227
if (!process_sp)
236228
return false;
237229

238-
ObjCLanguageRuntime *runtime =
239-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
240-
lldb::eLanguageTypeObjC);
230+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
241231

242232
if (!runtime)
243233
return false;
@@ -426,9 +416,7 @@ bool lldb_private::formatters::NSNumberSummaryProvider(
426416
if (!process_sp)
427417
return false;
428418

429-
ObjCLanguageRuntime *runtime =
430-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
431-
lldb::eLanguageTypeObjC);
419+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
432420

433421
if (!runtime)
434422
return false;
@@ -679,9 +667,7 @@ bool lldb_private::formatters::NSURLSummaryProvider(
679667
if (!process_sp)
680668
return false;
681669

682-
ObjCLanguageRuntime *runtime =
683-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
684-
lldb::eLanguageTypeObjC);
670+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
685671

686672
if (!runtime)
687673
return false;
@@ -795,9 +781,7 @@ bool lldb_private::formatters::NSDateSummaryProvider(
795781
if (!process_sp)
796782
return false;
797783

798-
ObjCLanguageRuntime *runtime =
799-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
800-
lldb::eLanguageTypeObjC);
784+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
801785

802786
if (!runtime)
803787
return false;
@@ -892,9 +876,7 @@ bool lldb_private::formatters::ObjCClassSummaryProvider(
892876
if (!process_sp)
893877
return false;
894878

895-
ObjCLanguageRuntime *runtime =
896-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
897-
lldb::eLanguageTypeObjC);
879+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
898880

899881
if (!runtime)
900882
return false;
@@ -953,9 +935,7 @@ bool lldb_private::formatters::NSDataSummaryProvider(
953935
if (!process_sp)
954936
return false;
955937

956-
ObjCLanguageRuntime *runtime =
957-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
958-
lldb::eLanguageTypeObjC);
938+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
959939

960940
if (!runtime)
961941
return false;
@@ -1054,8 +1034,8 @@ bool lldb_private::formatters::ObjCBooleanSummaryProvider(
10541034
if (!process_sp)
10551035
return false;
10561036

1057-
if (AppleObjCRuntime *objc_runtime =
1058-
(AppleObjCRuntime *)process_sp->GetObjCLanguageRuntime()) {
1037+
if (AppleObjCRuntime *objc_runtime = llvm::dyn_cast_or_null<AppleObjCRuntime>(
1038+
process_sp->GetObjCLanguageRuntime())) {
10591039
lldb::addr_t cf_true = LLDB_INVALID_ADDRESS,
10601040
cf_false = LLDB_INVALID_ADDRESS;
10611041
objc_runtime->GetValuesForGlobalCFBooleans(cf_true, cf_false);

‎lldb/source/Plugins/Language/ObjC/NSArray.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ bool lldb_private::formatters::NSArraySummaryProvider(
344344
if (!process_sp)
345345
return false;
346346

347-
ObjCLanguageRuntime *runtime =
348-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
349-
lldb::eLanguageTypeObjC);
347+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
350348

351349
if (!runtime)
352350
return false;

‎lldb/source/Plugins/Language/ObjC/NSDictionary.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,7 @@ bool lldb_private::formatters::NSDictionarySummaryProvider(
347347
if (!process_sp)
348348
return false;
349349

350-
ObjCLanguageRuntime *runtime =
351-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
352-
lldb::eLanguageTypeObjC);
350+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
353351

354352
if (!runtime)
355353
return false;

‎lldb/source/Plugins/Language/ObjC/NSError.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ lldb_private::formatters::NSErrorSyntheticFrontEndCreator(
187187
lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
188188
if (!process_sp)
189189
return nullptr;
190-
ObjCLanguageRuntime *runtime =
191-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
192-
lldb::eLanguageTypeObjC);
190+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
193191
if (!runtime)
194192
return nullptr;
195193

‎lldb/source/Plugins/Language/ObjC/NSException.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ lldb_private::formatters::NSExceptionSyntheticFrontEndCreator(
179179
lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
180180
if (!process_sp)
181181
return nullptr;
182-
ObjCLanguageRuntime *runtime =
183-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
184-
lldb::eLanguageTypeObjC);
182+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
185183
if (!runtime)
186184
return nullptr;
187185

‎lldb/source/Plugins/Language/ObjC/NSIndexPath.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ class NSIndexPathSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
6868
if (!process_sp)
6969
return false;
7070

71-
ObjCLanguageRuntime *runtime =
72-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
73-
lldb::eLanguageTypeObjC);
71+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
7472

7573
if (!runtime)
7674
return false;

‎lldb/source/Plugins/Language/ObjC/NSSet.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ bool lldb_private::formatters::NSSetSummaryProvider(
225225
if (!process_sp)
226226
return false;
227227

228-
ObjCLanguageRuntime *runtime =
229-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
230-
lldb::eLanguageTypeObjC);
228+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
231229

232230
if (!runtime)
233231
return false;
@@ -304,9 +302,7 @@ lldb_private::formatters::NSSetSyntheticFrontEndCreator(
304302
lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
305303
if (!process_sp)
306304
return nullptr;
307-
ObjCLanguageRuntime *runtime =
308-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
309-
lldb::eLanguageTypeObjC);
305+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
310306
if (!runtime)
311307
return nullptr;
312308

‎lldb/source/Plugins/Language/ObjC/NSString.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ bool lldb_private::formatters::NSStringSummaryProvider(
5959
if (!process_sp)
6060
return false;
6161

62-
ObjCLanguageRuntime *runtime =
63-
(ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
64-
lldb::eLanguageTypeObjC);
62+
ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
6563

6664
if (!runtime)
6765
return false;

‎lldb/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ using namespace lldb_private;
4343

4444
static const char *vtable_demangled_prefix = "vtable for ";
4545

46+
char ItaniumABILanguageRuntime::ID = 0;
47+
4648
bool ItaniumABILanguageRuntime::CouldHaveDynamicValue(ValueObject &in_value) {
4749
const bool check_cxx = true;
4850
const bool check_objc = false;

‎lldb/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class ItaniumABILanguageRuntime : public lldb_private::CPPLanguageRuntime {
3636

3737
static lldb_private::ConstString GetPluginNameStatic();
3838

39+
static char ID;
40+
41+
bool isA(const void *ClassID) const override {
42+
return ClassID == &ID || CPPLanguageRuntime::isA(ClassID);
43+
}
44+
45+
static bool classof(const LanguageRuntime *runtime) {
46+
return runtime->isA(&ID);
47+
}
48+
3949
bool IsVTableName(const char *name) override;
4050

4151
bool GetDynamicTypeAndAddress(ValueObject &in_value,
@@ -86,9 +96,8 @@ class ItaniumABILanguageRuntime : public lldb_private::CPPLanguageRuntime {
8696

8797
ItaniumABILanguageRuntime(Process *process)
8898
: // Call CreateInstance instead.
89-
lldb_private::CPPLanguageRuntime(process),
90-
m_cxx_exception_bp_sp(), m_dynamic_type_map(),
91-
m_dynamic_type_map_mutex() {}
99+
lldb_private::CPPLanguageRuntime(process), m_cxx_exception_bp_sp(),
100+
m_dynamic_type_map(), m_dynamic_type_map_mutex() {}
92101

93102
lldb::BreakpointSP m_cxx_exception_bp_sp;
94103
DynamicTypeCache m_dynamic_type_map;

‎lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
using namespace lldb;
4646
using namespace lldb_private;
4747

48+
char AppleObjCRuntime::ID = 0;
49+
4850
AppleObjCRuntime::~AppleObjCRuntime() {}
4951

5052
AppleObjCRuntime::AppleObjCRuntime(Process *process)

‎lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class AppleObjCRuntime : public lldb_private::ObjCLanguageRuntime {
2828
// because
2929
// you can't make an instance of this generic runtime.
3030

31-
static bool classof(const ObjCLanguageRuntime *runtime) {
32-
switch (runtime->GetRuntimeVersion()) {
33-
case ObjCRuntimeVersions::eAppleObjC_V1:
34-
case ObjCRuntimeVersions::eAppleObjC_V2:
35-
return true;
36-
default:
37-
return false;
38-
}
31+
static char ID;
32+
33+
bool isA(const void *ClassID) const override {
34+
return ClassID == &ID || ObjCLanguageRuntime::isA(ClassID);
35+
}
36+
37+
static bool classof(const LanguageRuntime *runtime) {
38+
return runtime->isA(&ID);
3939
}
4040

4141
// These are generic runtime functions:

‎lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
using namespace lldb;
3838
using namespace lldb_private;
3939

40+
char AppleObjCRuntimeV1::ID = 0;
41+
4042
AppleObjCRuntimeV1::AppleObjCRuntimeV1(Process *process)
4143
: AppleObjCRuntime(process), m_hash_signature(),
4244
m_isa_hash_table_ptr(LLDB_INVALID_ADDRESS) {}

‎lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ class AppleObjCRuntimeV1 : public AppleObjCRuntime {
2929

3030
static lldb_private::ConstString GetPluginNameStatic();
3131

32-
static bool classof(const ObjCLanguageRuntime *runtime) {
33-
switch (runtime->GetRuntimeVersion()) {
34-
case ObjCRuntimeVersions::eAppleObjC_V1:
35-
return true;
36-
default:
37-
return false;
38-
}
32+
static char ID;
33+
34+
bool isA(const void *ClassID) const override {
35+
return ClassID == &ID || AppleObjCRuntime::isA(ClassID);
36+
}
37+
38+
static bool classof(const LanguageRuntime *runtime) {
39+
return runtime->isA(&ID);
3940
}
4041

4142
lldb::addr_t GetTaggedPointerObfuscator();

0 commit comments

Comments
 (0)