Skip to content

Commit c133af7

Browse files
committed
Test class instance size and alignment
1 parent 89886d5 commit c133af7

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

src/parser/cxx/ast_rewriter.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,8 @@ auto ASTRewriter::operator()(BaseSpecifierAST* ast) -> BaseSpecifierAST* {
11991199
copy->accessSpecifier = ast->accessSpecifier;
12001200
copy->symbol = ast->symbol;
12011201

1202+
binder_.bind(ast);
1203+
12021204
return copy;
12031205
}
12041206

@@ -3784,6 +3786,10 @@ auto ASTRewriter::SpecifierVisitor::operator()(ClassSpecifierAST* ast)
37843786
auto value = rewrite(node);
37853787
*baseSpecifierList = make_list_node(arena(), value);
37863788
baseSpecifierList = &(*baseSpecifierList)->next;
3789+
3790+
if (value->symbol) {
3791+
classSymbol->addBaseClass(value->symbol);
3792+
}
37873793
}
37883794

37893795
copy->lbraceLoc = ast->lbraceLoc;
@@ -3800,6 +3806,8 @@ auto ASTRewriter::SpecifierVisitor::operator()(ClassSpecifierAST* ast)
38003806
// copy->symbol = ast->symbol; // TODO: remove done by the binder
38013807
copy->isFinal = ast->isFinal;
38023808

3809+
binder()->complete(copy);
3810+
38033811
return copy;
38043812
}
38053813

src/parser/cxx/symbol_printer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ struct DumpSymbols {
100100
} else {
101101
out << std::format("{} {}\n", classKey, to_string(symbol->name()));
102102
}
103+
for (auto baseClass : symbol->baseClasses()) {
104+
++depth;
105+
visit(*this, baseClass);
106+
--depth;
107+
}
103108
if (!symbol->constructors().empty()) {
104109
++depth;
105110
for (auto constructor : symbol->constructors()) {

tests/api_tests/test_rewriter.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,12 @@ const auto N = S<0, 1, 2>;
259259

260260
TEST(Rewriter, Class) {
261261
auto source = R"(
262+
struct Base {
263+
int x;
264+
};
265+
262266
template <typename T1, typename T2>
263-
struct Pair {
267+
struct Pair : Base {
264268
T1 first;
265269
T2 second;
266270
auto operator=(const Pair& other) -> Pair&;
@@ -309,8 +313,12 @@ using Pair1 = Pair<int, float*>;
309313
dump(os, classDeclInstance->symbol);
310314

311315
ASSERT_EQ(os.str(), R"(class Pair
316+
base class Base
312317
field int first
313318
field float* second
314319
function ::Pair& operator =(const ::Pair&)
315320
)");
316-
}
321+
322+
ASSERT_EQ(classInstance->sizeInBytes(), 12);
323+
ASSERT_EQ(classInstance->alignment(), 4);
324+
}

tests/api_tests/test_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct Source {
4747

4848
explicit Source(std::string_view source, bool templateInstantiation = true) {
4949
// default to wasm32 memory layout
50-
auto memoryLayout = std::make_unique<MemoryLayout>(32);
50+
memoryLayout = std::make_unique<MemoryLayout>(32);
5151

5252
unit.control()->setMemoryLayout(memoryLayout.get());
5353

tests/unit_tests/sema/using_decl_01.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ auto main() -> int {
3636
// CHECK-NEXT: function operator bool() const
3737
// CHECK-NEXT: function void f()
3838
// CHECK-NEXT: class Derived
39+
// CHECK-NEXT: base class Base
3940
// CHECK-NEXT: using operator bool() const
4041
// CHECK-NEXT: using void f()
4142
// CHECK-NEXT: function int main()
4243
// CHECK-NEXT: block
4344
// CHECK-NEXT: using decltype(nullptr) nullptr_t
44-

0 commit comments

Comments
 (0)