Skip to content

Commit d74692e

Browse files
committed
[ASTImporter] Modifying ImportDeclContext(...) to ensure that we also handle the case when the FieldDecl is an ArrayType whose ElementType is a RecordDecl
When we fixed ImportDeclContext(...) in D71378 to make sure we complete each FieldDecl of a RecordDecl when we are importing the definition we missed the case where a FeildDecl was an ArrayType whose ElementType is a record. This fix was motivated by a codegen crash during LLDB expression parsing. Since we were not importing the definition we were crashing during layout which required all the records be defined. Differential Revision: https://reviews.llvm.org/D86660 (cherry picked from commit 6807f24)
1 parent edbe9da commit d74692e

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,12 +1743,28 @@ ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
17431743
Decl *ImportedDecl = *ImportedOrErr;
17441744
FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(ImportedDecl);
17451745
if (FieldFrom && FieldTo) {
1746-
const RecordType *RecordFrom = FieldFrom->getType()->getAs<RecordType>();
1747-
const RecordType *RecordTo = FieldTo->getType()->getAs<RecordType>();
1748-
if (RecordFrom && RecordTo) {
1749-
RecordDecl *FromRecordDecl = RecordFrom->getDecl();
1750-
RecordDecl *ToRecordDecl = RecordTo->getDecl();
1746+
RecordDecl *FromRecordDecl = nullptr;
1747+
RecordDecl *ToRecordDecl = nullptr;
1748+
// If we have a field that is an ArrayType we need to check if the array
1749+
// element is a RecordDecl and if so we need to import the defintion.
1750+
if (FieldFrom->getType()->isArrayType()) {
1751+
// getBaseElementTypeUnsafe(...) handles multi-dimensonal arrays for us.
1752+
FromRecordDecl = FieldFrom->getType()->getBaseElementTypeUnsafe()->getAsRecordDecl();
1753+
ToRecordDecl = FieldTo->getType()->getBaseElementTypeUnsafe()->getAsRecordDecl();
1754+
}
1755+
1756+
if (!FromRecordDecl || !ToRecordDecl) {
1757+
const RecordType *RecordFrom =
1758+
FieldFrom->getType()->getAs<RecordType>();
1759+
const RecordType *RecordTo = FieldTo->getType()->getAs<RecordType>();
1760+
1761+
if (RecordFrom && RecordTo) {
1762+
FromRecordDecl = RecordFrom->getDecl();
1763+
ToRecordDecl = RecordTo->getDecl();
1764+
}
1765+
}
17511766

1767+
if (FromRecordDecl && ToRecordDecl) {
17521768
if (FromRecordDecl->isCompleteDefinition() &&
17531769
!ToRecordDecl->isCompleteDefinition()) {
17541770
Error Err = ImportDefinition(FromRecordDecl, ToRecordDecl);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
class TestImportDefinitionArrayType(TestBase):
7+
8+
mydir = TestBase.compute_mydir(__file__)
9+
10+
def test(self):
11+
self.build()
12+
lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.cpp"))
13+
14+
self.expect_expr("__private->o", result_type="char", result_value="'A'")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This is a reproducer for a crash during codegen. The base issue is when we
2+
// Import the DeclContext we force FieldDecl that are RecordType to be defined
3+
// since we need these to be defined in order to layout the class.
4+
// This case involves an array member whose ElementType are records. In this
5+
// case we need to check the ElementType of an ArrayType and if it is a record
6+
// we need to import the definition.
7+
struct A {
8+
int x;
9+
};
10+
11+
struct B {
12+
// When we import the all the FieldDecl we need to check if we have an
13+
// ArrayType and then check if the ElementType is a RecordDecl and if so
14+
// import the defintion. Otherwise during codegen we will attempt to layout A
15+
// but won't be able to.
16+
A s1[2];
17+
A s2[2][2][3];
18+
char o;
19+
};
20+
21+
class FB {
22+
public:
23+
union {
24+
struct {
25+
unsigned char *_s;
26+
} t;
27+
char *tt[1];
28+
} U;
29+
30+
FB(B *p) : __private(p) {}
31+
32+
// We import A but we don't import the definition.
33+
void f(A **bounds) {}
34+
35+
void init();
36+
37+
private:
38+
B *__private;
39+
};
40+
41+
void FB::init() {
42+
return; // break here
43+
}
44+
45+
int main() {
46+
B b;
47+
FB fb(&b);
48+
49+
b.o = 'A';
50+
51+
fb.init();
52+
}

0 commit comments

Comments
 (0)