Skip to content

Commit b448d1b

Browse files
committed
[lldb] Fix dynamic_cast by no longer failing on variable without metadata
Summary: Our IR rewriting infrastructure currently fails when it encounters a variable which has no metadata associated. This causes dynamic_cast to fail as in this case IRForTarget considers the type info pointers ('@_ZTI...') to be variables without associated metadata. As there are no variables for these internal variables, this is actually not an error and dynamic_cast would work fine if we didn't throw this error. This patch fixes this by removing this diagnostics code. In case we would actually hit a variable that has no metadata (but is supposed to have), we still have the error in the expression log so this shouldn't make it harder to diagnose any missing metadata errors. This patch should fix dynamic_cast and also adds a bunch of test coverage to that language feature. Fixes rdar://10813639 Reviewers: davide, labath Reviewed By: labath Subscribers: friss, labath, abidh, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D65932 llvm-svn: 368511
1 parent c4b5b66 commit b448d1b

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "ExtBase.h"
2+
3+
char ExtBase::bar() {
4+
return 'x';
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ExtBase {
2+
virtual char bar();
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
LEVEL = ../../../make
2+
CXX_SOURCES := main.cpp ExtBase.cpp
3+
include $(LEVEL)/Makefile.rules
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from lldbsuite.test import lldbinline
2+
3+
lldbinline.MakeInlineTest(__file__, globals(), [])
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "ExtBase.h"
2+
3+
class Base {
4+
public:
5+
virtual char foo() {
6+
return 'b';
7+
}
8+
};
9+
10+
class Derived : public Base {
11+
public:
12+
char foo() override {
13+
return 'd';
14+
}
15+
};
16+
17+
class NonOverrideDerived : public Base {
18+
};
19+
20+
class ExtDerived : public ExtBase {
21+
public:
22+
char bar() override {
23+
return 'y';
24+
}
25+
};
26+
27+
int main() {
28+
Derived d;
29+
NonOverrideDerived d2;
30+
Base *b = &d;
31+
Base *real_base = new Base();
32+
char c = dynamic_cast<Derived *>(b)->foo();
33+
34+
ExtDerived ext_d;
35+
ExtBase *ext_b = &ext_d;
36+
ExtBase *ext_real_base = new ExtBase();
37+
c = dynamic_cast<ExtDerived *>(ext_b)->bar();
38+
39+
40+
return 0; //% self.expect("expression dynamic_cast<class Derived *>(b) == (Derived*)b", substrs = ["bool", " = true"])
41+
//% self.expect("expression dynamic_cast<class Base *>(b) == (Base*)b", substrs = ["bool", " = true"])
42+
//% self.expect("expression dynamic_cast<class Derived *>(real_base) == nullptr", substrs = ["bool", " = true"])
43+
//% self.expect("expression dynamic_cast<class NonOverrideDerived *>(&d) == nullptr", substrs = ["bool", " = true"])
44+
//% self.expect("expression dynamic_cast<class ExtDerived *>(real_base) == nullptr", substrs = ["bool", " = true"])
45+
//% self.expect("expression dynamic_cast<class Derived *>(&d2) == nullptr", substrs = ["bool", " = true"])
46+
//% self.expect("expression dynamic_cast<class NonOverrideDerived *>(&d2) == (NonOverrideDerived *)&d2", substrs = ["bool", " = true"])
47+
//% self.expect("expression dynamic_cast<class Derived *>(&ext_d) == nullptr", substrs = ["bool", " = true"])
48+
//% self.expect("expression dynamic_cast<class ExtDerived *>(ext_b) == (class ExtDerived*)ext_b", substrs = ["bool", " = true"])
49+
//% self.expect("expression dynamic_cast<class ExtBase *>(ext_real_base) == (class ExtBase*)ext_real_base", substrs = ["bool", " = true"])
50+
//% self.expect("expression dynamic_cast<class ExtDerived *>(ext_real_base) == nullptr", substrs = ["bool", " = true"])
51+
}

lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,16 +1265,10 @@ bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) {
12651265
clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
12661266

12671267
if (!named_decl) {
1268-
if (IsObjCSelectorRef(llvm_value_ptr))
1269-
return true;
1270-
1271-
if (!global_variable->hasExternalLinkage())
1272-
return true;
1273-
12741268
LLDB_LOG(log, "Found global variable \"{0}\" without metadata",
12751269
global_variable->getName());
12761270

1277-
return false;
1271+
return true;
12781272
}
12791273

12801274
llvm::StringRef name(named_decl->getName());

0 commit comments

Comments
 (0)