Skip to content

Commit a925152

Browse files
committed
Hoist static locals to module scope
1 parent f467a98 commit a925152

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,8 @@ bool Converter::VisitCompoundStmt(clang::CompoundStmt *stmt) {
13381338

13391339
bool Converter::VisitDeclStmt(clang::DeclStmt *stmt) {
13401340
for (auto *decl : stmt->decls()) {
1341-
if (clang::isa<clang::TagDecl>(decl)) {
1341+
auto *var = clang::dyn_cast<clang::VarDecl>(decl);
1342+
if (clang::isa<clang::TagDecl>(decl) || (var && var->isStaticLocal())) {
13421343
Buffer buf(*this);
13431344
Convert(decl);
13441345
hoisted_records_ += std::move(buf).str();

‎tests/unit/static_local.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ int foo() {
1111
return kX1 + kX2 + static_i;
1212
}
1313

14+
int from_local_class() {
15+
static int x = 3;
16+
struct S {
17+
int get() const { return ++x; }
18+
};
19+
return S{}.get() + S{}.get();
20+
}
21+
1422
int main() {
1523
assert(foo() + foo() + foo() == 15);
24+
assert(from_local_class() == 9);
25+
assert(from_local_class() == 13);
1626
return 0;
1727
}

0 commit comments

Comments
 (0)